早教吧 育儿知识 作业答案 考试题库 百科 知识分享

用VB函数编写ax^2+bx+c=0的根,设b^2-4ac>0前提是b^2-4ac>0

题目详情
用VB函数编写ax ^2+bx+c=0的根,设b ^2-4ac>0
前提是b ^2-4ac>0
▼优质解答
答案和解析
Private Sub Command1_Click()
Dim a As Single,b As Single,c As Single,x1 As Single,x2 As Single,delta As Single,sqrdelta As Single
Do
a = Val(InputBox("请输入二次项系数"))
If a = 0 Then
MsgBox "二次想系数不得为零!"
Else
Exit Do
End If
Loop
b = Val(InputBox("请输入一次项系数"))
c = Val(InputBox("请输入常数项"))
delta = b * b - 4 * a * c
If delta < 0 Then
Print "判别式小于零,有两个虚根:"
sqrdelta = Sqr(-delta)
Print "X1="; -b / 2 / a; "+"; sqrdelta; "i"
Print "X2="; -b / 2 / a; "-"; sqrdelta; "i"
ElseIf delta = 0 Then
Print "判别式等于零,有一个实根:"
Print "X="; -b / 2 / a
Else
Print "判别式大于零,有两个实根:"
sqrdelta = Sqr(-delta)
Print "X1="; -b / 2 / a + sqrdelta
Print "X2="; -b / 2 / a - sqrdelta
End If
End Sub
虚根、单根、双根都考虑到了.用就成了.