Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. class fraction
  9. {
  10. private:
  11.  long num;
  12.  long den;
  13.  char over;
  14. public:
  15.  void getFrac()
  16.  {
  17.   cout << "Enter a fraction: ";
  18.   cin >> num >> over >> den;
  19.   lowterms();
  20.  }
  21.  fraction addFraction(fraction f2)
  22.  {
  23.   f2.num =(num * f2.den) + (den * f2.num);
  24.   f2.den = (den * f2.den); 
  25.   return f2;
  26.  }
  27.  fraction subFraction(fraction f2)
  28.  {
  29.   f2.num =(num * f2.den) - (den * f2.num);
  30.   f2.den = (den * f2.den);
  31.   return f2;
  32.  }
  33.  fraction mulFraction(fraction f2)
  34.  {
  35.   f2.num =(num * f2.num);
  36.   f2.den = (den * f2.den);
  37.   return f2;
  38.  }
  39.  fraction divFraction(fraction f2)
  40.  {
  41.   f2.num =(num * f2.den);
  42.   f2.den = (den * f2.num);
  43.   return f2;
  44.  }
  45.  void fraction::lowterms()
  46.  {
  47.   long tnum, tden, temp, gcd;
  48.   tnum = labs(num);
  49.   tden = labs(den);
  50.   if (tden == 0)
  51.   {
  52.       cout<<"Division by 0 is not allowed";exit(3);
  53.   }
  54.   else if (tnum == 0)
  55.   { num = 0; den = 1; return; }
  56.  
  57.   while(tnum != 0)
  58.   {
  59.    if (tnum < tden)
  60.    { temp = tnum; tnum = tden; tden = temp; }
  61.    tnum = tnum - tden;
  62.   }
  63.   gcd = tden;
  64.   num = num / gcd;
  65.   den = den / gcd;
  66.  }
  67.  void showFrac() const
  68.  {
  69.   cout << "The result is " << num << "/" << den << endl;
  70.  }
  71. bool equals(fraction f2)
  72. {
  73.     if (num == f2.num && den == f2.den)
  74.     {
  75.         return true;
  76.     }
  77.     else
  78.     {
  79.         return false;
  80.     }
  81. }
  82. string toString()
  83. {
  84.     ostringstream s;
  85.     if(den == 1)
  86.     {
  87.     s << num;
  88.     }
  89.     else
  90.     {
  91.     s << num << over << den;
  92.     }
  93.     return s.str();
  94. }
  95. };
  96. int main()
  97. {
  98.  fraction f1, f2, f3, f4, f5, f6;
  99.  
  100.  cout << "This will show the sum, difference, product and quotient of two fractions.\n " << endl;
  101.  f1.getFrac();
  102.  f2.getFrac();
  103.  f3 = f1.addFraction(f2);
  104.  f3.lowterms();
  105.  f3.showFrac();
  106.  f4 = f1.subFraction(f2);
  107.  f4.lowterms();
  108.  f4.showFrac();
  109.  f5 = f1.mulFraction(f2);
  110.  f5.lowterms();
  111.  f5.showFrac();
  112.  f6 = f1.divFraction(f2);
  113.  f6.lowterms();
  114.  f6.showFrac();
  115.  if(f1.equals(f2))
  116.  {
  117.      cout<<"Fractions are equal"<<endl;
  118.  }
  119.  else if(!f1.equals(f2))
  120.  {
  121.      cout<<"Fractions are not equal"<<endl;
  122.  }
  123. cout<<"First fraction is "<<f1.toString()<<endl;
  124. cout<<"Second fraction is "<<f2.toString();
  125.  
  126.  system("pause>0");
  127.  return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement