Question

C++ A rational number is of the form a/b, where a and b are integers, and...

C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.Pointers aren't needed and it should be able to handle all of these examples and more. Its supposed to accept a rational number dynamically in the form of "a/b" and tell you the result.

Details:

  1. Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file.
  1. The class should read and display all rational numbers in the form a/b,
  2. The operations that should be implemented for the rational numbers are
  3. except when b is 1, then it should just display a
  4. or when b is 0, then it should display #div0

  5. Operator

    Example

    Result

    Addition

    3/9 + 1/7

    10/21

    Subtraction

    3/9*1/7

    4/21

    Multiplication

    3/8 * 1/6

    1/16

    Division

    3/8 / 1/6

    9/4

    Invert

    2/4 I

    4/2

    Mixed fraction

    8/3 M

    2 and 2/3

    Reduce

    18/24 R

    3/4

    Less than

    1/6 < 3/8

    True

    Less than or equal to

    1/6 <= 3/8

    True

    Greater Than

    1/6 > 3/8

    False

    Greater than or equal

    1/6 >= 3/8

    False

    Equal to

    3/8 == 9/24

    True

  6. The arithmetic operators must be overloaded to work with the class
  7. The class must have
    1. At least 2 private member variables, numerator and denominator
    2. At least 4 public member functions
      1. getNu(), getDe(), setNu(value), setDe(value)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

RationalNumber.h

#ifndef RATIONALNUMBER

#define RATIONALNUMBER

#include <iostream>

using namespace std;

class RationalNum {

friend RationalNum operator+(const RationalNum& left, const RationalNum& right);

friend RationalNum operator-(const RationalNum& left, const RationalNum& right);

friend RationalNum operator*(const RationalNum& left, const RationalNum& right);

friend RationalNum operator/(const RationalNum& left, const RationalNum& right);

friend bool operator==(const RationalNum& left, const RationalNum& right);

friend bool operator!=(const RationalNum& left, const RationalNum& right);

friend bool operator<(const RationalNum& left, const RationalNum& right);

friend bool operator>(const RationalNum& left, const RationalNum& right);

friend bool operator<=(const RationalNum& left, const RationalNum& right);

friend bool operator>=(const RationalNum& left, const RationalNum& right);

friend ostream& operator<<(ostream& out, const RationalNum& obj);

friend istream& operator>>(istream& in, RationalNum& obj);

  

public:

RationalNum();

RationalNum(double x);

RationalNum(int numerator_, int denominator_ = 1);

RationalNum& operator=(const RationalNum& obj);

RationalNum& operator+=(const RationalNum& obj);

RationalNum& operator-=(const RationalNum& obj);

RationalNum& operator*=(const RationalNum& obj);

  

void invert();

void setNumerator(int numerator_);

void Mixedfraction();

int getNumerator() const;

void setDenominator(int denominator_);

int getDenominator() const;

private:

int numerator;

int denominator;

void simplify();

};

#endif

RationalNumber.cpp

#include "RationalNumber.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

RationalNum operator+(const RationalNum& left, const RationalNum& right) {

RationalNum 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;

}

RationalNum operator-(const RationalNum& left, const RationalNum& right) {

return left+((-1)*right);

}

RationalNum operator*(const RationalNum& left, const RationalNum& right) {

RationalNum temp;

RationalNum temp_2(right.getNumerator(),left.getDenominator());

RationalNum 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;

}

RationalNum operator/(const RationalNum& left, const RationalNum& right) {

RationalNum temp_1(left.getNumerator(),left.getDenominator());

RationalNum temp_2(right.getDenominator(),right.getNumerator());

return temp_1*temp_2;

}

bool operator==(const RationalNum& left, const RationalNum& right) {

return (left.numerator == right.numerator && left.denominator == right.denominator);

}

bool operator!=(const RationalNum& left, const RationalNum& right) {

return !(left == right);

}

bool operator<(const RationalNum& left, const RationalNum& right) {

int lside = left.getNumerator()*right.getDenominator();

int rside = left.getDenominator()*right.getNumerator();

return (lside < rside);

}

bool operator>(const RationalNum& left, const RationalNum& right) {

int lside = left.getNumerator()*right.getDenominator();

int rside = left.getDenominator()*right.getNumerator();

return (lside > rside);

}

bool operator<=(const RationalNum& left, const RationalNum& right) {

return ( (left < right) || (left == right) );

}

bool operator>=(const RationalNum& left, const RationalNum& right) {

return ( (left > right) || (left == right) );

}

