Details of this Paper

Create a four-function fraction calculator

Description

solution


Question

Question;Create a four-function fraction calculator. Here are the formulas for the four arithmetic operations applied to fractions:Math: calculator input = internal operationAddition: a/b + c/d = (a*d + b*c) / (b*d)Subtraction: a/b - c/d = (a*d - b*c) / (b*d)Multiplication: a/b * c/d = (a*c) / (b*d)Division: a/b / c/d = (a*d) / (b*c)The left expression is the user input and the right expression actual overloaded math operation. The fractional number e.g. a/b, is represented by having the slash "/" sign in between the numerator and denominator number with NO space in between. The math operation is using the infix expression, e.g. x * y expression is written with the operator * in between the operand x and y.The user should type the first fraction, an operator, and a second fraction. The program should then display the result and ask whether the user wants to continue.Create the frac class with the following interface definition:private:long num, //numeratorlong den, //denomenatorpublic:frac() //constructor (no args){ num=0, den=1, }frac(long d) //constructor (one arg){ num=d, den=1, }frac(long n, long d) //constructor (two args){ num = n, den = d, }frac operator = (frac&), // assignment operatorfrac operator + (frac&), //arithmetic operatorsfrac operator - (frac&),frac operator * (frac&),frac operator / (frac&),bool operator == (frac&), //logical operatorsbool operator != (frac&),void outputfrac(), //user interface functionsvoid inputfrac(),frac lowterms(frac&),Also, make sure the frac class has the necessary mutators and accesors to support the application!Implementationscreate the frac.cpp and complete the defition of all method functions,use the sample code from the frac_oo.cpp,use overloaded operators for addition, subtraction, multiplication, and division. Also overload the == and != comparison operators, anduse them to exit from the loop if the user enters 0/1, 0/1 for the values of the two input frac class numbers.The finished application is required to apply the lowterms() function so that it returns the value of its argument reduced to lowest terms. This makes it more useful in the arithmetic functions, where it can be applied just before the answer is returned.___________________________________________________Starter Kit is provided to jump start your development.frac_pp.cpp: an example procedural program with identical functionality.frac_oo.cpp: an example code segment for main() application to interact with your class frac.The user interface provided in the frac_oo.cpp is expecting the user to enter one infix +-*/ operation expression, i.e. x * y, or two operands with the operator in between. A white space is required between operand and operator.User Interface Examples:Enter fraction, operator, fractionform 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/4 + 3/8You have entered: 3/4 + 3/89/8Enter fraction, operator, fractionform 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/4 - 3/8You have entered: 3/4 - 3/83/8Enter fraction, operator, fractionform 3/4 + 3/8 (or 0/1 + 0/1 to exit): 7/10 * 3/5You have entered: 7/10 * 3/521/50Enter fraction, operator, fractionform 3/4 + 3/8 (or 0/1 + 0/1 to exit): 7/10 / 3/5You have entered: 7/10 / 3/51/2Submit For multiple files project, please zip all files in your assignment folder in a.zip file and submit this frac.zip file to the dropbox here.zip frac.cpp and frac.h in one frac.zip file and submit to dropbox.Universal Requirement all assignments for this course:All submitted files shall contain the following information on top of the files (header of the C++, h file):frac_pp.cpp is#include #include // labs()using namespace std,struct frac{int num,int den,},frac inputfrac() { //inputchar dummychar,frac f,while(1) {cin >> f.num, //numeratorcin >> dummychar, //'/' charactercin >> f.den, //denominatorif(f.den != 0) break, //no problem, so exit loopcout << "Denominator cannot be zero. Try again: ",}return f,}frac lowterms(frac a) { //arg reduced to lowest termsfrac t, //temporary fractionlong tlong, gcd,t.num = labs(a.num), //use non-negative copiest.den = labs(a.den),if(t.num!=0 && t.den==0) //check for n/0{ cout << "Illegal fraction: division by 0", exit(1), }else if(t.num==0) //check for 0/n{ t.num=0, t.den = 1, return(t), }//this 'while' loop finds the gcd of t.num and t.denwhile(t.num != 0) {if(t.num < t.den) //ensure numerator larger{ tlong=t.num, t.num=t.den, t.den=tlong, }t.num = t.num - t.den, //subtract them}gcd = t.den,t.num = a.num / gcd, //divide both num and den by gcdt.den = a.den / gcd, //to reduce frac to lowest termsreturn t,}int main(){// frac f1, f2, fans,frac f1, f2, fans,char op,do {cout << "nEnter fraction, operator, fraction",cout op,f2 = inputfrac(),cout << "You have entered: " << f1.num << "/" << f1.den<< " " << op << " " << f2.num << "/" << f2.den << endl,switch(op) {case '+':fans.num = f1.num * f2.den + f2.num * f1.den,fans.den = f1.den * f2.den,break,case '-':fans.num = f1.num * f2.den - f2.num * f1.den,fans.den = f1.den * f2.den,break,case '*':fans.num = f1.num * f2.num,fans.den = f1.den * f2.den,break,case '/':fans.num = f1.num * f2.den,fans.den = f1.den * f1.num,break,default:cout << "No such operator",} //end switchfans = lowterms(fans),cout << fans.num << "/" << fans.den << endl,} while(f1.num != 0 || f2.num != 0),cout << endl,return 0,}frac_oo.cpp, isint main(){frac f1, f2, fans,char op,do {cout << "nEnter fraction, operator, fraction",cout op,f2.inputfrac(),switch(op) {case '+':fans = f1 + f2,fans.outputfrac(),break,case '-':fans = f1 - f2,fans.outputfrac(),break,case '*':fans = f1 * f2,fans.outputfrac(),break,case '/':fans = f1 / f2,fans.outputfrac(),break,default:cout << "No such operator",} //end switch} while(f1 != frac(0/1) || f2 != frac(0,1)),cout << endl,return 0,}frac__oo__starter.cpp is// File: frac_oo.cpp// Name:// DVC ID: 1234567// IDE: pocketcpp, or CodeBlock// Please finish the missing method definitions for the frac class#include #include // labs()using namespace std,class frac, // Forward Declaration// Function Prototypes for Overloaded Stream Operatorsostream &operator (istream &&, frac &);class frac {//private:long num,long den,public:// constructorsfrac() {num = 0,den = 1,}frac(long n, long d) {num = n,den = d,}frac(const frac &&f) {num = f.num;den = f.den,}// accessors and mutatorslong getNum() {return num,}long getDen() {return den,}void setNum(long n) {num = n,}void setDen(long d) {den = d,}// overload operatorfrac operator=(const frac &&f) {num = f.num;den = f.den,}// math operatorsfrac operator+(frac &&f);frac operator-(frac &&f);frac operator*(frac &&f);frac operator/(frac &&f);// logic operatorsbool operator==(frac &&f);bool operator!=(frac f),// auxiliary methodsfrac inputfrac(),void outputfrac() {std::cout << ' ' << num << '/' << den num, //numeratorcin >> dummychar, //'/' charactercin >> den, //denominatorif(den != 0) break, //no problem, so exit loopcout << "Denominator cannot be zero. Try again: ",}}frac frac::lowterms(frac &a) { //arg reduced to lowest termsfrac t, //temporary fractionlong tlong, gcd,t.num = labs(a.num), //use non-negative copiest.den = labs(a.den),if(t.num!=0 && t.den==0) //check for n/0{ cout << "Illegal fraction: division by 0", exit(1), }else if(t.num==0) //check for 0/n{ t.num=0, t.den = 1, return(t), }//this 'while' loop finds the gcd of t.num and t.denwhile(t.num != 0) {if(t.num < t.den) //ensure numerator larger{ tlong=t.num, t.num=t.den, t.den=tlong, }t.num = t.num - t.den, //subtract them}gcd = t.den,t.num = a.num / gcd, //divide both num and den by gcdt.den = a.den / gcd, //to reduce frac to lowest termsreturn t,}int main(){frac f1, f2, fans,char op,do {cout << "nEnter fraction, operator, fraction",cout op,f2.inputfrac(),switch(op) {case '+':fans = f1 + f2,fans.outputfrac(),break,case '-':fans = f1 - f2,fans.outputfrac(),break,case '*':fans = f1 * f2,fans.outputfrac(),break,case '/':fans = f1 / f2,fans.outputfrac(),break,default:cout

 

Paper#36833 | Written in 18-Jul-2015

Price : $37
SiteLock