|
|
Title | Make a Fraction class |
Description | This example shows how to make a Fraction class in Visual Basic 6. It uses private m_Numerator and m_Denominator variables to represent the fraction. It provides methods to combine Fractions (add, multiply, and so forth), and to simplify fractions. |
Keywords | fraction, numerator, denominator |
Categories | Algorithms |
|
|
The class uses the following code, most of which is straightforward.
|
|
Private m_Numerator As Long
Private m_Denominator As Long
' Initialize the fraction from a string A/B.
Public Sub FromString(ByVal txt As String)
Dim pieces() As String
pieces = Split(txt, "/")
m_Numerator = CLng(pieces(0))
m_Denominator = CLng(pieces(1))
Simplify
End Sub
Public Property Get Numerator() As Long
Numerator = m_Numerator
End Property
Public Property Let Numerator(ByVal value As Long)
m_Numerator = value
Simplify
End Property
Public Property Get Denominator() As Long
Denominator = m_Denominator
End Property
Public Property Let Denominator(ByVal value As Long)
m_Denominator = value
Simplify
End Property
' Set the numerator and denominator.
Public Sub SetValues(ByVal numer As Long, ByVal denom As _
Long)
m_Numerator = numer
m_Denominator = denom
Simplify
End Sub
' Return Me * frac.
Public Function Times(ByVal frac As Fraction) As Fraction
Dim result1 As New Fraction
Dim result2 As New Fraction
Dim result As New Fraction
Dim numer As Long
Dim denom As Long
' Swap numerators and denominators to simplify.
result1.SetValues m_Numerator, frac.Denominator
result2.SetValues frac.Numerator, m_Denominator
' Multiply the simplified results.
numer = result1.Numerator * result2.Numerator
denom = result1.Denominator * result2.Denominator
result.SetValues numer, denom
Set Times = result
End Function
' Return -Me.
Public Function Negate() As Fraction
Dim result As New Fraction
result.SetValues -m_Numerator, m_Denominator
Set Negate = result
End Function
' Return 1/Me.
Public Function Invert() As Fraction
Dim result As New Fraction
result.SetValues m_Denominator, m_Numerator
Set Invert = result
End Function
' Return Me + frac.
Public Function Plus(ByVal frac As Fraction) As Fraction
Dim result As New Fraction
Dim gcd_ab As Long
Dim numer As Long
Dim denom As Long
' Get the denominators' greatest common divisor.
gcd_ab = GCD(m_Denominator, frac.Denominator)
numer = _
m_Numerator * (frac.Denominator \ gcd_ab) + _
frac.Numerator * (m_Denominator \ gcd_ab)
denom = _
m_Denominator * (frac.Denominator \ gcd_ab)
result.SetValues numer, denom
Set Plus = result
End Function
' Return This - frac.
Public Function Minus(ByVal frac As Fraction) As Fraction
Set Minus = Me.Plus(frac.Negate())
End Function
' Return This / frac.
Public Function DivideBy(ByVal frac As Fraction) As Fraction
Set DivideBy = Me.Times(frac.Invert())
End Function
' Simplify the fraction.
Private Sub Simplify()
Dim gcd_ab As Long
' Simplify the sign.
If m_Denominator < 0 Then
m_Numerator = -m_Numerator
m_Denominator = -m_Denominator
End If
' Factor out the greatest common divisor of the
' numerator and the denominator.
gcd_ab = GCD(m_Numerator, m_Denominator)
m_Numerator = m_Numerator \ gcd_ab
m_Denominator = m_Denominator \ gcd_ab
End Sub
' Return a Double representation of the fraction.
Public Function ToDouble() As Double
ToDouble = m_Numerator / m_Denominator
End Function
' Return the fraction's value as a string.
Public Function ToString() As String
ToString = Format$(Numerator) & "/" & _
Format$(Denominator)
End Function
|
|
The Simplify subroutine divides the greatest common divisor (GCD) from the numerator and the denominator. The following code shows the GCD function, calculated using Euler's Method.
|
|
' Return the greatest common divisor (GCD) of a and b.
Public Function GCD(ByVal a As Long, ByVal b As Long) As _
Long
Dim tmp As Long
Dim remainder As Long
' Make a >= b.
a = Abs(a)
b = Abs(b)
If a < b Then
tmp = a
a = b
b = tmp
End If
' Pull out remainders.
Do
remainder = a Mod b
If remainder = 0 Then Exit Do
a = b
b = remainder
Loop
GCD = b
End Function
|
|
I'll leave it as an exercise to figure out why the Times method makes two intermediate Fractions that mix the numerators and denominators of the operands.
For more information on algorithms in Visual Basic, see my book Ready-to-Run Visual Basic Algorithms.
|
|
|
|
|
|