VB.Net Tutorial/Data Type/Complex Number
Версия от 16:40, 26 мая 2010; (обсуждение)
Complex
public class Test
public Shared Sub Main
Dim X, Y As Complex
X = New Complex(1, 4)
Y = New Complex(2, 3)
Console.WriteLine((X + Y).ToString)
Console.WriteLine((X - Y).ToString)
Console.WriteLine((X * Y).ToString)
Console.WriteLine((X = Y).ToString)
Console.WriteLine((X <> Y).ToString)
Console.WriteLine((-X).ToString)
Dim abs_x As Double = CDbl(X)
Console.WriteLine(abs_x.ToString)
End Sub
End class
Public Class Complex
Public Re As Double
Public Im As Double
Public Sub New()
End Sub
Public Sub New(ByVal real_part As Double, ByVal imaginary_part As Double)
Re = real_part
Im = imaginary_part
End Sub
Public Overrides Function ToString() As String
Return Re.ToString & " + " & Im.ToString & "i"
End Function
Public Shared Operator *(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
Return New Complex( _
c1.Re * c2.Re - c1.Im * c2.Im, _
c1.Re * c2.Im + c1.Im * c2.Re)
End Operator
Public Shared Operator +(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
Return New Complex( c1.Re + c2.Re,c1.Im + c2.Im)
End Operator
Public Shared Operator -(ByVal c1 As Complex, ByVal c2 As Complex) As Complex
Return New Complex(c1.Re - c2.Re, c1.Im - c2.Im)
End Operator
Public Shared Operator =(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
Return (c1.Re = c2.Re) AndAlso (c1.Im = c2.Im)
End Operator
Public Shared Operator <>(ByVal c1 As Complex, ByVal c2 As Complex) As Boolean
Return (c1.Re <> c2.Re) OrElse (c1.Im <> c2.Im)
End Operator
Public Shared Operator -(ByVal c1 As Complex) As Complex
Return New Complex(c1.Im, c1.Re)
End Operator
Public Shared Narrowing Operator CType(ByVal c1 As Complex) As Double
Return System.Math.Sqrt(c1.Re * c1.Re + c1.Im * c1.Im)
End Operator
End Class
3 + 7i -1 + 1i -10 + 11i False True 4 + 1i 4.12310562561766
Complex Number
Public Class Tester
Public Shared Sub Main
Dim result As New System.Text.StringBuilder
Dim a As ComplexNumber
Dim b As ComplexNumber
Dim c As ComplexNumber
a = New ComplexNumber(3, 4)
b = New ComplexNumber(5, -2)
c = a + b
result.AppendLine("Complex Numbers")
result.AppendLine("a = " & a.ToString())
result.AppendLine("b = " & b.ToString())
" ----- Addition.
c = a + b
result.AppendLine("a + b = " & c.ToString())
" ----- Subtraction.
c = a - b
result.AppendLine("a - b = " & c.ToString())
" ----- Multiplication.
c = a * b
result.AppendLine("a * b = " & c.ToString())
" ----- Division.
c = a / b
result.AppendLine("a / b = " & c.ToString())
" ----- Addition as assignment.
a += b
result.AppendLine("a += b ... a = " & a.ToString())
Console.WriteLine(result.ToString())
End Sub
End Class
Structure ComplexNumber
Public Real As Double
Public Imaginary As Double
Public Sub New(ByVal realPart As Double, ByVal imaginaryPart As Double)
Me.Real = realPart
Me.Imaginary = imaginaryPart
End Sub
Public Sub New(ByVal sourceNumber As ComplexNumber)
Me.Real = sourceNumber.Real
Me.Imaginary = sourceNumber.Imaginary
End Sub
Public Overrides Function ToString() As String
Return Real & "+" & Imaginary & "i"
End Function
Public Shared Operator +(ByVal a As ComplexNumber, _
ByVal b As ComplexNumber) As ComplexNumber
Return New ComplexNumber(a.Real + b.Real, a.Imaginary + b.Imaginary)
End Operator
Public Shared Operator -(ByVal a As ComplexNumber, _
ByVal b As ComplexNumber) As ComplexNumber
Return New ComplexNumber(a.Real - b.Real, a.Imaginary - b.Imaginary)
End Operator
Public Shared Operator *(ByVal a As ComplexNumber, _
ByVal b As ComplexNumber) As ComplexNumber
Return New ComplexNumber(a.Real * b.Real - a.Imaginary * b.Imaginary, _
a.Real * b.Imaginary + a.Imaginary * b.Real)
End Operator
Public Shared Operator /(ByVal a As ComplexNumber, _
ByVal b As ComplexNumber) As ComplexNumber
Return a * Reciprocal(b)
End Operator
Public Shared Function Reciprocal(ByVal a As ComplexNumber) As ComplexNumber
Dim divisor As Double
divisor = a.Real * a.Real + a.Imaginary * a.Imaginary
If (divisor = 0.0#) Then Throw New DivideByZeroException
Return New ComplexNumber(a.Real / divisor, -a.Imaginary / divisor)
End Function
End Structure
Complex Numbers a = 3+4i b = 5+-2i a + b = 8+2i a - b = -2+6i a * b = 23+14i a / b = 0.241379310344828+0.896551724137931i a += b ... a = 8+2i