怎样用vb求一元两次方程ax^2+bx+c=0的根?

1个回答

  • Private Sub Command1_Click()Dim a As Single, b As Single, c As SingleDim d As Single, x1 As Single, x2 As SingleDim r As Single, p As Singlea = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)If a = 0 ThenText4.Text = "一元二次方程的系数A不能为0"Text5.Text = "一元二次方程的系数A不能为0"Elsed = b ^ 2 - 4 * a * cr = -b / (2 * a)If d = 0 ThenText4.Text = rText5.Text = rElseIf d > 0 Thenx1 = (-b + Sqr(d)) / (2 * a)x2 = (-b - Sqr(d)) / (2 * a)Text4.Text = x1Text5.Text = x2ElseIf d < 0 Thenp = Sqr(-d) / 2 * aText4.Text = r "+" p "i"Text5.Text = r "-" p "i"End IfEnd IfEnd Sub