正文

复数运算之四2008-11-14 20:55:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/39395.html

分享到:

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


--
我很懒,我承认
妈妈说:早起的鸟儿有虫吃
我说:那早起的虫儿不就被鸟吃吗?

我到底是虫儿还是鸟儿?
我很笨,我也承认.

※ 来源:·北大未名站 bbs.pku.edu.cn·[FROM: 162.105.27.126]

附件列表 (使用WWW方式可以查看附件):
1: 复数相关.rar (94.250千字节)

附件1: 复数相关.rar

阅读(3195) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册