Question

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 the class Rational.

Include a constructor with two arguments that can be used to set the members variables of an object to any legitimate values. Also include a constructor that has only a single parameter of type int; call the single parameter whole_number and define the constructor so that the object will be initialized to the rational number whole_number/1. Also include a default constructor that initializes an object to 0 (that is, 0/1).

Overload the input and output operators >> and <<. Numbers are to be input and output in the form 1/2, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 15/32, and -300/-401 are also possible inputs. Overload all the following operators so that they correctly apply to the type Rational: ==, <, <=, >, >=, +, -, *, and /. Also write a test program to test your class.

(HINTS: Two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b. You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/-8 would be represented the same as -1/2. You should also write a test program to test the class.)     

// Display the three values to test cout
    cout << "\nTest1 equals " << test1;
    cout << "\nTest2 equals " << test2;
    cout << "\nTest3 equals " << test3;
// Test our operators
    cout << "\nTest1 * Test2 equals " << test1*test2;
    cout << "\nTest1 / Test3 equals " << test1/test3;
    cout << "\nTest2 + Test3 equals " << test2+test3;
    cout << "\nTest3 - Test1 equals " << test3-test1;
    if (test1 == test2)
        cout << "\nTest1 is equal to Test2";
    if (test1 < test2)
        cout << "\nTest1 is less than Test2";

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

Rational.h

#ifndef H_Rational
#define H_Rational

#include <iostream>
using namespace std;

//Rational class
class Rational
{
   //private members
   private:
int nm, dm;
//public members
public:
   Rational();
Rational(int n);
Rational(int n,int d);
Rational(const Rational& fx);

void operator=(Rational fx);
Rational operator+(Rational fx);
Rational operator-(Rational fx);
Rational operator*(Rational fx);
Rational operator/(Rational fx);

bool operator==(Rational fx);
bool operator!=(Rational fx);
bool operator<=(Rational fx);
bool operator>=(Rational fx);
bool operator<(Rational fx);
bool operator>(Rational fx);

void simplify();
friend ostream& operator<<(ostream& os,const Rational& ob);
friend istream& operator>>(istream& is,Rational& ob);
};

#endif

Rational.cpp

#include <iostream>
#include<math.h>
#include"Rational.h"
using namespace std;

//default constructor
Rational::Rational()
{
   nm = 0;
   dm = 1;
}

//parameterized constructor
Rational::Rational(int n)
{
   nm = n;
dm = 1;
}


//parameterized constructor
Rational::Rational(int n, int d)
{
   nm = n;
   if(d==0) throw "denominators error";
dm = d;
}

//copy constructor
Rational::Rational(const Rational& t)
{
nm = t.nm;
dm = t.dm;
}

//= operator overloading
void Rational::operator=(Rational fx)
{
nm = fx.nm;
dm = fx.dm;
}

//+ operator overloading
Rational Rational::operator+(Rational fx)
{
   int k, j;

k=fx.dm *dm;
j=fx.nm * dm + fx.dm * nm;
Rational tx(j,k);
tx.simplify();
return tx;
}


//- operator overloading
Rational Rational::operator-(Rational fx)
{
   int j, k;

k=fx.dm *dm;
j=fx.nm * dm - fx.dm * nm;
Rational tx(j,k);
tx.simplify();
return tx;
}

//* operator overloading
Rational Rational::operator*(Rational fx)
{
   int j, k;

   j = fx.nm * nm;
   k = fx.dm *dm;
Rational tx(j,k);
tx.simplify();
return tx;
}


// / operator overloading
Rational Rational::operator/(Rational fx)
{
   int j, k;

   j = fx.dm * nm;
   k = fx.nm * dm;
Rational tx(j,k);
tx.simplify();
return tx;
}


// == operator overloading
bool Rational::operator==(Rational fx)
{
return (nm==fx.nm && dm==fx.dm);
}


// != operator overloading
bool Rational::operator!=(Rational fx)
{
return (nm!=fx.nm || dm!=fx.dm);
}


// <= operator overloading
bool Rational::operator<=(Rational fx)
{
return (nm*fx.dm<=fx.nm*dm);
}


// >= operator overloading
bool Rational::operator>=(Rational fx)
{
return (nm*fx.dm>=fx.nm*dm);
}

// < operator overloading
bool Rational::operator<(Rational fx)
{
return (nm*fx.dm<fx.nm*dm);
}


// > operator overloading
bool Rational::operator>(Rational fx)
{
return (nm*fx.dm>fx.nm*dm);
}


// << operator overloading
ostream& operator<<(ostream& os,const Rational& ob)
{
os<<ob.nm<<"/"<<ob.dm<<"\n";
return os;
}

// >> operator overloading
istream& operator>>(istream& is, Rational& ob)
{
is>>ob.nm>>ob.dm;
return is;
}

//function to simplify the Rational
void Rational::simplify()
{
   int i, j, k;

   j = abs(nm);
   k = abs(dm);

   for(i=j; j%i!=0 || k%i!=0; i--);

   nm = nm/i;
   dm = dm/i;

   if(nm<0 && dm<0 ||dm<0)
   {
       nm = -nm;
       dm = -dm;
   }
}

Main.cpp

#include <iostream>
#include"Rational.h"

using namespace std;

//main function
int main()
{
   Rational test1(2,3), test2(1,3), test3(1,2);

cout << "\nTest1 equals " << test1;
cout << "\nTest2 equals " << test2;
cout << "\nTest3 equals " << test3;
// Test our operators
cout << "\nTest1 * Test2 equals " << test1*test2;
cout << "\nTest1 / Test3 equals " << test1/test3;
cout << "\nTest2 + Test3 equals " << test2+test3;
cout << "\nTest3 - Test1 equals " << test3-test1;
if (test1 == test2)
cout << "\nTest1 is equal to Test2";
if (test1 < test2)
cout << "\nTest1 is less than Test2";

   return 0;
}

Output:

Add a comment
Know the answer?
Add Answer to:
C++ and/or using Xcode... Define a class of rational numbers. A rational number is a number...
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 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...

  • C++ only I'm using code lite editor as well Please add comments and pre and post...

    C++ only I'm using code lite editor as well Please add comments and pre and post conditions too 9:16 AM .oooo Verizon GI 98% Use C++ only Please include comments and pre post conditions am also using a new editor called code lite write a class for rational numbers. Each 15 object in the class should have two inte values that define the rational number: the numerator and the denominator. For example, the fraction 516 would have a denominator of...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • 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++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

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

  • 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