20 points Write a fraction calculator program that adds subt
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](/WebImages/25/20-points-write-a-fraction-calculator-program-that-adds-subt-1064453-1761556563-0.webp)
![[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](/WebImages/25/20-points-write-a-fraction-calculator-program-that-adds-subt-1064453-1761556563-1.webp)
![[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](/WebImages/25/20-points-write-a-fraction-calculator-program-that-adds-subt-1064453-1761556563-2.webp)