-
-
Notifications
You must be signed in to change notification settings - Fork 596
Add Fraction Math in Modula2 #4520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d7ec6e9
Created new folder and file for FractionMath program in modula2
bellxalli 96ea07e
Moved the file around to its proper place
bellxalli 8a42d42
Made changes to the comparison operators
bellxalli 5b360b7
Made additionl=al changes to overloaded operators
bellxalli ae3b95b
Made changes to operators
bellxalli 2c8339e
Made changes to syntax throughout program
bellxalli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
MODULE FractionMath; | ||
|
||
FROM StrIO IMPORT WriteString, WriteLn; | ||
|
||
TRACED CLASS Fraction; | ||
REVEAL GreatestCommonDivisor, Reduce, SetFraction, ShowFraction, [=], [>], [<], [>=], [<=], | ||
[+], [-], [*], [/]; | ||
VAR | ||
numerator: INTEGER; | ||
denominator: INTEGER; | ||
gcd : INTEGER; | ||
|
||
PROCEDURE SetFraction(n, d : INTEGER); | ||
BEGIN | ||
numerator := n; | ||
denominator := d; | ||
END SetFraction; | ||
|
||
PROCEDURE GreatestCommonDivisor(n, d : INTEGER) : INTEGER; | ||
BEGIN | ||
IF d = 0 THEN | ||
WriteString("error cannot divide by 0"); | ||
END; | ||
|
||
WHILE n # d DO | ||
IF n > d THEN | ||
n := n - d; | ||
ELSE | ||
d := d - n; | ||
END; | ||
END; | ||
|
||
RETURN n; | ||
END GreatestCommonDivisor; | ||
|
||
PROCEDURE Reduce(n, d : INTEGER); | ||
BEGIN | ||
IF d = 0 THEN | ||
WriteString("error cannot divide by 0"); | ||
|
||
ELSE | ||
gcd := GreatestCommonDenominator(n, d); | ||
numerator := n / gcd; | ||
denominator := d / gcd; | ||
END; | ||
END Reduce; | ||
|
||
PROCEDURE ShowFraction(f : Fraction); | ||
BEGIN | ||
WriteString(f.numerator); | ||
WriteString("/"); | ||
WriteString(f.denominator); | ||
WriteLn; | ||
END ShowFraction; | ||
|
||
PROCEDURE [=] isEqual(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
IF (f1.numerator * f2.denominator) = (f1.denominator * f2.numerator) THEN | ||
RETURN TRUE; | ||
ELSE | ||
RETURN FALSE; | ||
END; | ||
END isEqual; | ||
|
||
PROCEDURE [#] notEqual(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
RETURN f1 = f2; | ||
IF f1 = f2 THEN | ||
RETURN FALSE; | ||
ELSE | ||
RETURN TRUE; | ||
END; | ||
END notEqual; | ||
|
||
PROCEDURE [>] isGreater(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
IF (f1.numerator * f2.denominator) > (f1.denominator * f2.numerator) THEN | ||
RETURN TRUE; | ||
ELSE | ||
RETURN FALSE; | ||
END; | ||
END isGreater; | ||
|
||
PROCEDURE [<] isLess(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
IF (f1.numerator * f2.denominator) < (f1.denominator * f2.numerator) THEN | ||
RETURN TRUE; | ||
ELSE | ||
RETURN FALSE; | ||
END; | ||
END isLess; | ||
|
||
PROCEDURE [>=] greaterOrEqual(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
IF (f1.numerator * f2.denominator) >= (f1.denominator * f2.numerator) THEN | ||
RETURN TRUE; | ||
ELSE | ||
RETURN FALSE; | ||
END; | ||
END greaterOrEqual; | ||
|
||
PROCEDURE [<=] lessOrEqual(f1, f2 : Fraction) : BOOL; | ||
BEGIN | ||
IF (f1.numerator * f2.denominator) <= (f1.denominator * f2.numerator) THEN | ||
RETURN TRUE; | ||
ELSE | ||
RETURN FALSE; | ||
END; | ||
END lessOrEqual; | ||
|
||
PROCEDURE [+] Add(f1, f2 : Fraction) : Fraction; | ||
VAR fResult : Fraction; | ||
BEGIN | ||
CREATE(fResult); | ||
IF f1.denominator = f2.denominator THEN | ||
fResult.SetFraction(f1.numerator + f2.numerator, f1.denominator); | ||
ELSE | ||
fResult.SetFraction((f1.numerator * f2.denominator) + (f2.numerator * f1.denominator), f1.denominator * f2.denominator); | ||
gcd = GreatestCommonDivisor(fResult.numerator, fResult.denominator); | ||
fResult.SetFraction(fResult.numerator / gcd, fResult.denominator / gcd); | ||
END; | ||
END Add; | ||
|
||
PROCEDURE [-] Sub(f1, f2 : Fraction) : Fraction; | ||
VAR fResult : Fraction; | ||
BEGIN | ||
CREATE(fResult); | ||
IF f1.denominator = f2.denominator THEN | ||
fResult.SetFraction(f1.numerator - f2.numerator, f1.denominator); | ||
ELSE | ||
fResult.SetFraction((f1.numerator * f2.denominator) - (f2.numerator * f1.denominator), f1.denominator * f2.denominator); | ||
gcd = GreatestCommonDivisor(fResult.numerator, fResult.denominator); | ||
fResult.SetFraction(fResult.numerator / gcd, fResult.denominator / gcd); | ||
END; | ||
END Sub; | ||
|
||
PROCEDURE [*] Mult(f1, f2 : Fraction) : Fraction; | ||
VAR fResult : Fraction; | ||
BEGIN | ||
CREATE(fResult); | ||
fResult.SetFraction(f1.numerator * f2.numerator, f1.denominator * f2.denominator); | ||
gcd = GreatestCommonDivisor(fResult.numerator, fResult.denominator); | ||
fResult.SetFraction(fResult.numerator / gcd, fResult.denominator / gcd); | ||
END Mult; | ||
|
||
PROCEDURE [/] Div(f1, f2 : Fraction) : Fraction; | ||
VAR fResult : Fraction; | ||
BEGIN | ||
CREATE(fResult); | ||
fResult.SetFraction(f1.numerator * f2.denominator, f1.denominator * f1.numerator); | ||
gcd = GreatestCommonDivisor(fResult.numerator, fResult.denominator); | ||
fResult.SetFraction(fResult.numerator / gcd, fResult.denominator / gcd); | ||
END Div; | ||
|
||
BEGIN | ||
SetFraction(1, 1); | ||
END Fraction; | ||
|
||
|
||
VAR fracOne : Fraction; | ||
fracTwo : Fraction; | ||
|
||
PROCEDURE result(f1 : INTEGER, op : STRING, f2 : INTEGER) : Fraction | ||
BEGIN | ||
IF op = "+" THEN | ||
RETURN f1 + f2; | ||
ELSE IF op = "-" THEN | ||
RETURN f1 - f2; | ||
ELSE IF op = "*" THEN | ||
RETURN f1 * f2; | ||
ELSE IF op = "/" THEN | ||
RETURN f1 / f2; | ||
ELSE IF op = ">" THEN | ||
RETURN f1 > f2 | ||
ELSE IF op = ">=" THEN | ||
RETURN f1 >= f2 | ||
ELSE IF op = "<" THEN | ||
RETURN f1 < f2 | ||
ELSE IF op = "<=" THEN | ||
RETURN f1 <= f2 | ||
ELSE IF op = "=" THEN | ||
RETURN f1 = f2 | ||
ELSE IF op = "#" THEN | ||
RETURN f1 # f2 | ||
ELSE | ||
RETURN WriteString("Invalid operator"); | ||
END; | ||
END result; | ||
|
||
BEGIN | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Command line arguments are used for inputs, and stdout is used for outputs for all samples. Please see the project requirements for details. |
||
CREATE(fracOne); | ||
CREATE(fracTwo); | ||
fracOne.SetFraction(2, 3); | ||
fracTwo.SetFraction(4, 5); | ||
|
||
IF fracOne # EMPTY AND fracTwo # EMPTY THEN | ||
|
||
ShowFraction(result(fracOne, "+", fracTwo)); | ||
ShowFraction(result(fracOne, "-", fracTwo)); | ||
ShowFraction(result(fracOne, "*", fracTwo)); | ||
ShowFraction(result(fracOne, "/", fracTwo)); | ||
ShowFraction(result(fracOne, ">", fracTwo)); | ||
ShowFraction(result(fracOne, "<", fracTwo)); | ||
ShowFraction(result(fracOne, ">=", fracTwo)); | ||
ShowFraction(result(fracOne, "<=", fracTwo)); | ||
ShowFraction(result(fracOne, "#", fracTwo)); | ||
ShowFraction(result(fracOne, "=", fracTwo)); | ||
|
||
ELSE | ||
WriteString("Error"); | ||
END; | ||
END FractionMath. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this procedure is never used. There is a lot of code duplication where this could have been used. I'd recommend just making this part of
SetFraction