以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 求助 分数计算 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=118837) |
-- 作者:jackyfashion -- 发布时间:2018/5/11 21:23:00 -- 求助 分数计算 老师: 您好! 请问点样设置可以像 Excel 的分数计算,比如 : 第三列 = 第一列+第二列 的分数计算 请老师示教!!! 谢谢!!! |
-- 作者:有点蓝 -- 发布时间:2018/5/11 22:08:00 -- 全局代码: Public Class simplefraction \' Methods Public Sub New(ByVal num As Integer, ByVal deno As Integer) Me._numerator = num Me._denominator = deno End Sub Public Function Display() As String Me.Simplifying Return String.Format("{0}/{1}", Me._numerator, Me._denominator ) End Function Public Shared Operator +(ByVal a As simplefraction, ByVal b As simplefraction) As simplefraction Return New simplefraction(((a._numerator * b._denominator ) + (b._numerator * a._denominator )), (a._denominator * b._denominator )) End Operator Public Shared Operator /(ByVal a As simplefraction, ByVal b As simplefraction) As simplefraction Return New simplefraction((a._numerator * b._denominator ), (a._denominator * b._numerator)) End Operator Public Shared Operator *(ByVal a As simplefraction, ByVal b As simplefraction) As simplefraction Return New simplefraction((a._numerator * b._numerator), (a._denominator * b._denominator )) End Operator Public Shared Operator -(ByVal a As simplefraction, ByVal b As simplefraction) As simplefraction Return New simplefraction(((a._numerator * b._denominator ) - (b._numerator * a._denominator )), (a._denominator * b._denominator )) End Operator Private Sub Simplifying() Dim turn As Integer = 0 Dim minus As Boolean = (Me._numerator < 0) Me._numerator = Math.Abs(Me._numerator) turn = Math.Min(Me._numerator, Me._denominator ) Do While (turn > 1) If (((Me._numerator Mod turn) = 0) AndAlso ((Me._denominator Mod turn) = 0)) Then Me._numerator = (Me._numerator / turn) Me._denominator = (Me._denominator / turn) Else turn -= 1 End If Loop If minus Then Me._numerator = (-1 * Me._numerator) End If End Sub \' Properties Public ReadOnly Property Denominator As String Get Return Me._denominator .ToString End Get End Property Public ReadOnly Property Numerator As String Get Return Me._numerator.ToString End Get End Property \' Fields Private _denominator As Integer Private _numerator As Integer End Class 调用 Dim _a As String = "1/2" Dim _b As String = "1/4" Dim arr() As String = _a.Split("/") Dim brr() As String = _b.Split("/") Dim a As new simplefraction(val(arr(0)), val(arr(1))) Dim b As new simplefraction(val(brr(0)), val(brr(1))) Dim c As simplefraction = a + b msgbox(c.Display) c = a - b msgbox(c.Display) c = a * b msgbox(c.Display) c = a / b msgbox(c.Display) |
-- 作者:jackyfashion -- 发布时间:2018/5/11 22:20:00 -- 谢谢老师!!! 我还是看不懂.以后看看能不能领悟得到. 谢谢!!!
|