2个回答
展开全部
VB中复数运算:
Imports System.Math
Imports System.Text.RegularExpressions
Public Class Form1
#Region "码型亮复数结构"
Structure Complex
Private Real As Single '设置复数的实数部分
Private TempString As String
Property RealPart() As Single
Get
Return Real
End Get
Set(ByVal value As Single)
Real = value
End Set
End Property
Private Imaginary As Single '设置复数的虚数部分
Property ImaginaryParty() As Single
Get
Return Imaginary
End Get
Set(ByVal value As Single)
Imaginary = value
End Set
End Property
Shadows ReadOnly Property ToString() As String '返回复数的标准形式,如
果没有指定值,迟宽返回0
Get
Dim TempReal As String = Real.ToString
Dim TempIma As String = Imaginary.ToString + "i"
If Real = 0 Then TempReal = ""
If Imaginary = 0 Then
TempIma = ""
ElseIf Imaginary = 1 AndAlso Real <> 0 Then
TempIma = TempIma.Replace("1", "+"租汪)
ElseIf Imaginary = -1 OrElse (Imaginary = 1 AndAlso Real = 0)
Then
TempIma = TempIma.Replace("1", "")
End If
If Real = 0 AndAlso Imaginary = 0 Then Return "0"
Return TempReal + TempIma
End Get
End Property
Public Function Add(ByVal Complex1 As Complex, ByVal Complex2 As Comp
lex) As Complex
Real = Complex1.RealPart + Complex2.RealPart
Imaginary = Complex1.ImaginaryParty + Complex2.ImaginaryParty
Return Me
End Function
Public Function Substract(ByVal Complex1 As Complex, ByVal Complex2 A
s Complex) As Complex
Real = Complex1.RealPart - Complex2.RealPart
Imaginary = Complex1.ImaginaryParty - Complex2.ImaginaryParty
Return Me
End Function
Public Function Multiple(ByVal Complex1 As Complex, ByVal Complex2 As
Complex) As Complex
Real = Complex1.RealPart * Complex2.RealPart - Complex1.Imaginary
Party * Complex2.ImaginaryParty
Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex
1.ImaginaryParty * Complex2.RealPart
Return Me
End Function
Public Function Devide(ByVal Complex1 As Complex, ByVal Complex2 As C
omplex) As Complex
If Complex2.RealPart = 0 Then
Real = Complex1.ImaginaryParty / Complex2.ImaginaryParty
Imaginary = -Complex1.RealPart / Complex2.ImaginaryParty
Return Me
ElseIf Complex2.ImaginaryParty = 0 Then
Real = Complex1.RealPart / Complex2.RealPart
Imaginary = Complex1.ImaginaryParty / Complex2.RealPart
Return Me
ElseIf Abs(Complex2.RealPart) >= Abs(Complex2.ImaginaryParty) The
n
Dim temp As Single = Complex2.ImaginaryParty / Complex2.RealP
art
Real = (Complex1.RealPart + Complex1.ImaginaryParty * temp) /
(Complex2.RealPart + Complex2.ImaginaryParty * temp)
Imaginary = (Complex1.ImaginaryParty - Complex1.RealPart * te
mp) / (Complex2.RealPart + Complex2.ImaginaryParty * temp)
Return Me
Else
Dim temp As Single = Complex2.RealPart / Complex2.ImaginaryPa
rty
Real = (Complex1.RealPart * temp + Complex1.ImaginaryParty) /
(Complex2.RealPart * temp + Complex2.ImaginaryParty)
Imaginary = (Complex1.ImaginaryParty * temp - Complex1.RealPa
rt) / (Complex2.RealPart * temp + Complex2.ImaginaryParty)
Return Me
End If
Real = Complex1.RealPart * Complex2.RealPart - Complex1.Imaginary
Party * Complex2.ImaginaryParty
Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex
1.ImaginaryParty * Complex2.RealPart
Return Me
End Function
Default ReadOnly Property StringToCom(ByVal inputstring As String) As
Complex
Get
TempString = inputstring
Return ChangeStringToComplex()
End Get
End Property
Private Function ChangeStringToComplex() As Complex
Const temp As String = "(\s*(?<num1>[+-]?(\d+\.?\d*|\d*\.?\d+))\s
*)?(\s*(?<num2>[+-]?(\d+\.?\d*|\d*\.?\d+|\s*))(?<ima>[iI])\s*)?"
Dim MyRegex As New Regex(temp)
Dim TempComplex As Complex
Try
If MyRegex.IsMatch(TempString) Then
Dim MyMatch As Match = MyRegex.Match(TempString)
If MyMatch.Groups("num1").Length > 0 Then
TempComplex.RealPart = Single.Parse(MyMatch.Groups("n
um1").Value, Globalization.NumberStyles.Any)
End If
If MyMatch.Groups("ima").Length > 0 Then
If MyMatch.Groups("num2").Value = "" Then '防止1-i的形
式,i,-i
TempComplex.ImaginaryParty = 1
ElseIf MyMatch.Groups("num2").Value = "-" Then
TempComplex.ImaginaryParty = -1
Else
TempComplex.ImaginaryParty = Single.Parse(MyMatch
.Groups("num2").Value, Globalization.NumberStyles.Any)
End If
End If
End If
Catch ex As Exception
Debug.Print(ex.Message)
'Your need add necessary code here!
End Try
Return TempComplex
End Function
End Structure
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste
m.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, Button7
.Click
If ComboBox1.Text.Length = 0 OrElse ComboBox2.Text.Length = 0 Then
TextBox1.AppendText(ComboBox1.Text + ComboBox2.Text)
Else
Dim MyComplex As Complex
Select Case CType(sender, Button).Text
Case "+"
TextBox1.AppendText(MyComplex.Add(MyComplex.StringToCom(C
omboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "-"
TextBox1.AppendText(MyComplex.Substract(MyComplex.StringT
oCom(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "*"
TextBox1.AppendText(MyComplex.Multiple(MyComplex.StringTo
Com(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "/"
TextBox1.AppendText(MyComplex.Devide(MyComplex.StringToCo
m(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "^"
'wait
Case "开方"
'wait
Case "清空"
'wait
End Select
TextBox1.AppendText(vbCrLf)
End If
End Sub
End Class
Imports System.Math
Imports System.Text.RegularExpressions
Public Class Form1
#Region "码型亮复数结构"
Structure Complex
Private Real As Single '设置复数的实数部分
Private TempString As String
Property RealPart() As Single
Get
Return Real
End Get
Set(ByVal value As Single)
Real = value
End Set
End Property
Private Imaginary As Single '设置复数的虚数部分
Property ImaginaryParty() As Single
Get
Return Imaginary
End Get
Set(ByVal value As Single)
Imaginary = value
End Set
End Property
Shadows ReadOnly Property ToString() As String '返回复数的标准形式,如
果没有指定值,迟宽返回0
Get
Dim TempReal As String = Real.ToString
Dim TempIma As String = Imaginary.ToString + "i"
If Real = 0 Then TempReal = ""
If Imaginary = 0 Then
TempIma = ""
ElseIf Imaginary = 1 AndAlso Real <> 0 Then
TempIma = TempIma.Replace("1", "+"租汪)
ElseIf Imaginary = -1 OrElse (Imaginary = 1 AndAlso Real = 0)
Then
TempIma = TempIma.Replace("1", "")
End If
If Real = 0 AndAlso Imaginary = 0 Then Return "0"
Return TempReal + TempIma
End Get
End Property
Public Function Add(ByVal Complex1 As Complex, ByVal Complex2 As Comp
lex) As Complex
Real = Complex1.RealPart + Complex2.RealPart
Imaginary = Complex1.ImaginaryParty + Complex2.ImaginaryParty
Return Me
End Function
Public Function Substract(ByVal Complex1 As Complex, ByVal Complex2 A
s Complex) As Complex
Real = Complex1.RealPart - Complex2.RealPart
Imaginary = Complex1.ImaginaryParty - Complex2.ImaginaryParty
Return Me
End Function
Public Function Multiple(ByVal Complex1 As Complex, ByVal Complex2 As
Complex) As Complex
Real = Complex1.RealPart * Complex2.RealPart - Complex1.Imaginary
Party * Complex2.ImaginaryParty
Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex
1.ImaginaryParty * Complex2.RealPart
Return Me
End Function
Public Function Devide(ByVal Complex1 As Complex, ByVal Complex2 As C
omplex) As Complex
If Complex2.RealPart = 0 Then
Real = Complex1.ImaginaryParty / Complex2.ImaginaryParty
Imaginary = -Complex1.RealPart / Complex2.ImaginaryParty
Return Me
ElseIf Complex2.ImaginaryParty = 0 Then
Real = Complex1.RealPart / Complex2.RealPart
Imaginary = Complex1.ImaginaryParty / Complex2.RealPart
Return Me
ElseIf Abs(Complex2.RealPart) >= Abs(Complex2.ImaginaryParty) The
n
Dim temp As Single = Complex2.ImaginaryParty / Complex2.RealP
art
Real = (Complex1.RealPart + Complex1.ImaginaryParty * temp) /
(Complex2.RealPart + Complex2.ImaginaryParty * temp)
Imaginary = (Complex1.ImaginaryParty - Complex1.RealPart * te
mp) / (Complex2.RealPart + Complex2.ImaginaryParty * temp)
Return Me
Else
Dim temp As Single = Complex2.RealPart / Complex2.ImaginaryPa
rty
Real = (Complex1.RealPart * temp + Complex1.ImaginaryParty) /
(Complex2.RealPart * temp + Complex2.ImaginaryParty)
Imaginary = (Complex1.ImaginaryParty * temp - Complex1.RealPa
rt) / (Complex2.RealPart * temp + Complex2.ImaginaryParty)
Return Me
End If
Real = Complex1.RealPart * Complex2.RealPart - Complex1.Imaginary
Party * Complex2.ImaginaryParty
Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex
1.ImaginaryParty * Complex2.RealPart
Return Me
End Function
Default ReadOnly Property StringToCom(ByVal inputstring As String) As
Complex
Get
TempString = inputstring
Return ChangeStringToComplex()
End Get
End Property
Private Function ChangeStringToComplex() As Complex
Const temp As String = "(\s*(?<num1>[+-]?(\d+\.?\d*|\d*\.?\d+))\s
*)?(\s*(?<num2>[+-]?(\d+\.?\d*|\d*\.?\d+|\s*))(?<ima>[iI])\s*)?"
Dim MyRegex As New Regex(temp)
Dim TempComplex As Complex
Try
If MyRegex.IsMatch(TempString) Then
Dim MyMatch As Match = MyRegex.Match(TempString)
If MyMatch.Groups("num1").Length > 0 Then
TempComplex.RealPart = Single.Parse(MyMatch.Groups("n
um1").Value, Globalization.NumberStyles.Any)
End If
If MyMatch.Groups("ima").Length > 0 Then
If MyMatch.Groups("num2").Value = "" Then '防止1-i的形
式,i,-i
TempComplex.ImaginaryParty = 1
ElseIf MyMatch.Groups("num2").Value = "-" Then
TempComplex.ImaginaryParty = -1
Else
TempComplex.ImaginaryParty = Single.Parse(MyMatch
.Groups("num2").Value, Globalization.NumberStyles.Any)
End If
End If
End If
Catch ex As Exception
Debug.Print(ex.Message)
'Your need add necessary code here!
End Try
Return TempComplex
End Function
End Structure
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As Syste
m.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, _
Button4.Click, Button5.Click, Button6.Click, Button7
.Click
If ComboBox1.Text.Length = 0 OrElse ComboBox2.Text.Length = 0 Then
TextBox1.AppendText(ComboBox1.Text + ComboBox2.Text)
Else
Dim MyComplex As Complex
Select Case CType(sender, Button).Text
Case "+"
TextBox1.AppendText(MyComplex.Add(MyComplex.StringToCom(C
omboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "-"
TextBox1.AppendText(MyComplex.Substract(MyComplex.StringT
oCom(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "*"
TextBox1.AppendText(MyComplex.Multiple(MyComplex.StringTo
Com(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "/"
TextBox1.AppendText(MyComplex.Devide(MyComplex.StringToCo
m(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString)
Case "^"
'wait
Case "开方"
'wait
Case "清空"
'wait
End Select
TextBox1.AppendText(vbCrLf)
End If
End Sub
End Class
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询