SHARE
TWEET

Fraction Program 2... ( This is a nicer version)

aod6060 May 6th, 2012 9 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.         This is the nicer version of the
  3.         fraction program
  4.        
  5.         like the last one I'm not going to simplify the fractions.
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. // Fraction Class
  13. struct Fraction {
  14.         int num, dem;
  15.        
  16.         Fraction() : num(0), dem(0) {}
  17.        
  18.         Fraction(int _num, int _dem) : num(_num), dem(_dem) {}
  19.        
  20.         Fraction& operator = (const Fraction& f) {
  21.                 num = f.num;
  22.                 dem = f.dem;
  23.                 return *this;
  24.         }
  25.        
  26.         Fraction operator + (Fraction& f) {
  27.                 Fraction temp = *this, temp2 = f, ret;
  28.                 equalize(temp, temp2);
  29.                 ret.num = temp.num + temp2.num;
  30.                 ret.dem = temp.dem;
  31.                 return ret;
  32.         }
  33.        
  34.         Fraction operator - (Fraction& f) {
  35.                 Fraction temp = *this, temp2 = f, ret;
  36.                 equalize(temp, temp2);
  37.                 ret.num = temp.num - temp2.num;
  38.                 ret.dem = temp.dem;
  39.                 return ret;
  40.         }
  41.        
  42.         Fraction operator * (Fraction& f) {
  43.                 Fraction temp;
  44.                 temp.num = num*f.num;
  45.                 temp.dem = dem*f.dem;
  46.                 return temp;
  47.         }
  48.        
  49.         Fraction operator / (Fraction& f) {
  50.                 Fraction temp;
  51.                 temp.num = num*f.dem;
  52.                 temp.dem = dem*f.num;
  53.                 return temp;
  54.         }
  55.        
  56.         private:
  57.        
  58.         void equalize(Fraction& f1, Fraction& f2) {
  59.                 if(f1.dem != f2.dem) {
  60.                         Fraction temp = f1, temp2 = f2;
  61.                        
  62.                         f1.num *= temp2.dem;
  63.                         f1.dem *= temp2.dem;
  64.                         f2.num *= temp.dem;
  65.                         f2.dem *= temp.dem;
  66.                 }
  67.         }
  68. };
  69.  
  70. // Making the Fraction class output streamable :D
  71. ostream& operator << (ostream& out, Fraction& frac) {
  72.         out << frac.num << "/" << frac.dem;
  73.         return out;
  74. }
  75.  
  76. // Making the Fraction class input streamable :D
  77. istream& operator >> (istream& in, Fraction& frac) {
  78.         in >> frac.num;
  79.         cout << "__" << endl;
  80.         in >> frac.dem;
  81.         return in;
  82. }
  83.  
  84. // Prints the answer
  85. void print_answer(char op, Fraction& a, Fraction& f1, Fraction& f2) {
  86.         cout << a << " = " << f1 << " " << op << " " << f2 << endl;
  87. }
  88.  
  89. int main() {
  90.         unsigned char op;
  91.         Fraction fraction1, fraction2, answer;
  92.        
  93.         cout << "Please type in the first fraction" << endl;
  94.         cin >> fraction1;
  95.        
  96.         cout << "Please type in the second fraction" << endl;
  97.         cin >> fraction2;
  98.        
  99.         cout << "Please type int the operator, (+, -, *, /) " << endl;
  100.        
  101.         cin >> op;
  102.        
  103.         if(op == '+') {
  104.                 answer = fraction1 + fraction2;
  105.                 print_answer(op, answer, fraction1, fraction2);
  106.         } else if(op == '-') {
  107.                 answer = fraction1 - fraction2;
  108.                 print_answer(op, answer, fraction1, fraction2);
  109.         } else if(op == '*') {
  110.                 answer = fraction1 * fraction2;
  111.                 print_answer(op, answer, fraction1, fraction2);
  112.         } else if(op == '/') {
  113.                 answer = fraction1 / fraction2;
  114.                 print_answer(op, answer, fraction1, fraction2);
  115.         } else {
  116.                 cout << "Invalid operator " << op << " please use (+, -, *, /)" << endl;
  117.         }
  118.        
  119.         return 0;
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top