Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ PROGRAMMING Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What

C++ PROGRAMMING

Hi! I have to make a program that adds fractions and simplifies them. I feel like I know how to write that. What I'm having trouble with is implementing two files the professer gave us. I would appreicate any help in understanding their purpose as in if Im supposed to take information from those files or give it information. Thank you!

I have attatched the homework instructions and the two files given.

Implementation

The main program, called calculator.cpp , is provided. 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 . 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. An example of a C implementation of this algorithm is provided on the web site web.cs.ucdavis.edu/~fgygi/ecs40 in the example programs of Lecture 1. The operators '+', '-' and '=' must be overloaded and defined. Member functions getNum() and getDen() should be implemented, returning the (reduced) numerator and denominator of the fraction. It should be possible to use the class Fraction in the program useFraction.cpp which #includes the header Fraction.h. The program useFraction.cpp is provided on the web site web.cs.ucdavis.edu/~fgygi/ecs40. You should not modify that program.

// // 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; } }

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

which of the following statements is not true about eye contact

Answered: 1 week ago

Question

What are the determinants of cash cycle ? Explain

Answered: 1 week ago