Create a four-function fraction calculator

Assignment Help Basic Computer Science
Reference no: EM13215234

Create a four-function fraction calculator. Here are the formulas for the four arithmetic operations applied to fractions:

Math: calculator input = internal operation
Addition: 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; //numerator
long den; //denomenator
public:
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 operator
frac operator + (frac&); //arithmetic operators
frac operator - (frac&);
frac operator * (frac&);
frac operator / (frac&);
bool operator == (frac&); //logical operators
bool operator != (frac&);
void outputfrac(); //user interface functions
void inputfrac();
frac lowterms(frac&);

Also, make sure the frac class has the necessary mutators and accesors to support the application!
Implementations

create 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, and
use 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, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/4 + 3/8
You have entered: 3/4 + 3/8
9/8

Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 3/4 - 3/8
You have entered: 3/4 - 3/8
3/8

Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 7/10 * 3/5
You have entered: 7/10 * 3/5
21/50

Enter fraction, operator, fraction
form 3/4 + 3/8 (or 0/1 + 0/1 to exit): 7/10 / 3/5
You have entered: 7/10 / 3/5
1/2
Submit 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 <iostream>
#include <cstdlib> // labs()
using namespace std;

struct frac{
int num;
int den;
};

frac inputfrac() { //input
char dummychar;
frac f;
while(1) {
cin >> f.num; //numerator
cin >> dummychar; //'/' character
cin >> f.den; //denominator
if(f.den != 0) break; //no problem, so exit loop
cout << "Denominator cannot be zero. Try again: ";
}
return f;
}

