avr39ripe

cppFractionHWExample

Jul 21st, 2021
661
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. class Fraction
  4. {
  5.     int numerator;
  6.     int denominator;
  7.     int gcd(int a, int b);
  8.     void reduce();
  9.  
  10. public:
  11.  
  12.     Fraction() : Fraction(1, 1) {}
  13.     Fraction(int gotNum, int gotDenom) : numerator{ gotNum }, denominator{ gotDenom } { reduce(); }
  14.  
  15.     void setNumerator(int num) { numerator = num; reduce(); };
  16.     int getNumerator() const { return numerator; };
  17.     void setDenominator(int denom) { denominator = denom ? denom : 1; reduce(); };
  18.     int getDenominator() const { return denominator; };
  19.     void set(int num, int denom) { numerator = num; denominator = denom ? denom : 1; reduce(); }
  20.     void print() const;
  21.     Fraction add(const Fraction& fract);
  22.     Fraction sub(const Fraction& fract);
  23.     Fraction mul(const Fraction& fract);
  24.     Fraction div(const Fraction& fract);
  25.     Fraction add(int Int);
  26.     Fraction sub(int Int);
  27.     Fraction mul(int Int);
  28.     Fraction div(int Int);
  29.  
  30. };
  31.  
  32. int Fraction::gcd(int a, int b)
  33. {
  34.     while (b)
  35.     {
  36.         a %= b;
  37.         std::swap(a, b);
  38.     }
  39.     return a;
  40. }
  41.  
  42. void Fraction::reduce()
  43. {
  44.     int gcdVal{ gcd(numerator, denominator) };
  45.     numerator /= gcdVal;
  46.     denominator /= gcdVal;
  47.  
  48.     if (denominator < 0 and numerator < 0)
  49.     {
  50.         denominator *= -1;
  51.         numerator *= -1;
  52.     }
  53. }
  54.  
  55. Fraction Fraction::add(const Fraction& fract)
  56. {
  57.     return Fraction{ numerator * fract.denominator + fract.numerator * denominator, denominator * fract.denominator };
  58. }
  59.  
  60. Fraction Fraction::sub(const Fraction& fract)
  61. {
  62.     return Fraction{ numerator * fract.denominator - fract.numerator * denominator, denominator * fract.denominator };
  63. }
  64.  
  65. Fraction Fraction::mul(const Fraction& fract)
  66. {
  67.     return Fraction{ numerator * fract.numerator, denominator * fract.denominator };
  68.  
  69. }
  70.  
  71. Fraction Fraction::div(const Fraction& fract)
  72. {
  73.     return Fraction{ numerator * fract.denominator, denominator * fract.numerator };
  74. }
  75.  
  76. Fraction Fraction::add(int Int)
  77. {
  78.     return Fraction{ numerator + Int * denominator, denominator };
  79. }
  80.  
  81. Fraction Fraction::sub(int Int)
  82. {
  83.     return Fraction{ numerator - Int * denominator, denominator };
  84. }
  85.  
  86. Fraction Fraction::mul(int Int)
  87. {
  88.     return Fraction{ numerator * Int, denominator };
  89. }
  90.  
  91. Fraction Fraction::div(int Int)
  92. {
  93.     return Fraction{ numerator, denominator * Int };
  94. }
  95.  
  96. void Fraction::print() const
  97. {
  98.     std::cout << '(' << numerator << " / " << denominator << ")";
  99. }
  100.  
  101. int main()
  102. {
  103.     Fraction a{ 1,2 };
  104.     Fraction b{ 1,3 };
  105.     int c{ 7 };
  106.  
  107.     std::cout << "a = "; a.print(); std::cout << " b = "; b.print(); std::cout << " c = " << c; std::cout << '\n';
  108.     std::cout << "a + b = "; a.add(b).print(); std::cout << '\n';
  109.     std::cout << "a - b = "; a.sub(b).print(); std::cout << '\n';
  110.     std::cout << "a * b = "; a.mul(b).print(); std::cout << '\n';
  111.     std::cout << "a / b = "; a.div(b).print(); std::cout << '\n';
  112.     std::cout << "a + c = "; a.add(c).print(); std::cout << '\n';
  113.     std::cout << "a - c = "; a.sub(c).print(); std::cout << '\n';
  114.     std::cout << "a * c = "; a.mul(c).print(); std::cout << '\n';
  115.     std::cout << "a / c = "; a.div(c).print(); std::cout << '\n';
  116.  
  117.     return 0;
  118. }
RAW Paste Data