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.

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

Note : I have Developed Header file and implemenation file..I had some doubt that is should I have to accept the input from the user dynamically..? Or Should I have to use Hard Coding...?

________________________

// RationalNumber.h

#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
class RationalNumber
{
public:
  
RationalNumber();
// Initializes the rational number to 0
RationalNumber(int wholeNumber);
// Initializes the rational number to wholeNumber/1
RationalNumber(int m, int n);
// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)
void output();
// Precondition: The rational number is defined.
// Postcondition: The rational number has been displayed on
// the screen in the form m/n.
friend bool operator ==(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 equals r2; false otherwise.
friend bool operator <(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than r2; false otherwise.
friend bool operator >(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than r2; false otherwise.
friend bool operator <=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than or equal to r2; false otherwise.
friend bool operator >=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than or equal to r2; false otherwise.
friend bool operator !=(const RationalNumber& r1, const RationalNumber& r2);
// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is not equal to r2; false otherwise.

friend RationalNumber operator+(RationalNumber a, RationalNumber b);
friend RationalNumber operator-(RationalNumber a, RationalNumber b);
friend RationalNumber operator*(RationalNumber a, RationalNumber b);
friend RationalNumber operator/(RationalNumber a, RationalNumber b);
void toMixed();
void printRational();
int gcd(int a, int b);
private:
// Declaring variables
int numer; // the numerator of the number
int denom; // the denominator of the number
};
#endif

_____________________

// RationalNumber.cpp

#include <iostream>
using namespace std;
#include "RationalNumber.h"
// Initializes the rational number to 0
RationalNumber::RationalNumber()
{
this->numer = 0;
this->denom = 1;
}

// Initializes the rational number to wholeNumber/1
RationalNumber::RationalNumber(int wholeNumber)
{
this->numer=wholeNumber;
this->denom=1;
}

// Initializes the rational number to m/n if n is not 0;
// the sign of the rational is stored in the numerator
// (the denominator is always positive);
// exits if n = 0 (invalid rational number)

RationalNumber::RationalNumber(int m, int n)
{
if(n==0)
{
cout<<"** Denominator Must nor be zero **"<<endl;
exit(0);
}
else if(denom<0)
{
this->numer=-(m);
this->denom=-(n);
}
else
{
this->numer=(m);
this->denom=(n);
}
}

// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 equals r2; false otherwise.
bool operator ==(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}

// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than r2; false otherwise.
bool operator <(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}

// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than r2; false otherwise.
bool operator >(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}

// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is less than or equal to r2; false otherwise.
bool operator <=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}

// Precondition: r1 and r2 are valid rational numbers
// Returns true if r1 is greater than or equal to r2; false otherwise.
bool operator >=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}

bool operator !=(const RationalNumber& r1, const RationalNumber& r2)
{
int a = r1.numer;
int b = r1.denom;
int c = r2.numer;
int d = r2.denom;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;
}
RationalNumber operator+(RationalNumber r1, RationalNumber r2) {
int a, b, c, d;
a =r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int sumnumer = (a * d + b * c);
int sumdenom = (b * d);
RationalNumber r(sumnumer, sumdenom);

return r;
}
RationalNumber operator-(RationalNumber r1, RationalNumber r2) {
int a, b, c, d;
a = r1.numer;
b = r1.denom;
c = r2.numer;
d = r2.denom;
int subnumer = (a * d - b * c);
int subdenom = (b * d);
RationalNumber r(subnumer, subdenom);
return r;
}
RationalNumber operator*(RationalNumber a, RationalNumber b) {
RationalNumber result((a.numer*b.numer) , (a.denom*b.denom));
return result;
}
RationalNumber operator/(RationalNumber a, RationalNumber b) {
RationalNumber result((a.numer*b.denom) , (a.denom*b.numer));
return result;
}
void RationalNumber::printRational()
{
int g=gcd(numer,denom);
if(denom!=1)
cout<<numer/g<<"/"<<denom/g;
else
cout<<numer/g;
}
int RationalNumber::gcd(int a, int b)
{
// % is modulus which is the remainder of a division
// base case
if ((a % b) == 0)
{
return b;
}
// recursive case
else
{
return gcd(b, a % b);
}
}

//Method which will convert a fraction into mixed fraction.
void RationalNumber::toMixed()
{
int a=0,b=0;
if(nume>denom){
  
a = numer/denom;
  
b = numer-(a*denom);
}
cout<<"["<<a<<" and "<<b<<" / "<<denominator<<"]"<<endl;
}

_____________________

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

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

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

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

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

  • 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

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

  • Absolute value is always... 1. Positive 2. Non-negative Every fraction is a rational number. True or...

    Absolute value is always... 1. Positive 2. Non-negative Every fraction is a rational number. True or False? The sum of 2/3 and 5/7 is 7/10. True or False? The quotient of two fractions cannot be a whole number True or False? The set of whole numbers is a subset of the set of rational numbers. True or False? Which of these fractions is between 7/13 and 14/19 ? 1. a. 6/17 2. b. 9/16 3. c. 1/2 4. d. None...

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