Module Program Public Class Complex Public Property RPart As Double ' Real part Public Property IPart As Double ' Imaginary part ' Constructor to create a complex number Public Sub New(r As Double, i As Double) RPart = r IPart = i End Sub ' Method to convert complex number to string representation Public Overrides Function ToString() As String If IPart < 0 Then Return RPart.ToString() & IPart.ToString() & "i" Else Return RPart.ToString() & "+" & IPart.ToString() & "i" End If End Function ' Static method to parse string representation into a complex number Public Shared Function StrCpx(s As String) As Complex Dim i As Integer = s.IndexOfAny(New Char() {"+"c, "-"c}, 1) ' Check if we found a valid index If i < 0 Then ' If no '+' or '-' was found after the first character, return real part only Return New Complex(Double.Parse(s), 0) End If ' Get the real part and imaginary part correctly Dim realPart As Double = Double.Parse(s.Substring(0, i)) Dim imagPartStr As String = s.Substring(i) ' Handle cases for the imaginary part, ensuring we strip the "i" Dim imagPart As Double = Double.Parse(imagPartStr.Replace("i", "").Trim()) ' Returning the constructed complex number Return New Complex(realPart, imagPart) End Function ' Method to add two complex numbers Public Shared Function CpxAdd(x1 As Complex, x2 As Complex) As Complex Return New Complex(x1.RPart + x2.RPart, x1.IPart + x2.IPart) End Function ' Method to subtract second complex number from the first Public Shared Function CpxSub(x1 As Complex, x2 As Complex) As Complex Return New Complex(x1.RPart - x2.RPart, x1.IPart - x2.IPart) End Function ' Method to multiply two complex numbers Public Shared Function CpxMul(x1 As Complex, x2 As Complex) As Complex Dim r As Double = x1.RPart * x2.RPart - x1.IPart * x2.IPart Dim i As Double = x1.RPart * x2.IPart + x1.IPart * x2.RPart Return New Complex(r, i) End Function ' Method to divide first complex number by the second Public Shared Function CpxDiv(x1 As Complex, x2 As Complex) As Complex Dim denom As Double = x2.RPart ^ 2 + x2.IPart ^ 2 Dim r As Double = (x1.RPart * x2.RPart + x1.IPart * x2.IPart) / denom Dim i As Double = (x1.IPart * x2.RPart - x1.RPart * x2.IPart) / denom Return New Complex(r, i) End Function End Class Sub Main() ' Create some complex numbers Dim c1 As New Complex(3, 4) ' 3 + 4i Dim c2 As New Complex(1, -2) ' 1 - 2i ' Display the complex numbers Console.WriteLine("c1: " & c1.ToString()) Console.WriteLine("c2: " & c2.ToString()) ' Addition Dim sum As Complex = Complex.CpxAdd(c1, c2) Console.WriteLine("c1 + c2: " & sum.ToString()) ' Subtraction Dim difference As Complex = Complex.CpxSub(c1, c2) Console.WriteLine("c1 - c2: " & difference.ToString()) ' Multiplication Dim product As Complex = Complex.CpxMul(c1, c2) Console.WriteLine("c1 * c2: " & product.ToString()) ' Division Dim quotient As Complex = Complex.CpxDiv(c1, c2) Console.WriteLine("c1 / c2: " & quotient.ToString()) ' Convert string to complex Dim c3 As Complex = Complex.StrCpx("5-6i") Console.WriteLine("c3 (from string): " & c3.ToString()) ' Wait for a key press before closing Console.WriteLine("Press any key to continue...") Console.ReadKey() ' Wait for a key press End Sub End Module