Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I

I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close.

// // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include using namespace std; void print_fraction(const Fraction& f) { cout << "print_fraction: " << f.getNum() << "/" << f.getDen() << endl; } int main() { Fraction x(2,3); Fraction y(6,-2); cout << x << endl; cout << y << endl; cin >> y; cout << y << endl; print_fraction(y); Fraction z = x + y; cout << x << " + " << y << " = " << z << endl; } 

// // calculator.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include #include using namespace std; int main() { Fraction x,y; char op; try { cin >> x; cin >> op; while ( cin && ( op == '+' || op == '-' ) ) { cin >> y; if ( op == '+' ) x = x + y; else x = x - y; cin >> op; } cout << x << endl; } catch ( invalid_argument& e ) { cout << "Error: " << e.what() << endl; } } 

//MYFILES

//fraction.cpp Fraction::Fraction() { num = 0; dem = 1; } Fraction::Fraction(int a, int b){ num = a; dem = b; } int Fraction::getNum(){ return num; } int Fraction::getDen(){ return den; } void Fraction::setNum(int a) { num = a; } void Fraction::setDen(int b) { den = b; } Fraction Fraction ::operator+( const Fraction & rightFraction ) { Fraction temp; temp.num = ( num * rightFraction.den ) + ( den * rightFraction.num ); temp.den = den * rightFraction.den; return temp; } // end function operator+ Fraction Fraction ::operator-( const Fraction & rightFraction ) { Fraction temp; temp.num = ( num * rightFraction.den ) - ( den * rightFraction.num ); temp.den = den * rightFraction.den; return temp; } // end function operator- ostream& operator<<(ostream& os, const Fraction& f) { os << f.getNum() << "/" << f.getDen(); // 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 throw "Error: divide by zero "; } while(b==0); f = Fraction(t, b); // set the fraction return is; } //Header file

#indef

#def

class Fraction{ public: Fraction(); Fraction(int, int); getNum(); getDen(); setNum(int); setDen(int); friend operator-( const Fraction & rightFraction ); friend operator+( const Fraction & rightFraction ); friend ostream& operator<<(ostream& os, const Fraction& f); friend istream& operator>>(istream& is, Fraction& f);

private: int num; int den; };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Information Systems 1 International Baltic Conference Dbandis 2020 Tallinn Estonia June 19 2020 Proceedings

Authors: Tarmo Robal ,Hele-Mai Haav ,Jaan Penjam ,Raimundas Matulevicius

1st Edition

303057671X, 978-3030576714

More Books

Students also viewed these Databases questions

Question

Understanding Group Affiliations?

Answered: 1 week ago

Question

What is liquidation ?

Answered: 1 week ago

Question

Explain the different types of Mergers.

Answered: 1 week ago

Question

What is dividend payout ratio ?

Answered: 1 week ago