Question

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 numbers in the form a/b,
except when b is 1, then it should just display a

The operations that should be implemented for the rational numbers are
Operator   Example   Result
Addition   3/8 + 1/6   13/24
Subtraction   3/8 – 1/6   5/24
Multiplication   3/8 * 1/6   1/16
Division   3/8 / 1/6   9/4
Invert   3/8 I   8/3
Mixed fraction   8/3 M   2 and 2/3
Reduce   6/8 R   3/4
Greatest Common Divisor   6/8 G   2
Lowest Common Denominator   1/6 L 3/8   24
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
      

You must have internal documentation:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output:

CAUsers win7\Desktop programs rational number RationalMain.exe Enter a Rational Number n/d3/8 Enter a Rational Number <n/d 1/

Programs:

1. RationalNumber?.h:

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
#include<iostream>
using namespace std;
//using std::ostream;
//class
class RationalNumber
{
   friend istream &operator>>(istream&, RationalNumber&);
   friend ostream &operator<<(ostream&, RationalNumber&);
public:
   RationalNumber(int,int);
   RationalNumber();
   void printRationalNumber();
   void operator=(RationalNumber);
   //arthmetic oparetors
   RationalNumber operator+(RationalNumber);
   RationalNumber operator-(RationalNumber);
   RationalNumber operator*(RationalNumber);
   RationalNumber operator/(RationalNumber);
   void I();
   void M();
   void R();
   int G();
   int L(RationalNumber&);
   //logical oparetors
   bool operator>(RationalNumber&);
   bool operator>=(RationalNumber&);
   bool operator<(RationalNumber&);
   bool operator<=(RationalNumber&);
   bool operator==(RationalNumber&);
   bool operator!=(RationalNumber&);
   void printRationalNumberAsDouble();
  
  
private:
   int numerator;
   int denominator;
};
#endif

2. RationalNumber.cpp :

#include <iostream>
#include <iomanip>
//using std::cout;
//using std::istream;
//using std::ostream;
//using std::endl;
using namespace std;
#include "RationalNumber.h"
RationalNumber::RationalNumber(int numerator, int denominator)
{
   this->numerator = numerator;
   this->denominator = denominator;
   if(denominator ==0)
   {
       this->denominator = 1;
   }// end if
  
}
RationalNumber RationalNumber::operator+(RationalNumber obj)
{
   //Addition 3/8 + 1/6 13/24
   int num=numerator*obj.denominator+obj.numerator*denominator;
int den=denominator*obj.denominator;
RationalNumber res(num,den);
res.R();
return res;

}
RationalNumber RationalNumber::operator-(RationalNumber obj)
{
   //Subtraction 3/8 - 1/6 5/24
   int num=numerator*obj.denominator-obj.numerator*denominator;
int den=denominator*obj.denominator;
  
RationalNumber res(num,den);
res.R();
return res;
}
RationalNumber RationalNumber::operator*(RationalNumber obj)
{
   //Multiplication 3/8 * 1/6 1/16
   int num=numerator*obj.numerator;
int den=denominator*obj.denominator;
RationalNumber res(num,den);
res.R();
return res;

}
RationalNumber RationalNumber::operator/(RationalNumber obj)
{
   //Division 3/8 / 1/6 9/4
   int num=numerator*obj.denominator;
int den=denominator*obj.numerator;
RationalNumber res(num,den);
res.R();
return res;
}
void RationalNumber::I()
{
   //Invert 3/8 I 8/3
   int num=numerator;
numerator=denominator;
denominator=num;
  
}
void RationalNumber::M()
{
   //Mixed fraction 8/3 M 2 and 2/3
   int f=numerator/denominator;
int r=numerator%denominator;
cout<<f<<" and "<<r<<"/"<<denominator<<endl;

}
void RationalNumber::R()
{
   //Reduce 6/8 R 3/4
   int gcd = G();
   if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
  
}


