Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMake a Fraction class in VB .NET
DescriptionThis example shows how to make a Fraction class in Visual Basic .NET. 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.
Keywordsfraction, numerator, denominator, VB.NET
CategoriesAlgorithms
 
The class uses the following code, most of which is straightforward.
 
Public Class Fraction
    Private m_Numerator As Long
    Private m_Denominator As Long

    ' Initialize the fraction from a string A/B.
    Public Sub New(ByVal txt As String)
        Dim pieces() As String

        pieces = Split(txt, "/")
        m_Numerator = CLng(pieces(0))
        m_Denominator = CLng(pieces(1))
        Simplify()
    End Sub

    ' Initialize the fraction from numerator and
    ' denominator.
    Public Sub New(ByVal numer As Long, ByVal denom As Long)
        m_Numerator = numer
        m_Denominator = denom
        Simplify()
    End Sub

    ' Property procedures.
    Public Property Numerator() As Long
        Get
            Return m_Numerator
        End Get
        Set(ByVal Value As Long)
            m_Numerator = Value
        End Set
    End Property
    Public Property Denominator() As Long
        Get
            Return m_Denominator
        End Get
        Set(ByVal Value As Long)
            m_Denominator = Value
        End Set
    End Property

    ' Return Me * frac.
    Public Function Times(ByVal frac As Fraction) As _
        Fraction
        ' Swap numerators and denominators to simplify.
        Dim result1 As New Fraction(Me.Numerator, _
            frac.Denominator)
        Dim result2 As New Fraction(frac.Numerator, _
            Me.Denominator)

        Return New Fraction( _
            result1.Numerator * result2.Numerator, _
            result1.Denominator * result2.Denominator)
    End Function

    ' Return -Me.
    Public Function Negate() As Fraction
        Return New Fraction(-Me.Numerator, Me.Denominator)
    End Function

    ' Return 1/Me.
    Public Function Invert() As Fraction
        Return New Fraction(Me.Denominator, Me.Numerator)
    End Function

    ' Return Me + frac.
    Public Function Plus(ByVal frac As Fraction) As Fraction
        Dim gcd_ab As Long

        ' Get the denominators' greatest common divisor.
        gcd_ab = GCD(Me.Denominator, frac.Denominator)

        Dim numer As Long = _
            Me.Numerator * (frac.Denominator \ gcd_ab) + _
            frac.Numerator * (Me.Denominator \ gcd_ab)
        Dim denom As Long = _
            Me.Denominator * (frac.Denominator \ gcd_ab)
        Return New Fraction(numer, denom)
    End Function

    ' Return This - frac.
    Public Function Minus(ByVal frac As Fraction) As _
        Fraction
        Return Me.Plus(frac.Negate())
    End Function

    ' Return This / frac.
    Public Function DivideBy(ByVal frac As Fraction) As _
        Fraction
        Return 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 Overrides Function ToString() As String
        ToString = Numerator.ToString() & "/" & _
            Denominator.ToString()
    End Function
End Class
 
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 Return b
        a = b
        b = remainder
    Loop
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.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated