avr39ripe

cppFractionBaseClass

Jul 13th, 2021
603
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. public:
  10.     Fraction(int num, int denom)
  11.         : numerator{ num }, denominator{ denom ? denom : 1 }
  12.     {
  13.         reduce();
  14.         std::cout << "Fraction constructed for " << this << '\n';
  15.     };
  16.     Fraction() : Fraction(1, 1) {};
  17.     ~Fraction() { std::cout << "Fraction destructed for " << this << '\n'; };
  18.     void setNumerator(int num) { numerator = num; reduce(); };
  19.     int getNumerator() const { return numerator; };
  20.     void setDenominator(int denom) { denominator = denom ? denom : 1; reduce(); };
  21.     int getDenominator() const { return denominator; };
  22.     void print() const;
  23.     Fraction& add(const Fraction& fract);
  24.     Fraction& sub(const Fraction& fract);
  25.     Fraction& mul(const Fraction& fract);
  26.     Fraction& div(const Fraction& fract);
  27.    
  28. };
  29.  
  30. int Fraction::gcd(int a, int b)
  31. {
  32.     while (b)
  33.     {
  34.         a %= b;
  35.         std::swap(a, b);
  36.     }
  37.     return a;
  38. }
  39.  
  40. void Fraction::reduce()
  41. {
  42.     int gcdVal{ gcd(numerator, denominator) };
  43.     numerator /= gcdVal;
  44.     denominator /= gcdVal;
  45.  
  46.     if (denominator < 0 and numerator < 0)
  47.     {
  48.         denominator *= -1;
  49.         numerator *= -1;
  50.     }
  51. }
  52.  
  53. Fraction& Fraction::add(const Fraction& fract)
  54. {
  55.     numerator *= fract.denominator + fract.numerator * denominator;
  56.     denominator *= fract.denominator;
  57.     reduce();
  58.     return *this;
  59. }
  60.  
  61. Fraction& Fraction::sub(const Fraction& fract)
  62. {
  63.     numerator *= fract.denominator - fract.numerator * denominator;
  64.     denominator *= fract.denominator;
  65.     reduce();
  66.     return *this;
  67. }
  68.  
  69. Fraction& Fraction::mul(const Fraction& fract)
  70. {
  71.     numerator *= fract.numerator;
  72.     denominator *= fract.denominator;
  73.     reduce();
  74.     return *this;
  75. }
  76.  
  77. Fraction& Fraction::div(const Fraction& fract)
  78. {
  79.     numerator *= fract.denominator;
  80.     denominator *= fract.numerator;
  81.     reduce();
  82.     return *this;
  83. }
  84.  
  85. void Fraction::print() const
  86. {
  87.     std::cout << '(' << numerator << " / " << denominator << ")";
  88. }
  89.  
  90. int main()
  91. {
  92.     Fraction a{ 1,2 };
  93.     Fraction b{ 1,3 };
  94.    
  95.     std::cout << " a + b = "; a.add(b).print(); std::cout << '\n';
  96.  
  97.     a.setNumerator(1); a.setDenominator(2);
  98.     std::cout << " a - b = "; a.sub(b).print(); std::cout << '\n';
  99.  
  100.     a.setNumerator(1); a.setDenominator(2);
  101.     std::cout << " a * b = "; a.mul(b).print(); std::cout << '\n';
  102.  
  103.     a.setNumerator(1); a.setDenominator(2);
  104.     std::cout << " a / b = "; a.div(b).print(); std::cout << '\n';
  105.  
  106.     return 0;
  107. }
RAW Paste Data