int RationalNumber::G()
{
   //Greatest Common Divisor 6/8 G 2
   int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
return gcd;

}
int RationalNumber::L(RationalNumber &obj)
{
   //Lowest Common Denominator 1/6 L 3/8 24
   int num=numerator*obj.denominator+obj.numerator*denominator;
int den=denominator*obj.denominator;
RationalNumber res(num,den);
   res.R();
   return res.denominator;
}

   bool RationalNumber::operator>(RationalNumber &number)
   {
       //greter than 1/6 > 3/8
       return (double) numerator / denominator > (double) number.numerator / number.denominator;
   }
   bool RationalNumber::operator>=(RationalNumber &number)
   {
       //greter than or equal 1/6 >= 3/8   
       return (double) numerator / denominator >= (double) number.numerator / number.denominator;
   }
   bool RationalNumber::operator<(RationalNumber &number)
   {
       //Less than 1/6 < 3/8   
       return (double) numerator / denominator < (double) number.numerator / number.denominator;
   }
   bool RationalNumber::operator<=(RationalNumber &number)
   {
       //Less than or equal 1/6 < 3/8
       return (double) numerator / denominator <= (double) number.numerator / number.denominator;
   }
   bool RationalNumber::operator==(RationalNumber &number)
   {
       //Equal to 3/8 = 9/24
       return (double) numerator / denominator == (double) number.numerator / number.denominator;
   }
   bool RationalNumber::operator!=(RationalNumber &number)
   {
       return (double) numerator / denominator != (double) number.numerator / number.denominator;
   }
   void RationalNumber::operator=(RationalNumber number)
   {
       numerator = number.numerator;
       denominator = number.denominator;
      
   }
   istream &operator>>(istream &in, RationalNumber &number)
   {
       char slash;
       in >> number.numerator >> slash >> number.denominator;
       if(number.denominator == 0)
       {
           cout<<"invalid denominator/n";
           number.denominator = 1;
       }
       return in;
   }
   ostream &operator<<(ostream &out, RationalNumber &number)
   {
       out << number.numerator << '/' << number.denominator;
      
       return out;
   }
void RationalNumber::printRationalNumber()
{
   cout << numerator << "/"<< denominator;
}//end printRational
void RationalNumber::printRationalNumberAsDouble()
{
   cout << (double) numerator / denominator;
}// end printRationalAsDouble

RationalNumber::RationalNumber()
{
   numerator = 0;
   denominator = 1;
}//end rational()

3. TestRationalNumber.cpp:

#include <iostream>
#include <iomanip>
using namespace std;
//#include "RationalNumber.h"
#include "RationalNumber.cpp"
int main()
{
// RationalNumber c( 1, 3 ), d( 2, 4 ), x;
RationalNumber c, d, x;
// test overloaded stream extraction operator
cout << "Enter a Rational Number (n/d): ";
cin >> c;
cout << "Enter a Rational Number (n/d): ";
cin >> d;
x = c + d; // test overloaded operators + and =
cout << c << " + " << d << " = " << x << endl;
x = c - d; // test overloaded operators - and =
cout << c << " - " << d << " = " << x << endl;
x = c * d; // test overloaded operators * and =
cout << c << " * " << d << " = " << x << endl;
x = c / d; // test overloaded operators / and =
cout << c << " / " << d << " = " << x << endl;
//Invert 3/8 I 8/3
RationalNumber i(3,8);
i.I();
cout<<"Invert 3/8 I "<<i<<endl;
   //Mixed fraction 8/3 M 2 and 2/3
   RationalNumber m(8,3);
   cout<<"Mixed fraction "<< m << "M ";
i.M();
   //Reduce 6/8 R 3/4
   RationalNumber res(6,8);
   cout<<"Greatest Common Divisor "<<res<<" is "<<res.G()<<endl;
   cout<<"Reduce "<<res<<" is ";
   res.R();
   cout<<res<<endl;
   //Greatest Common Divisor 6/8 G 2
   //Lowest Common Denominator 1/6 L 3/8 24

//test relational oparators
// test overloaded > operator
cout << c << ">" << d<<( ( c > d ) ? " True " : " False" )<<endl;
// test overloaded >= operator
cout << c << ">=" << d <<( ( c >= d ) ? " True " : " False " )<<endl;
// test overloaded < operator
cout << c << " < " << d<<( ( c <d )? " True " : " False" )<<endl;
// test overloaded <= operator
cout << c <<" <= " << d<<( ( c <= d ) ? " True " : " False" )<<endl;
// test overloaded == operator
cout << c << " == " << d<<( ( c == d ) ? " True " : " False" )<<endl;
// test overloaded != operator
cout << c << "!="<< d<<( ( c != d )? " True " : " False" )<<endl;
return 0;
} // end main

Add a comment
Know the answer?
Add Answer to:
General Description: A rational number is of the form a/b, where a and b are integers,...
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.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: Your program should have 3 files: a driver file to test the operations, a header file...

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

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

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

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

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

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

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

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