Question: C++ code, please include screenshot, thank you! Rational fractions are of the form a / b , in which a and b are integers and
C++ code, please include screenshot, thank you!
Rational fractions are of the form a / b, in which a and b are integers and b 0. In this exercise, by fractions we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations on fractions are defined by the following rules:
a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b * c/d = ac/bd
(a/b) / (c/d) = ad/bc, in which c/d 0
Fractions are compared as follows: a / b op c / d if ad op bc, in which op is any of the relational operations. For example, a / b < c / d if ad < bc.
Design a class fractionType that performs the arithmetic and relational operations on fractions. Overload the arithmetic and relational operators so that the appropriate symbols can be used to perform the operation. Also, overload the stream insertion and stream extraction operators for easy input and output.
Write the class fractionType, that performs operations on fractions. Among other things, test the following: Suppose x, y, and zare objects of type fractionType. If the input is 2/3, the statement:
cin >> x;
should store 2/3 in x. The statement:
cout << x + y << endl;
should output the value of x + y in fraction form. The statement:
z = x + y;
should store the sum of x and y in z in fraction form. Your answer need not be in the lowest terms.
fractionType.cpp
#include
// overload operator << ostream& operator << (ostream& os, const fractionType& fraction) { // write overloaded code here }
// overload operator >> istream& operator>> (istream& is, fractionType& fraction) { // write overloaded code here }
// overload operator == bool fractionType::operator==(fractionType rightFr) const { // write overloaded code here }
// overload operator != bool fractionType::operator!=(fractionType rightFr) const { // write overloaded code here }
// overload operator <= bool fractionType::operator<=(fractionType rightFr) const { // write overloaded code here }
// overload operator < bool fractionType::operator<(fractionType rightFr) const { // write overloaded code here }
// overload operator >= bool fractionType::operator>=(fractionType rightFr) const { // write overloaded code here }
// overload operator > bool fractionType::operator>(fractionType rightFr) const { // write overloaded code here }
// constructor fractionType::fractionType(int num, int deno) { numerator = num;
if (deno == 0) { cout << "denominator cannot be zero" << endl; denominator = 1; } else denominator = deno; }
void fractionType::setFraction(int num, int deno) { numerator = num;
if (deno == 0) { cout << "denominator cannot be zero" << endl; denominator = 1; } else denominator = deno; }
// overload the operator + fractionType fractionType::operator+(fractionType rightFr) { // write overloaded code here }
// overload the operator * fractionType fractionType::operator*(fractionType rightFr) { // write overloaded code here }
// overload operator - fractionType fractionType::operator-(fractionType rightFr) { // write overloaded code here }
// overload operator / fractionType fractionType::operator/(fractionType rightFr) { // write overloaded code here }
fractionType.h
#ifndef H_fraction
#define H_fraction
#include
using namespace std;
class fractionType
{
// overload stream insertion and extraction operators
friend ostream& operator<< (ostream&, const fractionType&);
friend istream& operator>> (istream&, fractionType&);
public:
void setFraction(int num, int den);
fractionType(int num = 0, int den = 1);
//Constructor
fractionType operator+(fractionType rightFr);
//overload +
fractionType operator*(fractionType rightFr);
//overload *
fractionType operator-(fractionType rightFr);
//overload -
fractionType operator/(fractionType rightFr);
//overload /
//overload relational operators
bool operator==(fractionType rightFr) const;
bool operator!=(fractionType rightFr) const;
bool operator<=(fractionType rightFr) const;
bool operator<(fractionType rightFr) const;
bool operator>=(fractionType rightFr) const;
bool operator>(fractionType rightFr) const;
private:
int numerator; //variable to store the numerator
int denominator; //variable to store the denominator
};
#endif
main.cpp
#include
using namespace std;
int main() { fractionType num1(5, 6); fractionType num2; fractionType num3;
cout << fixed; cout << showpoint; cout << setprecision(2);
cout << "Num1 = " << num1 << endl; cout << "Num2 = " << num2 << endl;
cout << "Enter the fraction in the form a / b: "; cin >> num2; cout << endl;
cout << "New value of num2 = " << num2 << endl;
num3 = num1 + num2;
cout << "Num3 = " << num3 << endl; cout << num1 << " + " << num2 << " = " << num1 + num2 << endl; cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
num3 = num1 - num2;
cout << "Num3 = " << num3 << endl; cout << num1 << " - " << num2 << " = " << num1 - num2 << endl; cout << "(" << num1 << ") / (" << num2 << ") = " << num1 / num2 << endl;
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
