vb 一元二次方程

Private Sub Command1_Click()
Dim a!, b!, c!, x1!, x2!, disc!
a = Val(Text1)
b = Val(Text4)
c = Val(Text3)
disc = b * b - 4 * a * c
If Text1 < -20 Or Text1 > 20 Or Text4 < -20 Or Text4 > 20 Or Text3 < -20 Or Text3 > 20 Then
MsgBox "超出范围 请在0-20内输入", 2 + vbExclamation, "错误提示"
Else
If a = 0 Then
x = Text3 / -Text4
Text2.Visible = False
Text5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label3.Visible = True
Text6.Visible = True
Label1.Visible = True
Label8.Visible = False
Text6.Text = Str$(x)
Else
If disc > 0 Then
x1 = (-b + Sqr(disc)) / (2 * a)
x2 = (-b - Sqr(disc)) / (2 * a)
Label1.Visible = True
Label8.Visible = False
Label7.Visible = True
Label6.Visible = True
Text5.Visible = True
Text2.Visible = True
Text2.Text = Str$(x1)
Text5.Text = Str$(x2)
ElseIf disc = 0 Then
x = (-b) / (2 * a)
Text2.Visible = False
Text5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label3.Visible = True
Text6.Visible = True
Label1.Visible = True
Label8.Visible = False
Text6.Text = Str$(x)
Else
Label8.Visible = True
End If
End If
End If
End Sub

Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Label8.Visible = False
Label7.Visible = False
Label6.Visible = False
Label1.Visible = False
Text2.Visible = False
Text5.Visible = False
Label3.Visible = False
Text6.Visible = False
End Sub

Private Sub Form_Load()
Label6.Visible = False
Text2.Visible = False
Label3.Visible = False
Text6.Visible = False
Label1.Visible = False
Label7.Visible = False
Text5.Visible = False
Label8.Visible = False
End Sub
这个算出来的答案怎么老是不对?
高手们帮忙解决哈!谢谢

原代码有如下几个地方需要注意:
1、text是个控件,默认text的值是字符串形式的,如果要和数值比较,要将它转换为数值。都用val函数转换了,比较时就该用转换后的值。
2、msgbox既可以做语句,也可以做函数,如果让它有“终止”、“重试”、“忽略”三个按钮,那么,要针对这三个按钮分别做一下处理。否则,不如默认它只有“确定”一个按钮。
3、考虑到a=0,但没考虑到b=0,原代码中有b做除数的情况,这时候一定要把b=0的情况排除掉,不然会出错。既然要全面考虑,那就应该考虑到各种情况。

Command1_Click()的代码如下(Command2和Form的事件过程没做改动):
Private Sub Command1_Click()
Dim a!, b!, c!, x1!, x2!, disc!, f%
Dim flag As Boolean
a = Val(Trim(Text1))
b = Val(Trim(Text4))
c = Val(Trim(Text3))
flag = True
If a < -20 Or a > 20 Or b < -20 Or b > 20 Or c < -20 Or c > 20 Then
f = MsgBox("超出范围 请在0-20内输入", 2 + vbExclamation, "错误提示")
If f = 3 Then flag = False '选择“终止”,则什么都不做
If f = 4 Then '选择“重试”,则将不符合要求的输入清空
If a < -20 Or a > 20 Then Text1.Text = ""
If b < -20 Or b > 20 Then Text4.Text = ""
If c < -20 Or c > 20 Then Text3.Text = ""
flag = False
End If
If f = 5 Then flag = True '选择“忽略”,则继续运行
End If
disc = b * b - 4 * a * c
If flag = True Then
If a = 0 Then
Text2.Visible = False
Text5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label3.Visible = True
Text6.Visible = True
Label1.Visible = True
Label8.Visible = False
If b = 0 And c = 0 Then
Text6.Text = "解为任意数"
ElseIf b = 0 And c <> 0 Then
Text6.Text = "无解"
Else '即a=0且b<>0且c<>0的情况
x = Format(-c / b, ".00")
Text6.Text = x
End If
Else
If disc > 0 Then
Label1.Visible = True
Label8.Visible = False
Label7.Visible = True
Label6.Visible = True
Text5.Visible = True
Text2.Visible = True
x1 = Format((-b + Sqr(disc)) / (2 * a), ".00")
x2 = Format((-b - Sqr(disc)) / (2 * a), ".00")
Text2.Text = x1
Text5.Text = x2
ElseIf disc = 0 Then
Text2.Visible = False
Text5.Visible = False
Label6.Visible = False
Label7.Visible = False
Label3.Visible = True
Text6.Visible = True
Label1.Visible = True
Label8.Visible = False
x = Format((-b) / (2 * a), ".00")
Text6.Text = x
Else
Text6.Text = "无解"
Label8.Visible = True
End If
End If
End If
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-05-17
你算什麽啊..什麽乱七八糟的代码
第2个回答  2009-05-17
Private Sub Command1_Click()
If a <> 0 And b^2 - 4 * a * c <0 Then
Text4 = (-b) / (2 * a) & "+j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
Text5 = (-b) / (2 * a) & "-j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
End If
End Sub

全部:
Dim a, b, c As Integer

