VB中如何进行复数运算

请举个例子。... 请举个例子。 展开
 我来答
科学普及交流
高粉答主

2015-07-04 · 帮助别人,幸福你我!
科学普及交流
采纳数:83444 获赞数:657674

向TA提问 私信TA
展开全部
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
297366302
2009-04-01 · 超过72用户采纳过TA的回答
知道小有建树答主
回答量:427
采纳率:0%
帮助的人:184万
展开全部
你要算什么,二维数组就可以解决,你自己想下很简单的
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式