20 points Write a fraction calculator program that adds subt

[20 points] Write a fraction calculator program that adds, subtracts, multiplies, and di vides fractions. Your program should check for the division by 0, have and use the fol- lowing functions (a) abs - returns the absolute value of a given integer. (b) min -returns the smallest of two positive integers. (c) gcd returns the greatest common divisor of two positive integers. (d) reduce reduces a given fraction, makes sure that the negative value, if any, is only in the denominator (e) flip flips and reduces a given fraction (f) add - finds the reduced sum of a pair of given fractions (g) subtract finds the reduced difference of a pair of given fractions, by making the second fraction negative then using the add function (h) multiply- finds the reduced product of a pair of given fractions. (i) divide -finds the reduced quotient of a pair of given fractions by flipping the second fraction then using the multiply function Sample Runs: Fraction Calculator 1.0 Enter an arithmetic expression of the form a/b e c/d, replace by any one of +, ,*,or / ?> 2/41/3 Solution : (l/2) + (1/3) = 5/6 Again (Y/N)? Y 1/-35/6 Solution : (-1/3) - (5/6) = -7/6 Again (Y/N)? Y ? 4/5-2/-10 Solution : (4/5) * (1/5) -4/25 Again (Y/N)? Y 6/3/ 1/3 Solution : (2/1) / (1/3) -6/1 Again (Y/N)? N

Solution

Following is the required code:

#include <iostream>
using namespace std;

struct fraction{
   int numerator;
   int denominator;
};

int abs( int val ){
   if( val < 0 ){ val*= -1; }
   return val;
}

int min( int a, int b){
   if( a < b ){ return a; }
   return b;
}

int gcd( int a, int b ){
   int gcdIs = 0;
   for(int i = 1; i <= min( a, b); i++){
       if( a%i == 0 && b%i == 0 ){
           gcdIs = i;
       }
   }
   return gcdIs;  
}

void reduce( fraction &a ){
   int sign = a.numerator*a.denominator;
   a.numerator= abs( a.numerator );
   a.denominator = abs( a.denominator );

   int gcdIs = gcd( a.numerator, a.denominator );
   a.numerator /= gcdIs;
   a.denominator /= gcdIs;
   if( sign < 0 ){ a.numerator*= -1; }
}

void flip( fraction& a ){
   int temp = a.numerator;
   a.numerator = a.denominator;
   a.denominator = temp;
   if( a.denominator < 0 ){
       a.denominator*= -1;
       a.numerator*= -1;
   }
}

fraction add( fraction a, fraction b ){
   fraction c;
   c.numerator = a.numerator*b.denominator + a.denominator*b.numerator;
   c.denominator = a.denominator*b.denominator;
   reduce(c);
   return c;
}

fraction subtract( fraction a, fraction b ){
   b.numerator*= -1;
   return add( a, b );
}

fraction multiply( fraction a, fraction b ){
   fraction c;
   c.numerator = a.numerator*b.numerator;
   c.denominator = a.denominator*b.denominator;
   reduce(c);
   return c;
}

fraction divide( fraction a, fraction b ){
   flip(b);
   return multiply( a, b );
}

int main(){
   cout << \"Fraction Calculator 1.0\" << endl;
   cout << \"Enter an arithmetic expression of the form a/b @ c/d,\" << endl;
   cout << \"Replace @ by any one of +, -, *, or /\" << endl;
   fraction a,b, result;
   char optr, temp;

   while( true ){
       cout << \"?> \";
       cin >> a.numerator >> temp >> a.denominator >> optr >> b.numerator >> temp >> b.denominator;
       if( a.denominator == 0 || b.denominator == 0 ){
           cout << \"Errorneous input, enter again\" << endl;
           continue;
       }
       reduce(a);
       reduce(b);
       if( optr == \'+\' ){
           result = add( a, b );   }
       if( optr == \'-\' ){
           result = subtract( a, b );   }
       if( optr == \'*\' ){
           result = multiply( a, b );   }
       if( optr == \'/\' ){
           result = divide( a, b );   }

       //print the result
       cout << \"Solution: (\"<< a.numerator << \"/\" << a.denominator <<\") \";
       cout << optr;
       cout << \" (\"<< b.numerator << \"/\" << b.denominator <<\") = \";
       cout << result.numerator << \"/\" << result.denominator << endl;      
      
       cout << \"Again (Y/N)? \";
       cin >> temp;
       if( temp != \'Y\' ){
           break;
       }
   }

   return 0;
}

 [20 points] Write a fraction calculator program that adds, subtracts, multiplies, and di vides fractions. Your program should check for the division by 0, have
 [20 points] Write a fraction calculator program that adds, subtracts, multiplies, and di vides fractions. Your program should check for the division by 0, have
 [20 points] Write a fraction calculator program that adds, subtracts, multiplies, and di vides fractions. Your program should check for the division by 0, have

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site