Private Sub Command1_Click()
a = Val(Text1)
b = Val(Text2)
c = Val(Text3)
If a = 0 Then
MsgBox "这是一元一次方程"
x = -c / b
Text4.Visible = False
Text5 = x
Else
If a <> 0 And (b * b) - 4 * a * c >= 0 Then
Text4.Text = Val((-b + Sqr((b * b) - 4 * a * c)) / 2 * a)
Text5.Text = Val((-b - Sqr((b * b) - 4 * a * c)) / 2 * a)
ElseIf a <> 0 And (b * b) - 4 * a * c < 0 Then
Text4 = (-b) / (2 * a) & "+j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
Text5 = (-b) / (2 * a) & "-j" & Sqr(-b ^ 2 + 4 * a * c) / (2 * a)
End If
End If
End Sub

Private Sub Text1_Click()
Text1 = InputBox("请输入数值", "输入框")
End Sub

Private Sub Text2_Click()
Text2 = InputBox("请输入数值", "输入框")
End Sub

Private Sub Text3_Click()
Text3 = InputBox("请输入数值", "输入框")

如何利用vb制作一元二次方程求根系统?
启动VB编程程序。可以到官方网上下载visual basic程序,然后进行安装,安装结束,双击打开程序,新建一个工程。设计求根系统面板。这个面板由6个标签控件,6个文本框和两个命令按钮组成。单击标签控件A,在form面板上插入6个标签。命名标签。右击标签1,打开属性窗口。把标签名称改为a,同样的把标签2,3,,...

一元二次方程计算机解
在VB编程中,可以使用以下代码来实现一元二次方程的求解。这个代码适用于一般形式的一元二次方程 ax^2 + bx + c = 0,并会以对话框的形式显示解的结果。首先,需要定义变量a、b和c,以及可能的解x1和x2,例如:vbDim a As Double, b As Double, c As Double, x1 As Double, x2 As Do...

VB6.0实现一元二次方程的求解
打开VB6.0,新建一个工程,在窗体中添加两个命令按钮,caption分别改为“计算”、“输出”,将窗体Form1的caption属性改为“解一元二次方程”,添加5个标控件,将其caption属性分别改为“a”、"b”、“c”、“X1”、“X2”,添加5个文本框,并将其Text属性清空,将界面设置成下图 一元二次...

VB编一元二次方程
MsgBox "此方程无实数根", 0, "错误"Text1.Text = ""Text2.Text = ""Text3.Text = ""Exit Sub ElseIf A = 0 Then MsgBox "二次项系数不能为0", 0, "错误"Else IntSqr Pbs, PbsZs, PbsXs MsgBox "X1=〔" & -B & "+" & PbsZs & PbsXs & " 〕\/" & (2 * A) & C...

VB6.0一元二次方程计算器怎么让计算结果保留根号与分数
Private Sub Command1_Click()'“x2-8x+1=0”答案是4+根号15 与 4-根号15。Dim A As Double A = Text1.Text Dim B As Double B = Text2.Text Dim C As Double C = Text3.Text Dim D As Double D = B ^ 2 - 4 * A * C If D < 0 Then MsgBox ("此题无实数解"): ...

vb一元二次方程,要求有函数图像。不会编,有没有大佬会做,在线等挺急...
只含有一个未知数(一元),并且未知数项的最高次数是2(二次)的整式方程叫做一元二次方程。标准形式为:ax²+bx+c=0(a≠0)。其中ax²是二次项,a是二次项系数;bx是一次项;b是一次项系数;c是常数项。使方程左右两边相等的未知数的值就是这个一元二次方程的解,一元二次...

用VB编写求一元二次方程的代码是什么?
'求一元二次方程的跟ax^2+bx+c=0 Private Sub Command1_Click()Dim a#, b#, c#, gen a = InputBox("输入A的值")b = InputBox("输入B的值")c = InputBox("输入C的值")If a = 0 And b = 0 And c = 0 Then Print "x的值是全体实数."ElseIf a = 0 And b = 0 And ...

如何用VB求一元二次方程的虚数解(一定是虚数解)。
vb代码如下:Private Sub Command1_Click()Dim a As Single, b As Single, c As Single Dim d As Single, x1 As Single, x2 As Single a = InputBox("请输入一元二次方程的系数a")b = InputBox("请输入一元二次方程的系数b")c = InputBox("请输入一元二次方程的系数c")If a = ...

求解一道VB题:输入一元二次方程的系数a,b,c的值,编程求解一元二次方程a...
Dim a As Single, b As Single, c As Single, t As Double a = Val (Text1.Text) b = Val (Text2.Text)。含义及特点:(1)一元二次方程的解(根)的意义:能使一元二次方程左右两边相等的未知数的值称为一元二次方程的解。一般情况下,一元二次方程的解也称为一元二次方程的...

VB解一元二次方程
Private Sub Command1_Click()a = Val(Text1, Text) Text1, Text中间是.不是,b = Val(Text2, Text) 同样 c = Val(taxt3, Text) 同样 X1 = Val(Text4, Text) 同样,这句删除!X2 = Val(Text5, Text) 同样,这句也删除!d = b ^ 2 - 4 * a * c If d > 0 Then X1 =...

相似回答
大家正在搜