“求一元二次方程的根”的vb代码怎么写,跪求,谢谢
发布网友
发布时间:2022-12-10 20:17
我来回答
共3个回答
热心网友
时间:2023-07-10 16:37
Private Sub Command1_Click()
a = Val(Text1) '文本框里的值取出来
b = Val(Text2) '分别存到三个变量里,做方程的系数
c = Val(Text3)
If a <> 0 Then '首先判断第一个系数不为0 的情况
dt = b ^ 2 - 4 * a * c '公式中用到的表达式暂存
r = -b / (2 * a)
If dt > 0 Then '这种情况时用公式求方程的根
s = Sqr(dt) / (2 * a)
q1 = "x1=" & Format(r + s, "#.00")
q2 = "x2=" & Format(r - s, "#.00")
Label1.Caption = "方程的根分别是:" & q1 & " 和 " & q2 '输出方程的两个根
ElseIf dt = 0 Then
Label1.Caption = "x1=x2=" & Format(r, "#.00") '两个相等根的输出
Else
Label1.Caption = "无实根" '没有实根
End If
ElseIf b <> 0 Then '一元一次方程的情况处理
y = -c / b
Label1.Caption = "方程只有一个根" & y
Else
Label1.Caption = "方程无意义" '方程不成立的处理
End If
End Sub
热心网友
时间:2023-07-10 16:38
Private Sub Command1_Click()
Dim A As Single,B As Single,C As Single
Dim R1 As Single,R2 As Single
A = Val(Text1.Text)
B = Val(Text2.Text)
C = Val(Text3.Text)
If B ^ 2 - 4 * A * C < 0 Then
MsgBox "该方程无解."
ElseIf B ^ 2 - 4 * A * C = 0 Then
R1 = -B / 2 / A
MsgBox "该方程仅有一个根为" & R1 & "."
Else
R1 = (-B + Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
R2 = (-B - Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
MsgBox "该方程有两个根分别为" & R1 & "和" & R2 & "."
End If
End Sub
来自:求助得到的回答
热心网友
时间:2023-07-10 16:38
Private Sub Command1_Click()
Dim A As Single,B As Single,C As Single
Dim R1 As Single,R2 As Single
A = Val(Text1.Text)
B = Val(Text2.Text)
C = Val(Text3.Text)
If B ^ 2 - 4 * A * C < 0 Then
MsgBox "该方程无解."
ElseIf B ^ 2 - 4 * A * C = 0 Then
R1 = -B / 2 / A
MsgBox "该方程仅有一个根为" & R1 & "."
Else
R1 = (-B + Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
R2 = (-B - Sqr(B ^ 2 - 4 * A * C)) / (2 * A)
MsgBox "该方程有两个根分别为" & R1 & "和" & R2 & "."
End If
End Sub