vb输入一元二次方程ax^2+bx+c=0的系数a,b,c,计算并输出一元二次方程的两个实根x1,x2.
我的代码是这个PrivateSubCommand1_Click()DimaAsSingle,bAsSingle,cAsSingleDimdAsSingle,x1AsSin...
我的代码是这个
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
Dim d As Single, x1 As Single, x2 As Single
Dim r As Single, p As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
If a = 0 Then
Text4.Text = "一元二次方程的系数A不能为0"
Text5.Text = "一元二次方程的系数A不能为0"
Else
d = b ^ 2 - 4 * a * c
r = -b / (2 * a)
If d = 0 Then
Label3.Caption = r
Label5.Caption = r
ElseIf d > 0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Label3.Caption = x1
Label5.Caption = x2
ElseIf d < 0 Then
p = Sqr(-d) / 2 * a
Label3.Caption = r & "+" & p & "i"
Label5.Caption = r & "-" & p & "i"
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label3.Caption = ""
Label5.Caption = ""
End Sub
x2还正常,但为什么x1显示的是-.3596118?!前面的零为什么不显示啊 展开
Private Sub Command1_Click()
Dim a As Single, b As Single, c As Single
Dim d As Single, x1 As Single, x2 As Single
Dim r As Single, p As Single
a = Val(Text1.Text)
b = Val(Text2.Text)
c = Val(Text3.Text)
If a = 0 Then
Text4.Text = "一元二次方程的系数A不能为0"
Text5.Text = "一元二次方程的系数A不能为0"
Else
d = b ^ 2 - 4 * a * c
r = -b / (2 * a)
If d = 0 Then
Label3.Caption = r
Label5.Caption = r
ElseIf d > 0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Label3.Caption = x1
Label5.Caption = x2
ElseIf d < 0 Then
p = Sqr(-d) / 2 * a
Label3.Caption = r & "+" & p & "i"
Label5.Caption = r & "-" & p & "i"
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Label3.Caption = ""
Label5.Caption = ""
End Sub
x2还正常,但为什么x1显示的是-.3596118?!前面的零为什么不显示啊 展开
2个回答
展开全部
VB纯小数的输出,默认的都是不输出整数0的。
0.12就输出为.12
-0.12就输出 -.12
如要输出的话,要自己编写代码:
Private Sub Command1_Click()
x = Val(InputBox(""))
If x > 0 And x < 1 Then
Print "0" & x
Else
If x < 0 And x > -1 Then
Print "-0" & -x
Else
Print x
End If
End If
End Sub
0.12就输出为.12
-0.12就输出 -.12
如要输出的话,要自己编写代码:
Private Sub Command1_Click()
x = Val(InputBox(""))
If x > 0 And x < 1 Then
Print "0" & x
Else
If x < 0 And x > -1 Then
Print "-0" & -x
Else
Print x
End If
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询