Question

This is a C++ Program!!! Define a class called Rational that would represent rational numbers. Write...

This is a C++ Program!!!
Define a class called Rational that would represent rational numbers.
Write a constructor that would accept two integers and initialize the numerator
and denominator of the object to their values. Also include a default
constructor that would initialize the numerator to 0 and denominator to 1.
Implement the member functions add, sub, mul, div, less, and equal to carry out
the operations +, -, *, /, <, and == (i.e., a + b would be a.add(b)).
Provide member functions input and output that would allow the user to input
a rational number from standard input and output the rational number to standard
output.
Write a test program that would let the user input two rational numbers and
then print their sum, difference, product, quotient, and whether the first
rational number is less than, equal to, or greater than the second.
Note: let any sign be carried by the numerator; keep the denominator
positive.

I want the same output as show:

Enter:2/3·5/7 this is what the you enter.
two rational numbers:2/3 + 5/7·= 29/21
2/3 - 5/7 = -1/21
2/3 * 5/7 = 10/21
2/3 / 5/7 = 14/15
2/3 is less than 5/7
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

Rational.h

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>
using namespace std;

class Rational {
friend Rational operator+(const Rational& left, const Rational& right);
friend Rational operator-(const Rational& left, const Rational& right);
friend Rational operator*(const Rational& left, const Rational& right);
friend Rational operator/(const Rational& left, const Rational& right);
  
friend bool operator<(const Rational& left, const Rational& right);
  
  
friend ostream& operator<<(ostream& out, const Rational& obj);
friend istream& operator>>(istream& in, Rational& obj);
  
public:
Rational();
Rational(double x);
Rational(int numerator_, int denominator_ = 1);
  
  
void setNumerator(int numerator_);
int getNumerator() const;
void setDenominator(int denominator_);
int getDenominator() const;

private:
int numerator;
int denominator;
void simplify();
};
#endif

Rational.cpp

#include"Rational.h"
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <limits.h>
using namespace std;

int absInt(int x) {
if (x >= 0) {
return x;
}
else {
return -x;
}
}

void getFactors (int num, vector<int>& factorSet) {
if (num != 1) {
factorSet.push_back(num);
}
for (int i = 2; i <= sqrt( static_cast<double>(num) ); i++) {
if (num%i == 0) {
factorSet.push_back(i);
factorSet.push_back(num/i);
}
}
}
void simplifyFun(int& a, int& b) {
int tempN = a;
int tempD = b;
int small, temp;
vector<int> factorSet;
if (tempN == tempD) {
a = 1;
b = 1;
return ;
}
else if (tempN == -tempD) {
a = -1;
b = 1;
return ;
}
else if (tempN == 0) {
b = 1;
return ;
}


if (absInt(tempN) < absInt(tempD)) {
small = absInt(tempN);
}
else {
small = absInt(tempD);
}

getFactors(small, factorSet);
for (int i = 0; i < factorSet.size(); i++) {
temp = factorSet[i];
while (tempN%temp == 0 && tempD%temp == 0) {
tempN /= temp;
tempD /= temp;
}
}
a = tempN;
b = tempD;
}

//friend functions definitions
Rational operator+(const Rational& left, const Rational& right) {
Rational temp;
int tempLD = left.getDenominator();
int tempRD = right.getDenominator();
simplifyFun(tempLD, tempRD);
temp.setDenominator(left.getDenominator()*tempRD);
temp.setNumerator(left.getNumerator()*tempRD+right.getNumerator()*tempLD);
temp.simplify();
return temp;
}

Rational operator-(const Rational& left, const Rational& right) {
return left+((-1)*right);
}

Rational operator*(const Rational& left, const Rational& right) {
Rational temp;
Rational temp_2(right.getNumerator(),left.getDenominator());
Rational temp_3(left.getNumerator(),right.getDenominator());
int a = temp_2.getDenominator();
int b = temp_2.getNumerator();
int c = temp_3.getDenominator();
int d = temp_3.getNumerator();
temp.setNumerator(b*d);
temp.setDenominator(a*c);
return temp;
}

Rational operator/(const Rational& left, const Rational& right) {
Rational temp_1(left.getNumerator(),left.getDenominator());
Rational temp_2(right.getDenominator(),right.getNumerator());
return temp_1*temp_2;
}

istream& operator>>(istream& in, Rational& obj) {
string inputstr;
int num = 0;
int sign = 1;
bool slashExist = false;
bool dotExist = false;
bool validInput = true;
int virtualDenominator = 1;
cin >> inputstr;
for (int i = 0; i < inputstr.size(); i++) {
char temp = inputstr[i];
if (temp == '.') {
if (dotExist == false && slashExist == false && i != 0) {
dotExist = true;
}
else {
validInput = false;
break;
}
}
else if (temp == '/') {
if (dotExist == false && slashExist == false && i != 0) {
slashExist = true;
obj.setNumerator(sign*num);
num = 0;
sign = 1;
}
else {
validInput = false;
break;
}
}
else if (temp == '-') {
if (i == 0){
sign = -sign;
}
else if (inputstr[i-1] == '/') {
sign = -sign;
}
else {
validInput = false;
break;
}
}
else if (temp <= '9' && temp >= '0') {
if (dotExist) {
if (virtualDenominator > INT_MAX/10) {
cout << "this frational is too long to handle.";
validInput = false;
break;
}
else {
virtualDenominator *= 10;
}
}
if (num > INT_MAX/10) {
cout << "this number is too long to handle.";
validInput = false;
break;
}
num *= 10;
num += inputstr[i]-'0';
}
else {
validInput = false;
break;
}
}
  
if (validInput == false) {
obj.setNumerator(0);
obj.setDenominator(1);
cout << "Input is not valid! The whole set to 0" << endl;
}
else {
if (slashExist == true) {
obj.setDenominator(sign*num);
}
else if (dotExist) {
obj.setNumerator(sign*num);
obj.setDenominator(virtualDenominator);
}
else {
obj.setNumerator(sign*num);
obj.setDenominator(1);
}
}
obj.simplify();
return in;
}

