永发信息网

100分寻求用VB求解一元三次方程的源码

答案:2  悬赏:50  手机版
解决时间 2021-02-23 09:56
100分寻求用VB求解一元三次方程的源码
最佳答案
看来是没人会啊,建议你自己查找相关资料,先用公式法或者迭代法做,然后自己编程序,用相应的算法吧,祝你好运,下面是个c语言的,希望对你有用
#include "Stdio.h"
#include "Conio.h"
#include "math.h"

main()
{
float a,b,c,d;
float x=0;
float result;
scanf("%f%f%f%f",&a,&b,&c,&d);

result=-(a*x*x*x+b*x*x+d)/c;

while(abs(result-x)>0.001)
{ x=result;
result=-(a*x*x*x+b*x*x+d)/c;
}
printf("A base:x=%f",result);
}
全部回答
Sub SolveCubicEquations(ByVal CubicEquation As String, Optional ByVal x As String = "x", Optional ByRef result As String)
Dim a As Single, b As Single, c As Single, d As Single, temp As String, n As Byte
Dim f As Single, g As Single, h As Single, i As Single, j As Single, alpha As Single
CubicEquation = Replace(CubicEquation, " ", "")
result = Replace(CubicEquation, "-", "+-")
s = Split(Split(result, "=")(0), "+")
For n = 0 To UBound(s)
If s(n) Like "*" & x & "^3" Then temp = Trim(Split(s(n), x)(0)): a = IIf(temp = "-", -1, IIf(temp = "", 1, Val(temp)))
If s(n) Like "*" & x & "^2" Then temp = Trim(Split(s(n), x)(0)): b = IIf(temp = "-", -1, IIf(temp = "", 1, Val(temp)))
If s(n) Like "*" & x Then temp = Trim(Split(s(n), x)(0)): c = IIf(temp = "-", -1, IIf(temp = "", 0, Val(temp)))
If IsNumeric(s(n)) Then d = s(n)
Next
f = c / a - b * b / (3 * a * a)
g = 2 * b ^ 3 / (3 * a) ^ 3 - b * c / (3 * a * a) + d / a
h = g ^ 2 / 4 + f ^ 3 / 27
Select Case Sgn(h)
Case -1 'Roots Are Real
i = Sqr(g ^ 2 / 4 - h)
j = -g / (2 * i)
If j = 1 Then alpha = 0
If j <> 1 Then alpha = (Atn(-j / Sqr(1 - j ^ 2)) + 2 * Atn(1)) / 3
result = "Cubic Equations {" & CubicEquation & "} has 3 Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(2 * i ^ (1 / 3) * Cos(alpha) - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-i ^ (1 / 3) * (Cos(alpha) + (3 ^ 0.5) * Sin(alpha)) - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "3=" & Format(-i ^ (1 / 3) * (Cos(alpha) - (3 ^ 0.5) * Sin(alpha)) - b / (3 * a), "0.0000")
Case 0 'All 3 Roots Are Real and Equal
result = "Cubic Equation {" & CubicEquation & "} has 3 Equal Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(-(d / a) ^ (1 / 3), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-(d / a) ^ (1 / 3), "0.0000")
result = result & vbCrLf & x & "3=" & Format(-(d / a) ^ (1 / 3), "0.0000")
Case 1 'Only 1 Root Is Real
i = (-g / 2 + h ^ 0.5) ^ (1 / 3)
j = -(g / 2 + h ^ 0.5) ^ (1 / 3)
result = "Cubic Equations {" & CubicEquation & "} has only 1 Real Roots:" & vbCrLf & String(50, "-")
result = result & vbCrLf & x & "1=" & Format(i + j - b / (3 * a), "0.0000")
result = result & vbCrLf & x & "2=" & Format(-(i + j) / 2 - b / (3 * a), "0.0000") & "+" & Format(Abs(i - j) * 3 ^ 0.5 / 2, "0.0000") & "*i"
result = result & vbCrLf & x & "3=" & Format(-(i + j) / 2 - b / (3 * a), "0.0000") & "-" & Format(Abs(i - j) * 3 ^ 0.5 / 2, "0.0000") & "*i"
End Select
result = Replace(result, "0.0000+", "")
result = Replace(result, "0.0000-", "")
result = Replace(result, "0.0000", 0)
result = Replace(result, ".0000", "")
result = result & vbCrLf
Debug.Print result
End Sub
Sub macro1()
SolveCubicEquations "2x^3-4x^2-22x+24=0"
SolveCubicEquations "x^3 + 6x^2 + 12x + 8 = 0"
SolveCubicEquations "y^3 + 7y -9 = 0", "y"
SolveCubicEquations "3z^3 + 5z = 0", "z"
SolveCubicEquations "-2x^3 + 8x^2 = 0", "x"
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
阿里巴巴详情页不打字就没有五星吗
芜湖贝斯特新能源开发有限公司在哪里啊,我有
"可见一斑"用日语怎么说?
男主角名字有凌 都市小说 好像是叫郭凌峰还是
甲水泥厂因无力支付建厂赊购钢材的贷款,遂向
狗吃狗肉吗?如果不吃,那为什么?
起旺饭店在哪里啊,我有事要去这个地方
理发店现在帮客人洗完头发后都需要按摩背部吗
什么是存档覆盖啊。。。。。。教教我我在玩传
广东省有多少人口
【浪花朵朵】浪花朵朵 作文
为什么http:// 有反斜杠是错误
溧泉在哪里啊,我有事要去这个地方
描写秋天是什么的一段话
中国邮政(吕王镇邮政支局)这个地址在什么地方
推荐资讯
2016年的元旦是星期五,那么据此你知道2015年
威海福美瑞动力设备有限公司地址在什么地方,
下列各项中,表述错误的选项是()A.应付账款
关山梁在什么地方啊,我要过去处理事情
求韩国电影妈妈的朋友百度云
巨林天下城怎么去啊,有知道地址的么
下图是草莓酒制作的步骤。下列分析中正确的是
如何辨别耳机音质好坏?
用输钱的方法玩;电脑版支持那些?
Hi, I am looking for an English native spe
下图是某水库流入水量和流出水量在一年中的变
P为椭圆x的平方/a的平方+y的平方/b的平方=1上
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?