具有复杂根的二次方程

时间:2015-10-02 20:46:50

标签: vb.net equation quadratic

我只是在学习visual basic,并且一直在尝试编写一个代码来解决复数作为根的二次方程。任何指向正确方向的人都会受到赞赏。

Public Class Form1
    Dim a, b, c As Integer
    Dim x1, x2 As Double

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles eqnRT1.TextChanged

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtA.Text = ""
        txtB.Text = ""
        txtC.Text = ""
        eqnRT1.Text = ""
        eqnRT2.Text = ""
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        Dim a,b,c as integer
        a = txtA.Text 
        b = txtB.Text 
        c = txtC.Text 

        Dim x1 = (-b + math.Sqrt(b * b - 4 * a * c)) / (2 * a)
        Dim x2 = (-b - math.Sqrt(b * b - 4 * a * c)) / (2 * a)
        eqnRT1.Text = Str(x1)
        eqnRT2.Text = Str(x2)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

互联网上有很多答案可以做到这一点......

    Sub Main()

    Dim a, b, c As Single
    Console.WriteLine("Write coefficient 'a'")
    a = Console.ReadLine
    Console.WriteLine("Write coefficient 'b'")
    b = Console.ReadLine
    Console.WriteLine("Write coefficient 'c'")
    c = Console.ReadLine

    Dim d As Integer = b ^ 2 - 4 * a * c
    If d >= 0 Then
        If d = 0 Then
            Console.WriteLine("Roots are real and equal")
        Else
            Console.WriteLine("Roots are real and different")
        End If

        Console.Write("Roots are: ")
        Console.Write((-b + d ^ 0.5) / (2 * a) & " , ")
        Console.WriteLine((-b - d ^ 0.5) / (2 * a))

    Else
        Console.WriteLine("Roots are complex")
        Console.Write("Roots are: ")
        Console.Write(-b / (2 * a) & "+" & (d * -1) ^ 0.5 / (2 * a) & "i")
        Console.Write(" , ")
        Console.WriteLine(-b / (2 * a) & "-" & (d * -1) ^ 0.5 / (2 * a) & "i")

    End If
    Console.ReadLine()

End Sub

答案 1 :(得分:0)

此代码似乎工作正常。

Public Class Form1
    Dim a, b, c As Integer
    Dim x1, x2 As Double

    Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click
    End Sub

    Private Sub TextBox4_TextChanged(sender As Object, e As EventArgs) Handles eqnRT1.TextChanged
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtA.Text = ""
        txtB.Text = ""
        txtC.Text = ""
        eqnRT1.Text = ""
        eqnRT2.Text = ""
    End Sub

    Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        Dim a = New System.Numerics.Complex(Double.Parse(txtA.Text), 0.0)
        Dim b = New System.Numerics.Complex(Double.Parse(txtB.Text), 0.0)
        Dim c = New System.Numerics.Complex(Double.Parse(txtC.Text), 0.0)
        Dim x1 = (-b + System.Numerics.Complex.Sqrt(b * b - 4 * a * c)) / (2 * a)
        Dim x2 = (-b - System.Numerics.Complex.Sqrt(b * b - 4 * a * c)) / (2 * a)
        eqnRT1.Text = String.Format("{0:0.00}+{1:0.00}i", x1.Real, x1.Imaginary)
        eqnRT2.Text = String.Format("{0:0.00}+{1:0.00}i", x2.Real, x2.Imaginary)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub
End Class