Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- struct fraction
- {
- public:
- int a;
- int b;
- fraction(int _a, int _b)
- {
- a = _a;
- b = _b;
- }
- string to_string()
- {
- return "(" + std::to_string(a) + "/" + std::to_string(b) + ")";
- }
- //This function subtracts the input fraction from this fraction
- //Do not simplify the result in any way
- fraction subtract(fraction f)
- {
- // Your code starts here
- fraction subResult{a,b};
- subResult.a = (a * f.b) - (b * f.a);
- subResult.b = (b * f.b);
- return subResult;
- // Your code ends here
- }
- //This function divides this fraction by the input fraction
- //Do not simplify the result in any way
- fraction divide(fraction f)
- {
- // Your code starts here
- fraction divResult{a,b};
- divResult.b = (f.a * b);
- divResult.a = (f.b * a);
- return divResult;
- // Your code ends here
- }
- //This function simplifies the fraction
- //Definition of simplification: https://www.mathsisfun.com/simplifying-fractions.html
- fraction simplify()
- {
- // Your code starts here
- fraction simResult{ a,b };
- int t, gcd;
- while (b != 0)
- {
- t = b;
- b = a % b;
- a = t;
- }
- gcd = a;
- simResult.a = simResult.a / gcd;
- simResult.b = simResult.b / gcd;
- return simResult;
- // Your code ends here
- }
- };
- //After
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement