// The following program is basically a fraction calculator. the user will enter // in their query in the form (x1/y1 [oper] x2/y2). the program will then read // the input, put the numbers into the appropriate variables, and read in the // operator. then the program will evaluate it, through the switch function in // the main function. if the result is an improper fraction, it will convert it // into a mixed fraction. then the program will reduce the fraction, if possible. // finally the program will output the fraction in the correct form, and wait for // a new query. the program will exit once 'x' is used for the operator. // input: x1/y1 [oper] x2/y2 // output: the result of the input #include #include // function reduces fractions, passed by reference // input: fraction numerator (num) and denominator (den) // output: reduced numerator and denominator void reduce(int &num, int &den){ int i = 1, least, gcf, rednum, redden; if(num >= den) least = num; else least = den; for(i; i <= least; i++){ if((num % i == 0) && (den % i == 0)) gcf = i; } num = num / gcf; den = den / gcf; } // function expands an inproper fraction to mixed, num and whole passed by reference // input: numerator, denominator, and whole number // output: numerator and whole number passed back by reference void expand(int &num, int den, int &whole){ whole = num / den; num = num % den; } // function gets the values for both fractions and the operator // input: nothing, except placeholder variables // both fraction's numerators and denominators, and the operator // output: fraction's numerators and denominators, and the operator passed back by reference void frac_input(int &num1, int &den1, int &num2, int &den2, char &oper){ char trash; bool valid; do{ valid = true; cout << "::> "; cin >>num1>>trash>>den1>>oper>>num2>>trash>>den2; if(den1 == 0 || den2 == 0){ valid = false; cout << "Denominator is zero. please retry." << endl; } if(oper != '*' && oper != '/' && oper != '+' && oper != '-' && oper != 'x'){ valid = false; cout << "Invalid operator. please retry. " << endl; } } while (valid == false); } // function multiplies fractions // input: both fractions numerators and denominators // output: result numerator and denominator passed by reference void multiply_frac(int num1, int den1, int num2, int den2, int &ansnum, int &ansden){ ansnum = num1 * num2; ansden = den1 * den2; } // function divides fractions // input: both fractions numerators and denominators // output: result numerator and denominator passed by reference void divide_frac(int num1, int den1, int num2, int den2, int &ansnum, int &ansden){ ansnum = num1 * den2; ansden = den1 * num2; } // function adds fractions // input: both fractions numerators and denominators // output: result numerator and denominator passed by reference void add_frac(int num1, int den1, int num2, int den2, int &ansnum, int &ansden){ if(den1 != den2){ num1 = num1 * den2; num2 = num2 * den1; ansden = den1 * den2; } else ansden = den1; ansnum = num1 + num2; } // function subtracts fractions // input: both fractions numerators and denominators // output: result numerator and denominator passed by reference void subtract_frac(int num1, int den1, int num2, int den2, int &ansnum, int &ansden){ if(den1 != den2){ num1 = num1 * den2; num2 = num2 * den1; ansden = den1 * den2; } else ansden = den1; ansnum = num1 - num2; } // function outputs original query, and result that is reduced and expanded // input: both fraction numerators and denominators, the answer fraction, // whole number if mixed fraction, and the operator // output: nothing except text void output(int num1, int den1, int num2, int den2, int ansnum, int ansden, int whole, char oper){ cout << num1 << "/" << den1 << " " << oper << " " << num2 << "/" << den2 << " = "; if(whole != 0 && ansnum == 0) cout << whole; else if(ansnum != 0 && whole != 0) cout << whole << " & " << abs(ansnum) << "/" << ansden; else if(ansnum != 0 && whole == 0) cout << ansnum << "/" << ansden; else if(ansnum == 0 && whole == 0) cout << "0"; cout << endl; } // main function. function initializes all fraction variables, and acts upon user // input. calls the input function, then a switch to evaluate the user query then // expands, reduces, and outputs the result unless the exit operator ('x') is used int main(){ int num1, num2, den1, den2, ansnum, ansden, whole = 0; char oper; cout << "Welcome. enter your query in the following form: 4/3 + 3/5." << endl << "Put 'x' in the operator (4/3 x 3/3) to exit." << endl; do{ frac_input(num1, den1, num2, den2, oper); switch(oper){ case '*': multiply_frac(num1, den1, num2, den2, ansnum, ansden); break; case '/': divide_frac(num1, den1, num2, den2, ansnum, ansden); break; case '+': add_frac(num1, den1, num2, den2, ansnum, ansden); break; case '-': subtract_frac(num1, den1, num2, den2, ansnum, ansden); break; } if(oper != 'x'){ expand(ansnum, ansden, whole); reduce(ansnum, ansden); output(num1, den1, num2, den2, ansnum, ansden, whole, oper); } } while(oper != 'x'); return 0; }