frac lowterms(frac a) { //arg reduced to lowest terms
frac t; //temporary fraction
long tlong, gcd;

t.num = labs(a.num); //use non-negative copies
t.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.den
while(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 gcd
t.den = a.den / gcd; //to reduce frac to lowest terms
return t;
}

int main()
{
// frac f1, f2, fans;
frac f1, f2, fans;
char op;

do {
cout << "nEnter fraction, operator, fraction";
cout << "nform 3/4 + 3/8 (or 0/1 + 0/1 to exit): ";
f1 = inputfrac();
cin >> 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 switch

fans = lowterms(fans);

cout << fans.num << "/" << fans.den << endl;
} while( f1.num != 0 || f2.num != 0 );
cout << endl;
return 0;
}


frac_oo.cpp , is

int main()
{
frac f1, f2, fans;
char op;

do {
cout << "nEnter fraction, operator, fraction";
cout << "nform 3/4 + 3/8 (or 0/1 + 0/1 to exit): ";
f1.inputfrac();
cin >> 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 <iostream>
#include <cstdlib> // labs()

using namespace std;

class frac; // Forward Declaration

// Function Prototypes for Overloaded Stream Operators
ostream &operator << (ostream &, const frac &);
istream &operator >> (istream &, frac &);

class frac {
//private:
long num;
long den;

public:
// constructors
frac() {
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 mutators
long getNum() {return num;}
long getDen() {return den;}
void setNum(long n) {num = n;}
void setDen(long d) {den = d;}

// overload operator
frac operator=(const frac &f) {
num = f.num;
den = f.den;
}

// math operators
frac operator+(frac &f);
frac operator-(frac &f);
frac operator*(frac &f);
frac operator/(frac &f);

// logic operators
bool operator==(frac &f);
bool operator!=(frac f);


// auxiliary methods
frac inputfrac();
void outputfrac() {std::cout << ' ' << num << '/' << den << ' ';}
frac lowterms(frac &a);

};


frac frac::inputfrac() { //input
char dummychar;
while(1) {
cin >> num; //numerator
cin >> dummychar; //'/' character
cin >> den; //denominator
if(den != 0) break; //no problem, so exit loop
cout << "Denominator cannot be zero. Try again: ";
}
}

frac frac::lowterms(frac &a) { //arg reduced to lowest terms
frac t; //temporary fraction
long tlong, gcd;

t.num = labs(a.num); //use non-negative copies
t.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.den
while(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 gcd
t.den = a.den / gcd; //to reduce frac to lowest terms
return t;
}

int main()
{
frac f1, f2, fans;
char op;

do {
cout << "nEnter fraction, operator, fraction";
cout << "nform 3/4 + 3/8 (or 0/1 + 0/1 to exit): ";
f1.inputfrac();
cin >> 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;
}

 

Reference no: EM13215234

Questions Cloud

Add an overloaded assignment operator : Add an overloaded assignment operator, a copy constructor to the Cube class, and a printCube member function in the attached lab6_ex2_copy_operator_starter.cpp. This starter is incomplete, you have to fill the right stuff in the blank in order to ..
Develope a good business plan : You have a product in mind you want to manufacture. You have also developed a good business plan and are sure you will have no problem with financing.
Design a class numbers : Design a class Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number.
Apply the dynamic programming algorithm : Apply the dynamic programming algorithm to find all the solutions to the change-making problem for the denominations 1, 3, 5 and the amount n = 9
Create a four-function fraction calculator : Create a four-function fraction calculator. Here are the formulas for the four arithmetic operations applied to fractions.
Tolerate or even encourage the abuse tof the children : Should american companies refuse to do business in countries that tolerate or even encourage the abuse tof the children? explain
Why bill is obligated to furnish over one-half of the cost : Jane and Bill have lived in a home Bill inherited from his parents. Their son Jim lives with them. Bill and Jane obtain a divorce during the current year. Under the terms of the divorce, Jane receives possession of the home for a period of five ye..
State what will be the dollar value of the management team''s : What will be the dollar value of the management team's original $2 million equity investment at the time of the liquidity event?
What is the implied cycle service level chosen by company : What is the order size, What is the reorder point if a 90% cycle service level is desired and what is the implied cycle service level chosen by the company?

Reviews

Write a Review

Basic Computer Science Questions & Answers

  Why the sdlc is adequate to develop any system

Why the SDLC is adequate to develop any system. Explain by giving at least two examples of systems, such as client-management systems and decision-support systems.

  Prove that the omega notation is reflexive and transitive

Prove that the omega notation is reflexive and transitive: for any functions f, g, h : N -> R?0, 1. f(n) ? ? (f(n)) 2. if f(n) ? ? (g(n)) Use the duality Rule!

  Physical characteristics and moisture content

Explain how the different physical characteristics and moisture content of soil lead to the different angles of repose, swell, and shrinkage factors. Explain how these factors affect excavation operations and costs.

  Specific design criteria

Give two languages that are in direct conflict with each other. Provide examples of these conflicts as either programming examples (features allowed or not allowed) or program model, (environment). Compare and contrast the terms readability and write..

  Create an html5 document that contains an unordered list

Create an HTML5 document that contains an unordered list with links to the following examples headings, email, images as hyperlink these are from textbook, special characters, tables, HTML5 forms, and internal links to be included.

  Write a c++ program that calculates distance of a golf ball

write a C++ program that calculates the distance of a golf ball fallen in with each one-seconds interval drop from airplane calculate the distance in the current time Interval (ft) and total distance the golf ball falls at the end of each interval..

  Write a java method which takes an integer

Write a Java method which takes an integer array parameter and locates the minimum value in the array and outputs that value using System.out.printf. Use an enhanced for loop to process the array. You only need one loop to do this!

  Hash function h is used and the signature

Suppose a hash function h is used and the signature must be valid for h(m) instead of m. Explain how  this scheme protects against existential forgery

  Explaining data visualization form of business intelligence

Is data visualization a form of business intelligence? Describe why or why not? What security issues are related with data visualization?

  How it utilizes it and did the designer or programer choose

1-direct manipulation 2-menu selection 3-form fill-in 4-command language 5-natural language For each one,provide a software example you've used that utilizes it, and how it utilizes it and did the designer or the programer choose the correct style..

  What operations can be used on pointer variables

In C++, what operations can be used on pointer variables? Why use these operations?

  Create a priority queue class called priqueue

Create a priority queue class called priQueue derived from the vector class. construct as a template class. priority queue needs to be based on a value from 1 to 10, any element outside the range of 1 to 10 should be given a value of 5.......

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd