Create a simple fraction calculator that can add and subtract any number of frac
ID: 3565963 • Letter: C
Question
Create a simple fraction calculator that can add and subtract any number of fractions and writes
the answer as a reduced fraction.
Your program will read input from stdin and write output on stdout.
A fraction is represented as the sequence:
a/b
where a and b are integers and any amount of white space characters ' ' (including none) can
separate a from '/' and '/' from b.
Input consists of an expression made of fractions separated by the operators '+' or '-'.
The number of fractions in the expression is arbitrary. Each of the following 6 lines is an
example of valid input expression:
1/2 + 3/4
1/2-5/7+3/5
355/113
3 /9- 21 / -7
4/7-5/-8
-2/-3 +7 /5
Note that the numerator and/or denominator of a fraction given as input may be negative.
The input will consist of a single expression on a single line.
The output should consist of a single, irreducible fraction written as
c/d
where c is a signed integer and d is a positive integer (i.e. the denominator cannot be negative in
the output). The numbers c and d should not have any common factors (apart from 1).
Error processing and special cases
You can expect the input to consist of complete expressions, i.e. there will be no incomplete or
missing fractions in the input. Fractions with a zero denominator may appear in input, and must
cause the following error message to be printed on stdout:Error: division by zero
with no other output.
If the answer is an integer, the program should only print that integer and not a fraction
Example: input= 3/2 + 4/8 , output = 2
Implementation
The main program, called calculator.cpp , is provided at the bottom
Your project must include a class
Fraction that encapsulates all functions related to the processing of fractions. The >> and <<
operators must be overloaded and used to read and write fractions. The presence of a zero
denominator in input should be handled by throwing an exception of type
invalid_argument defined in <stdexcept>. The class Fraction must be defined in a
source file Fraction.cpp and declared in a header file Fraction.h. The implementation
of the member functions should be defined in the file Fraction.cpp. The class Fraction must
include a constructor Fraction::Fraction(int a, int b). The internal
representation of the fraction should be in reduced form, i.e. using a pair of numbers that have no
common factors. Constructors and other member functions must ensure that the fraction is
always reduced. Use Euclid's algorithm to simplify fractions to reduced form.. The operators '+', '-' and '=' must be overloaded and
defined.
It should be possible to use the class Fraction in the program useFraction.cpp
which #includes the header Fraction.h .
_______________________________________________________________________
Explanation / Answer
calculator.cpp
#include "Fraction.h"
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
Fraction x,y;
char op;
try
{
cin >> x; //reading first fraction //calling operator>>()
cin >> op; //reading if it's + or - //calling operator<<()
while ( cin && ( op == '+' || op == '-' ) )
{
cin >> y; //reading the second fraction
if ( op == '+' )
x = x + y; // operator +()
else
x = x - y; // operator -()
cin >> op;
}
cout << x << endl;
}
catch ( invalid_argument& e )
{
cout << "Error: " << e.what() << endl;
}
}
useFraction.cpp
#include "Fraction.h"
#include<iostream>
using namespace std;
int main()
{
Fraction x(2,3);
Fraction y(6,-2);
cout << x << endl;
cout << y << endl;
cin >> y;
cout << y << endl;
Fraction z = x + y;
cout << x << " + " << y << " = " << z << endl;
}
Fraction.h
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream> // for cout and cin
class Fraction
{
public:
Fraction();
Fraction (int a, int b);
void setNumerator(int);
void setDenominator(int);
int getNumerator() const;
int getDenominator() const;
Fraction operator+ (const Fraction& f2) const; // operator overloading for +
Fraction operator- (const Fraction& f2) const; // operator overloading for -
Fraction& operator= (const Fraction&);
friend std::istream& operator >> (std::istream& is, Fraction& f); // for cin
friend std::ostream& operator << (std::ostream& os, const Fraction& f); // for cout
private:
// Data members (attributes)
int Numerator; // private data field
int Denominator; // private data field
};
#endif
Fraction.cpp
#include <iostream>
#include "Fraction.h"
#include <stdexcept>
using namespace std;
Fraction::Fraction()
{ // in default constructor, we set the value as.
Numerator = 0;
Denominator = 1;
}
Fraction::Fraction(int a, int b)
{ // set the passed value "a" to Numerator and "b" to denominator
Numerator = a;
Denominator = b;
}
int Fraction::getNumerator () const
{
return Numerator;
}
int Fraction::getDenominator () const
{
return Denominator;
}
//set num and denom
void Fraction::setNumerator(int a)
{
Numerator = a;
}
void Fraction::setDenominator(int b)
{
Denominator = b;
}
ostream& operator<<(ostream& os, const Fraction& f)
{
os << f.getNumerator() << "/" << f.getDenominator(); // output in fraction format
return os;
}
istream& operator>>(istream& is, Fraction& f)
{
int t, b;
do
{
// Read the numerator first
is >> t;
char c;
is >> c;
if (c == '/') // If there is a slash, then read the next number
is >> b;
else //otherwise just "unget()" it and set to b=1
{
is.unget();
b = 1;
}
if (b==0) // if b came out to 0, fraction is not possible, so we loop back to ask user input again
cout<<"Error: divide by zero ";
} while(b==0);
f = Fraction(t, b); // set the fraction
return is;
}
Fraction Fraction::operator + (const Fraction& f) const
{// if we add (a/b + c/d) then it becomes (ad + bc) / bd, we apply this below
Fraction result(getNumerator() * f.getDenominator() + f.getNumerator() * getDenominator(),
getDenominator() * f.getDenominator());
return result;
}
Fraction Fraction::operator - (const Fraction& f) const
{// if we subtract (a/b - c/d) then it becomes (ad - bc) / bd, we apply this below
Fraction result(getNumerator() * f.getDenominator() - f.getNumerator() * getDenominator(),
getDenominator() * f.getDenominator());
return result;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.