Simple Rational Class (Warning: Code dump)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Simple Rational Class (Warning: Code dump)

Postby 1000101 » Mar 26, 2008 21:59

This code implements rationals (no simplifying, however) in an easy to use class which can just be dropped into your own code.

Code: Select all

Type Rational
   
   As Integer      Numerator, Denominator
   
   Declare Operator   Cast () As String
   Declare Operator   Cast () As Single
   Declare Operator   Cast () As Double
   
End Type


Declare Operator   +         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
Declare Operator   -         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
Declare Operator   *         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
Declare Operator   /         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational




Operator   Rational.Cast () As String
   
   
   Return "" & This.Numerator & "/" & This.Denominator
   
   
End Operator


Operator   Rational.Cast () As Single
   
   
   Return This.Numerator / This.Denominator
   
   
End Operator


Operator   Rational.Cast () As Double
   
   
   Return This.Numerator / This.Denominator
   
   
End Operator


'' r:r


Operator   +         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs.Numerator * rhs.Denominator + lhs.Denominator * rhs.Numerator, lhs.Denominator * rhs.Denominator )
   
   
End Operator


Operator   -         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs.Numerator * rhs.Denominator - lhs.Denominator * rhs.Numerator, lhs.Denominator * rhs.Denominator )
   
   
End Operator


Operator   *         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs.Numerator * rhs.Numerator, lhs.Denominator * rhs.Denominator )
   
   
End Operator


Operator   /         ( ByRef lhs As Rational, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs.Numerator * rhs.Denominator, lhs.Denominator * rhs.Numerator )
   
   
End Operator


'' r:i


Operator   +         ( ByRef lhs As Rational, ByRef rhs As Integer ) As Rational
   
   
   Return Type( lhs.Numerator + rhs * lhs.Denominator, lhs.Denominator )
   
   
End Operator


Operator   -         ( ByRef lhs As Rational, ByRef rhs As Integer ) As Rational
   
   
   Return Type( lhs.Numerator - rhs * lhs.Denominator, lhs.Denominator )
   
   
End Operator


Operator   *         ( ByRef lhs As Rational, ByRef rhs As Integer ) As Rational
   
   
   Return Type( lhs.Numerator * rhs, lhs.Denominator )
   
   
End Operator


Operator   /         ( ByRef lhs As Rational, ByRef rhs As Integer ) As Rational
   
   
   Return Type( lhs.Numerator, lhs.Denominator * rhs )
   
   
End Operator


'' i:r


Operator   +         ( ByRef lhs As Integer, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs * rhs.Denominator + rhs.Numerator, rhs.Denominator )
   
   
End Operator


Operator   -         ( ByRef lhs As Integer, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs * rhs.Denominator - rhs.Numerator, rhs.Denominator )
   
   
End Operator


Operator   *         ( ByRef lhs As Integer, ByRef rhs As Rational ) As Rational
   
   
   Return Type( lhs * rhs.Numerator, rhs.Denominator )
   
   
End Operator


Operator   /         ( ByRef lhs As Integer, ByRef rhs As Rational ) As Rational
   
   
   Return Type( rhs.Numerator, lhs * rhs.Denominator )
   
   
End Operator




Dim As Integer    I = 3
Dim As Rational   A = ( 1, 2 )
Dim As Rational   B = ( 2, 1 )
Dim As Rational   C


C = A + B : Print A; " + "; B; " = "; C
C = A - B : Print A; " - "; B; " = "; C
C = A * B : Print A; " * "; B; " = "; C
C = A / B : Print A; " / "; B; " = "; C
Print
C = B + A : Print B; " + "; A; " = "; C
C = B - A : Print B; " - "; A; " = "; C
C = B * A : Print B; " * "; A; " = "; C
C = B / A : Print B; " / "; A; " = "; C
Print
C = A + I : Print A; " + "; I; " = "; C
C = A - I : Print A; " - "; I; " = "; C
C = A * I : Print A; " * "; I; " = "; C
C = A / I : Print A; " / "; I; " = "; C
Print
C = I + B : Print I; " + "; B; " = "; C
C = I - B : Print I; " - "; B; " = "; C
C = I * B : Print I; " * "; B; " = "; C
C = I / B : Print I; " / "; B; " = "; C
Print

Sleep
Veggiet
Posts: 156
Joined: Apr 17, 2006 19:41

Postby Veggiet » Mar 27, 2008 0:16

That's cool

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests