|
| 1 | +'' examples/manual/proguide/iterators/fraction.bas |
| 2 | +'' |
| 3 | +'' NOTICE: This file is part of the FreeBASIC Compiler package and can't |
| 4 | +'' be included in other distributions without authorization. |
| 5 | +'' |
| 6 | +'' See Also: https://www.freebasic.net/wiki/wikka.php?wakka=ProPgTypeIterators |
| 7 | +'' -------- |
| 8 | + |
| 9 | +Type fraction |
| 10 | + ' user interface |
| 11 | + Declare Constructor (ByVal n As Integer, ByVal d As Integer) |
| 12 | + Declare Operator Cast () As String |
| 13 | + ' overload iteration operators when Step is defined in For...Next statement |
| 14 | + Declare Operator For (ByRef iterateStep As fraction) |
| 15 | + Declare Operator Next (ByRef iterateCondition As fraction, ByRef iterateStep As fraction) As Integer |
| 16 | + Declare Operator Step (ByRef step_var As fraction) |
| 17 | + ' internal variables and cast operator |
| 18 | + As Integer num, den |
| 19 | + Declare Operator Cast () As Double |
| 20 | +End Type |
| 21 | + |
| 22 | +Constructor fraction (ByVal n As Integer, ByVal d As Integer) |
| 23 | + this.num = n |
| 24 | + this.den = d |
| 25 | +End Constructor |
| 26 | + |
| 27 | +Operator fraction.Cast () As String |
| 28 | + ' search for the highest common factor (a) between numerator and denominator |
| 29 | + Dim As Integer a = Abs(This.num), b = Abs(This.den) |
| 30 | + If a <> 0 Then |
| 31 | + While a <> b |
| 32 | + If a > b Then |
| 33 | + a -= b |
| 34 | + Else |
| 35 | + b -= a |
| 36 | + End If |
| 37 | + Wend |
| 38 | + Else |
| 39 | + a = 1 |
| 40 | + End If |
| 41 | + ' reduce the fraction |
| 42 | + Return num \ a & "/" & den \ a |
| 43 | +End Operator |
| 44 | + |
| 45 | +Operator fraction.Cast () As Double |
| 46 | + Return This.num / This.den |
| 47 | +End Operator |
| 48 | + |
| 49 | +Operator fraction.For (ByRef iterateStep As fraction) |
| 50 | + ' search for the least common multiple (a) between the two denominators |
| 51 | + Dim As Integer a = Abs(This.den), b = Abs(iterateStep.den), c = a, d = b |
| 52 | + While a <> b |
| 53 | + If a > b Then |
| 54 | + b += d |
| 55 | + Else |
| 56 | + a += c |
| 57 | + End If |
| 58 | + Wend |
| 59 | + ' align at the same denominator the 2 fractions |
| 60 | + This.num *= a \ This.den |
| 61 | + This.den = a |
| 62 | + iterateStep.num *= a \ iterateStep.den |
| 63 | + iterateStep.den = a |
| 64 | +End Operator |
| 65 | + |
| 66 | +Operator fraction.Next (ByRef iterateCondition As fraction, ByRef iterateStep As fraction) As Integer |
| 67 | + If iterateStep.num < 0 Or iterateStep.den < 0 Then |
| 68 | + Return This >= iterateCondition |
| 69 | + Else |
| 70 | + Return This <= iterateCondition |
| 71 | + End If |
| 72 | +End Operator |
| 73 | + |
| 74 | +Operator fraction.Step (ByRef iterateStep As fraction) |
| 75 | + This.num += iterateStep.num |
| 76 | +End Operator |
| 77 | + |
| 78 | + |
| 79 | +Print "iteration from 1/8 to 1/2 by step of 1/12:" |
| 80 | +For iterator As fraction = fraction(1, 8) To fraction(1, 2) Step fraction(1, 12) |
| 81 | + Print " " & iterator; |
| 82 | +Next |
| 83 | +Print |
| 84 | +Print |
| 85 | +Print "iteration from 7/10 to -8/5 by step of -8/15:" |
| 86 | +For iterator As fraction = fraction(7, 10) To fraction(-8, 5) Step fraction(-8, 15) |
| 87 | + Print " " & iterator; |
| 88 | +Next |
| 89 | +Print |
| 90 | + |
| 91 | +Sleep |
| 92 | + |
0 commit comments