| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 581 人关注过本帖
标题:[原创]本人用VB一年,转入VB.NET半年,写了一个计算分数的结构(当然转成类也行 ...
只看楼主 加入收藏
xhwxwk
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2005-8-24
收藏
 问题点数:0 回复次数:0 
[原创]本人用VB一年,转入VB.NET半年,写了一个计算分数的结构(当然转成类也行),请哪

本人用VB一年,转入VB.NET半年,写了一个计算分数的类,请哪位大虾改进一下. Class Fraction #Region "定义类型" '定义分子 Dim Numerator As Long '定义分母 Dim Denominator As Long '比较分数大小时输出结果的枚举 Enum CompareInfo As Byte Smaller = 0 Equals = 1 Bigger = 2 End Enum #End Region #Region "构造函数" '空构造函数 '如果没有此构造函数,形如 Dim f As New Fraction 的语句将出错 Sub New()

End Sub '以一个32位整数构造类型 Sub New(ByVal DecimalInteger As Integer) Numerator = DecimalInteger Denominator = 1 End Sub '以一个64位整数构造类型 Sub New(ByVal DecimalLong As Long) Numerator = DecimalLong Denominator = 1 End Sub '以分子,分母构造类型 Sub New(ByVal Numerator As Long, ByVal Denominator As Long) Try Dim m As Long = Numerator Dim n As Long = Denominator Dim jj As Long = GreatestCommonDivisor(m, n) m = m \ jj n = n \ jj Me.Numerator = m Me.Denominator = n Catch ex As Exception MsgBox(ex.Message) End Try End Sub '以一个单精度浮点数构造类型 Sub New(ByVal DecimalSingle As Single) Try Dim s As String = DecimalSingle.ToString Dim Index As Integer = s.IndexOf(".") + 1 Dim d As String = s.Substring(0, Index - 1) Dim ss As String = s.Substring(Index) Dim i As Integer = ss.Length Dim sss As String = "1" & New String("0", i) Dim l As Long = Int64.Parse(d) Dim k As Long = Int64.Parse(ss) Dim j As Long = Int64.Parse(sss) Dim m As Long = l * j + k Dim n As Long = j Dim jj As Long = GreatestCommonDivisor(m, n) m = m \ jj n = n \ jj Me.Numerator = m Me.Denominator = n Catch ex As Exception MsgBox(ex.Message) End Try End Sub '以一个双精度浮点数构造类型 Sub New(ByVal DecimalDouble As Double) Try Dim s As String = DecimalDouble.ToString Dim Index As Integer = s.IndexOf(".") + 1 Dim d As String = s.Substring(0, Index - 1) Dim ss As String = s.Substring(Index) Dim i As Integer = ss.Length Dim sss As String = "1" & New String("0", i) Dim l As Long = Int64.Parse(d) Dim k As Long = Int64.Parse(ss) Dim j As Long = Int64.Parse(sss) Dim m As Long = l * j + k Dim n As Long = j Dim jj As Long = GreatestCommonDivisor(m, n) m = m \ jj n = n \ jj Me.Numerator = m Me.Denominator = n Catch ex As Exception MsgBox(ex.Message) End Try End Sub #End Region #Region "运算函数" '求最大公约数 Function GreatestCommonDivisor(ByVal Number1 As Long, ByVal Number2 As Long) As Long Try Dim m As Long = Number1 Dim n As Long = Number2 If n > m Then Dim j As Long = m m = n n = j End If Do While Not n = 0 Dim r As Long = m Mod n m = n n = r Loop Return m Catch ex As Exception MsgBox(ex.Message) End Try End Function '求最小公倍数 Function LeaseCommonMultiple(ByVal Number1 As Long, ByVal Number2 As Long) As Long Try Dim m As Long = Number1 Dim n As Long = Number2 If n > m Then Dim j As Long = m m = n n = j End If Do While Not n = 0 Dim r As Long = m Mod n m = n n = r Loop Return (Number1 \ m) * Number2 Catch ex As Exception MsgBox(ex.Message) End Try End Function '求此分数的绝对值 Function Abs() As Fraction Try Dim m As Long = Math.Abs(Me.Numerator) Dim n As Long = Math.Abs(Me.Denominator) Dim Result As New Fraction(m, n) Return Result Catch ex As Exception MsgBox(ex.Message) End Try End Function '求指定分数的绝对值 Function Abs(ByVal MyFraction As Fraction) As Fraction Try Dim m As Long = Math.Abs(MyFraction.Numerator) Dim n As Long = Math.Abs(MyFraction.Denominator) Dim Result As New Fraction(m, n) Return Result Catch ex As Exception MsgBox(ex.Message) End Try End Function '将此分数转换成它的倒数 Sub Reciprocal() Try Dim j As Long = Me.Denominator Me.Denominator = Me.Numerator Me.Numerator = j Catch ex As Exception MsgBox(ex.Message) End Try End Sub '求指定分数的倒数 Function Reciprocal(ByVal MyFraction As Fraction) As Fraction Try Dim Result As New Fraction(MyFraction.Denominator, MyFraction.Numerator) Return Result Catch ex As Exception MsgBox(ex.Message) End Try End Function '将此分数约分 Sub ReductionFraction() Try Dim m As Long = Me.Numerator Dim n As Long = Me.Denominator Dim j As Long = GreatestCommonDivisor(m, n) m = m \ j n = n \ j Me.Numerator = m Me.Denominator = n Catch ex As Exception MsgBox(ex.Message) End Try End Sub '将指定分数约分 Function ReductionFraction(ByVal MyFraction As Fraction) As Fraction Try Dim m As Long = MyFraction.Numerator Dim n As Long = MyFraction.Denominator Dim j As Long = GreatestCommonDivisor(m, n) m = m \ j n = n \ j Dim Result As New Fraction(m, n) Return Result Catch ex As Exception MsgBox(ex.Message) End Try End Function '比较分数 Function Compare(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As CompareInfo Try Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = LeaseCommonMultiple(n1, n2) m1 = (j \ n1) * m1 m2 = (j \ n2) * m2 If m1 = m2 Then Return CompareInfo.Equals ElseIf m1 > m2 Then Return CompareInfo.Bigger Else Return CompareInfo.Smaller End If Catch ex As Exception MsgBox(ex.Message) End Try End Function '判断两个分数是否相等 Overloads Function Equals(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As Boolean Try Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = LeaseCommonMultiple(n1, n2) m1 = (j \ n1) * m1 m2 = (j \ n2) * m2 If m1 = m2 Then Return True Else Return False End If Catch ex As Exception MsgBox(ex.Message) End Try End Function '通分 Sub ReductionOfFractionToACommonDenominator(ByRef Number1 As Fraction, ByRef Number2 As Fraction) Try Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = LeaseCommonMultiple(n1, n2) m1 = (j \ n1) * m1 m2 = (j \ n2) * m2 Number1.Numerator = m1 : Number2.Numerator = m2 Number1.Denominator = j : Number2.Denominator = j Catch ex As Exception MsgBox(ex.Message) End Try End Sub '加 Function Plus(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As Fraction Try Number1.ReductionFraction() : Number2.ReductionFraction() Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = LeaseCommonMultiple(n1, n2) m1 = (j \ n1) * m1 m2 = (j \ n2) * m2 Dim m As Long = m1 + m2 Dim MyFraction As New Fraction(m, j) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try End Function '减 Function Minus(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As Fraction Try Number1.ReductionFraction() : Number2.ReductionFraction() Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = LeaseCommonMultiple(n1, n2) m1 = (j \ n1) * m1 m2 = (j \ n2) * m2 Dim m As Long = m1 - m2 Dim MyFraction As New Fraction(m, j) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try End Function '乘 Function Multiply(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As Fraction Try Number1.ReductionFraction() : Number2.ReductionFraction() Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = m1 * m2 Dim k As Long = n1 * n2 Dim MyFraction As New Fraction(j, k) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try End Function '除 Function Divide(ByVal Number1 As Fraction, ByVal Number2 As Fraction) As Fraction Try Number1.ReductionFraction() : Number2.ReductionFraction() If Number2.Numerator = 0 Then Exit Function Dim m1 As Long, m2 As Long, n1 As Long, n2 As Long m1 = Number1.Numerator : n1 = Number1.Denominator m2 = Number2.Numerator : n2 = Number2.Denominator Dim j As Long = m1 * n2 Dim k As Long = n1 * m2 Dim MyFraction As New Fraction(j, k) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try End Function '乘方 Function Power(ByVal Number As Fraction, ByVal PowerTimes As Integer) As Fraction Number.ReductionFraction() If PowerTimes > 0 Then Try Dim m As Long = Number.Numerator ^ PowerTimes Dim n As Long = Number.Denominator ^ PowerTimes Dim MyFraction As New Fraction(m, n) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try Exit Function ElseIf PowerTimes = 0 Then Try Dim MyFraction As New Fraction(1, 1) Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try Exit Function Else Try Dim MyPowerTimes As Long = Math.Abs(PowerTimes) Dim m As Long = Number.Numerator ^ MyPowerTimes Dim n As Long = Number.Denominator ^ MyPowerTimes Dim MyFraction As New Fraction(n, m) MyFraction.ReductionFraction() Return MyFraction Catch ex As Exception MsgBox(ex.Message) End Try Exit Function End If End Function #End Region #Region "转换函数" '将此分数转换成一个64位整数(近似值) Function ToLongInteger() As Long Try Return Math.Round(Me.Numerator / Me.Denominator) Catch ex As Exception MsgBox(ex.Message) End Try End Function '将此分数转换成一个双精度浮点数(近似值) Function ToDouble() As Double Try Dim j As Long = Me.Numerator \ Me.Denominator Dim k As Long = Me.Numerator Mod Me.Denominator Return (j + k / Me.Denominator) Catch ex As Exception MsgBox(ex.Message) End Try End Function '将此分数转换成字符串(形同 5/7) Function FractionToString() As String Try Return Me.Numerator.ToString & "/" & Me.Denominator.ToString Catch ex As Exception MsgBox(ex.Message) End Try End Function '将此分数转换成字符串(形同 5/7)分隔符由传入参数 FractionLine 指定 (如 FractionLine 指定为 \,则传出 5\7) Function FractionToString(ByVal FractionLine As String) As String Try Return Me.Numerator.ToString & FractionLine & Me.Denominator.ToString Catch ex As Exception MsgBox(ex.Message) End Try End Function #End Region End Class

[此贴子已经被作者于2005-8-25 9:40:23编辑过]

2005-08-24 16:02
快速回复:[原创]本人用VB一年,转入VB.NET半年,写了一个计算分数的结构(当然转成 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026005 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved