avr39ripe

cppFractionBaseSimple

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