Define each function in the as you type the code and must be

Define each function in the as you type the code and must be preceded by a comment block that includes the following:

One line per parameter indicating if it is an input (passed by value) or IO (passed by reference) parameter, the type of the parameter and a very brief description of the parameter.

One line indicating the return type and describing it, for void functions indicate the function returns nothing.

A very brief description of the function\'s purpose.

This comment block should contain the information needed to use the function.

here is the 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;
}

Solution

#include <iostream>
using namespace std;

struct fraction{
   int numerator;
   int denominator;
};

/*
   Params:
       val->   type: int, value for which you want to calculate absolute value
   returns absolute value of the input parameter
*/
int abs( int val ){
   if( val < 0 ){ val*= -1; }
   return val;
}

/*
   Params:
   a->       type: int   (by value)
   b->       type: int   (by value)
   returns the minimum value of a and b
*/
int min( int a, int b){
   if( a < b ){ return a; }
   return b;
}

/*
   Params:
   a->       type: int   (by value)
   b->       type: int   (by value)
   calculates and returns the gcd of a and 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;
}

/*
   Params:
   a->       type: fraction(by reference), A fraction for which the numerator and denominator terms should be reduced
   returns: None
   It reduces the numerator and denominator terms of the input fraction parameter using gcd
*/
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; }
}

/*
   Params:
   a->       type: fraction(by reference), A fraction for which numerator and denominators are to be swapped
   returns: None
   It swaps numerator and denominator values of a given fraction
*/
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;
   }
}

/*
   Params:
   a->       type: fraction   (by value)
   b->       type: fraction   (by value)
   return:   type: fraction
   calculates and returns the addition of two fractions
*/
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;
}

/*
   Params:
   a->       type: fraction   (by value)
   b->       type: fraction   (by value)
   return:   type: fraction
   calculates and returns the subtraction of two fractions
*/
fraction subtract( fraction a, fraction b ){
   b.numerator*= -1;
   return add( a, b );
}

/*
   Params:
   a->       type: fraction   (by value)
   b->       type: fraction   (by value)
   return:   type: fraction
   calculates and returns the multiplication of two fractions
*/
fraction multiply( fraction a, fraction b ){
   fraction c;
   c.numerator = a.numerator*b.numerator;
   c.denominator = a.denominator*b.denominator;
   reduce(c);
   return c;
}

/*
   Params:
   a->       type: fraction   (by value)
   b->       type: fraction   (by value)
   return:   type: fraction
   calculates and returns the division of two fractions
*/
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;
}

Define each function in the as you type the code and must be preceded by a comment block that includes the following: One line per parameter indicating if it is
Define each function in the as you type the code and must be preceded by a comment block that includes the following: One line per parameter indicating if it is
Define each function in the as you type the code and must be preceded by a comment block that includes the following: One line per parameter indicating if it is
Define each function in the as you type the code and must be preceded by a comment block that includes the following: One line per parameter indicating if it is
Define each function in the as you type the code and must be preceded by a comment block that includes the following: One line per parameter indicating if it is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site