//member function definition
Rational::Rational() {
setNumerator(0);
setDenominator(1);
}

Rational::Rational(double x) {
int i = 1;
while (x*i-static_cast<int>(x*i) != 0) {
if (i > INT_MAX/10) {
cout << "this frational number : " << x << " can not be transfer to rational number, it's too long, now set it 0." << endl;
setNumerator(0);
setDenominator(1);
return ;
}
else {
i *= 10;
}
}
setNumerator(x*i);
setDenominator(i);
simplify();
}

Rational::Rational(int numerator_, int denominator_) {
setNumerator(numerator_);
setDenominator(denominator_);
simplify();
}
void Rational::setNumerator(int numerator_) {
numerator = numerator_;
}

int Rational::getNumerator() const {
return numerator;
}

void Rational::setDenominator(int denominator_) {
if (denominator_ == 0) {
denominator = 1;
numerator = 0;
cout << "Denominator is 0! Not good! THe whole is set to 0." << endl;
}
else {
denominator = denominator_;
}
}

int Rational::getDenominator() const {
return denominator;
}
void Rational::simplify() {
int tempN = numerator;
int tempD = denominator;
simplifyFun(tempN,tempD);
setNumerator(tempN);
setDenominator(tempD);
}
ostream& operator<<(ostream& out, const Rational& obj) {
out << obj.numerator;
if (obj.numerator != 0 && obj.denominator != 1) {
out << "/" << obj.denominator;
}
return out;
}
bool operator<(const Rational& left, const Rational& right) {
int lside = left.getNumerator()*right.getDenominator();
int rside = left.getDenominator()*right.getNumerator();
return (lside < rside);
}

test.cpp

#include"Rational.h"

using namespace std;
int main()
{
   Rational r1,r2,r3;
   cout<<"Enter the first rational number: ";
   cin>>r1;
   cout<<"Enter the second rational number: ";
   cin>>r2;
   cout<<"\n\nTwo Rationla number are: "<<endl;
   cout<<r1<<endl;
   cout<<r2<<endl<<endl;

   cout<<r1<<" + "<<r2<<" = "<<(r1+r2)<<endl;
   cout<<r1<<" - "<<r2<<" = "<<(r1-r2)<<endl;
   cout<<r1<<" * "<<r2<<" = "<<(r1*r2)<<endl;
   cout<<r1<<" / "<<r2<<" = "<<(r1/r2)<<endl;

   if(r1<r2)
       cout<<r1<<" is less than "<<r2<<endl;
   else
       cout<<r1<<" is greater than "<<r2<<endl;
   return 0;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
This is a C++ Program!!! Define a class called Rational that would represent rational numbers. Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two...

    Define a class for rational numbers. A rational number is a "ratio-nal" number, composed of two integers with division indicated. Requirement: - two member variables: (int) numerator and (int) denominator. - two constructors: one that takes in both numerator and denominator to construct a rational number, and one that takes in only numerator and initialize the denominator as 1. - accessor/modifier - member functions: add(), sub(), mul(), div(), and less(). Usage: to add rational num b and rational num a,...

  • C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...

    C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2, etc., we mean the everyday meaning of the fraction, not the integer division this expression could produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call...

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

  • Define a class for rational numbers. A rational number is a number that can be represented...

    Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 etc we mean the everyday meaning of the fraction, not the integer division this expression would produce in a C++ program). Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class rational Num...

  • Make a new class called Rational to represent rational numbers in C++ language. Use a long...

    Make a new class called Rational to represent rational numbers in C++ language. Use a long to store each part (numerator and denominator). Be sure your main function fully test the class. 1. Create a helper function called gcd to calculate the greatest common divisor for the two given parameters. See Euclid’s algorithm (use google.com). Use this function to always store rationals in “lowest terms”. 2. Create a default, one argument and two argument constructor. For the two argument constructor,...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational class) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integers variables to represent the private data of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case are no initializers are provided and should store the fraction in reduced form. For example, the fraction 3/6 would be...

  • C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.]...

    C++ 1st) [Note: This assignment is adapted from programming project #7 in Savitch, Chapter 10, p.616.] Write a rational number class. Recall that a rational number is a ratio-nal number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • C++...please help follow exact steps output needs to be the exact same Create a class called...

    C++...please help follow exact steps output needs to be the exact same Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the elass the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT