Advertisement
KeiroKamioka

Jigoku1_Comp

Jan 28th, 2021
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct fraction
  7. {
  8. public:
  9.     int a;
  10.     int b;
  11.  
  12.     fraction(int _a, int _b)
  13.     {
  14.         a = _a;
  15.         b = _b;
  16.     }
  17.  
  18.     string to_string()
  19.     {
  20.         return "(" + std::to_string(a) + "/" + std::to_string(b) + ")";
  21.     }
  22.  
  23.     //This function subtracts the input fraction from this fraction
  24.     //Do not simplify the result in any way
  25.     fraction subtract(fraction f)
  26.     {
  27.         // Your code starts here
  28.         fraction subResult{a,b};
  29.         subResult.a = (a * f.b) - (b * f.a);
  30.         subResult.b = (b * f.b);
  31.  
  32.         return subResult;
  33.  
  34.         // Your code ends here
  35.     }
  36.  
  37.     //This function divides this fraction by the input fraction
  38.     //Do not simplify the result in any way
  39.     fraction divide(fraction f)
  40.     {
  41.         // Your code starts here
  42.         fraction divResult{a,b};
  43.         divResult.b = (f.a * b);
  44.         divResult.a = (f.b * a);
  45.         return divResult;
  46.  
  47.         // Your code ends here
  48.     }
  49.  
  50.     //This function simplifies the fraction
  51.     //Definition of simplification: https://www.mathsisfun.com/simplifying-fractions.html
  52.     fraction simplify()
  53.     {
  54.         // Your code starts here
  55.         fraction simResult{ a,b };
  56.         int t, gcd;
  57.         while (b != 0)
  58.         {
  59.             t = b;
  60.             b = a % b;
  61.             a = t;
  62.         }
  63.  
  64.         gcd = a;
  65.         simResult.a = simResult.a / gcd;
  66.         simResult.b = simResult.b / gcd;
  67.  
  68.         return simResult;
  69.  
  70.         // Your code ends here
  71.     }
  72. };
  73. //After
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement