Imports System.MathImports System.Text.RegularExpressionsPublic 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 Complex) 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 As 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.ImaginaryParty * Complex2.ImaginaryParty Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex1.ImaginaryParty * Complex2.RealPart Return Me End Function Public Function Devide(ByVal Complex1 As Complex, ByVal Complex2 As Complex) 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) Then Dim temp As Single = Complex2.ImaginaryParty / Complex2.RealPart Real = (Complex1.RealPart + Complex1.ImaginaryParty * temp) / (Complex2.RealPart + Complex2.ImaginaryParty * temp) Imaginary = (Complex1.ImaginaryParty - Complex1.RealPart * temp) / (Complex2.RealPart + Complex2.ImaginaryParty * temp) Return Me Else Dim temp As Single = Complex2.RealPart / Complex2.ImaginaryParty Real = (Complex1.RealPart * temp + Complex1.ImaginaryParty) / (Complex2.RealPart * temp + Complex2.ImaginaryParty) Imaginary = (Complex1.ImaginaryParty * temp - Complex1.RealPart) / (Complex2.RealPart * temp + Complex2.ImaginaryParty) Return Me End If Real = Complex1.RealPart * Complex2.RealPart - Complex1.ImaginaryParty * Complex2.ImaginaryParty Imaginary = Complex1.RealPart * Complex2.ImaginaryParty + Complex1.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("num1").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 System.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(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString) Case "-" TextBox1.AppendText(MyComplex.Substract(MyComplex.StringToCom(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString) Case "*" TextBox1.AppendText(MyComplex.Multiple(MyComplex.StringToCom(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString) Case "/" TextBox1.AppendText(MyComplex.Devide(MyComplex.StringToCom(ComboBox1.Text), MyComplex.StringToCom(ComboBox2.Text)).ToString) Case "^" 'wait Case "开方" 'wait Case "清空" 'wait End Select TextBox1.AppendText(vbCrLf) End If End SubEnd Class--我很懒,我承认妈妈说:早起的鸟儿有虫吃我说:那早起的虫儿不就被鸟吃吗?那我到底是虫儿还是鸟儿?我很笨,我也承认. ※ 来源:·北大未名站 bbs.pku.edu.cn·[FROM: 162.105.27.126]附件列表 (使用WWW方式可以查看附件):1: 复数相关.rar (94.250千字节)附件1: 复数相关.rar

评论