ostream& operator<<(ostream& out, const RationalNum& obj) {

out << obj.numerator;

if (obj.numerator != 0 && obj.denominator != 1) {

out << "/" << obj.denominator;

}

return out;

}

istream& operator>>(istream& in, RationalNum& 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

RationalNum::RationalNum() {

setNumerator(0);

setDenominator(1);

}

RationalNum::RationalNum(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();

}

RationalNum::RationalNum(int numerator_, int denominator_) {

setNumerator(numerator_);

setDenominator(denominator_);

simplify();

}

RationalNum& RationalNum::operator=(const RationalNum& obj) {

setNumerator(obj.getNumerator());

setDenominator(obj.getDenominator());

return *this;

}

RationalNum& RationalNum::operator+=(const RationalNum& obj) {

*this = *this+obj;

return *this;

}

RationalNum& RationalNum::operator-=(const RationalNum& obj) {

*this = *this-obj;

return *this;

}

RationalNum& RationalNum::operator*=(const RationalNum& obj) {

*this = *this*obj;

return *this;

}

void RationalNum::setNumerator(int numerator_) {

numerator = numerator_;

}

int RationalNum::getNumerator() const {

return numerator;

}

void RationalNum::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 RationalNum::getDenominator() const {

return denominator;

}

void RationalNum::invert()

{

RationalNum r(this->getDenominator(),this->getNumerator());

cout<<r;

}

void RationalNum::Mixedfraction()

{

int qau=this->numerator/this->denominator;

int rem=this->numerator%this->denominator;

cout<<qau<<" and "<<RationalNum(rem,this->denominator)<<endl;;

}

void RationalNum::simplify() {

int tempN = numerator;

int tempD = denominator;

simplifyFun(tempN,tempD);

setNumerator(tempN);

setDenominator(tempD);

}

test.cpp

#include"RationalNumber.h"

using namespace std;

int main()

{

RationalNum r1(3,9);

RationalNum r2(1,7);

cout<<r1<<" + "<<r2<<" = " << r1 + r2<<endl<<endl;;

cout<<r1<<" - "<<r2<<" = " << r1 + r2<<endl<<endl;

RationalNum r3(3,8);

RationalNum r4(1,6);

cout<<r3<<" * "<<r4<<" = " << r3 * r4<<endl<<endl;;

cout<<r3<<" / "<<r4<<" = " << r3 / r4<<endl<<endl;;

RationalNum r8(8,3);

cout<<r8<<" M = ";

r8.Mixedfraction();

cout<<"\n";

cout<<r8<<"I ";r8.invert();

cout<<endl;

cout<<"\n";

RationalNum r5(1,6);

RationalNum r6(3,8);

if(r5<r6)

cout<<r5<<"<"<<r6<<":True"<<endl<<endl;

else

cout<<r5<<"<"<<r6<<":False"<<endl<<endl;

if(r5<=r6)

cout<<r5<<"<="<<r6<<":True"<<endl<<endl;

else

cout<<r5<<"<="<<r6<<":False"<<endl<<endl;

if(r5>r6)

cout<<r5<<">"<<r6<<":True"<<endl<<endl;

else

cout<<r5<<">"<<r6<<":False"<<endl<<endl;

if(r5>=r6)

cout<<r5<<">="<<r6<<":True"<<endl<<endl;

else

cout<<r5<<">="<<r6<<":False"<<endl<<endl;

RationalNum r7(9,24);

if(r3==r7)

cout<<r3<<"=="<<r7<<":True"<<endl<<endl;

else

cout<<r3<<"=="<<r7<<":Flase"<<endl<<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:
C++ A rational number is of the form a/b, where a and b are integers, and...
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
  • C++ A rational number is of the form a/b, where a and b are integers, and...

    C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.Pointers aren't needed and it should be able to handle all of these examples and more. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of...

  • C++ A rational number is of the form a/b, where a and b are integers, and...

    C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file. The class should read and display all rational numbers...

  • General Description: A rational number is of the form a/b, where a and b are integers,...

    General Description: A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file. The class should read and display all rational...

  • 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...

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • Write Each rational number in the form a/b where a and b are integers and b is not equal to 0

    Write Each rational number in the form a/b where a and b are integers and b is not equal to 0.1) 5 1/32) 733) -0.21

  • In mathematics, a rational number is any number that can be expressed as the quotient or...

    In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, p and q, with the denominator q not equal to zero. Since q may be equal to 1, every integer is a rational number. Define a class that can represent for a rational number. Use the class in a C++ program that can perform all of the following operations with any two valid rational numbers entered at the keyboard...

  • 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...

  • 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,...

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
ADVERTISEMENT