Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topic 10 Assignment c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q

Topic 10 Assignment 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 is also a rational number, as in 2/1 or 17/1, you should provide a constructor with a single int parameter as well. You should take into account that if the user constructs a rational number that corresponds to a non-reduced fraction, you should automatically reduce the fraction in your constructor. For example, if the user constructs a rational number with numerator 4 and denominator 16, you should store it as numerator 1 and denominator 4. You should also take into account the storage of negative values. For simplicitys sake, always store a negative in the numerator if need be. For example, if the user constructs a rational number with numerator 2 and denominator -4, it should be stored as -1, 2 respectively. In addition, if the user constructs a rational number with numerator -1 and denominator -3, it should be stored as 1, 3 respectively. Lastly, if the user happens to construct a rational number with zero as the denominator, simply change the denominator to 1.

Provide the following public member functions:

add accepts a rational number parameter and returns a rational number

sub accepts a rational number parameter and returns a rational number

mul accepts a rational number parameter and returns a rational number

div accepts a rational number parameter and returns a rational number

equals accepts a rational number parameter and returns a bool

less_than accepts a rational number parameter and returns a bool

greater_than accepts a rational number parameter and returns a bool

input accepts an istream parameter and gets input in the form numerator/denominator from the keyboard

output accepts an ostream parameter and writes rational numbers in the form numerator/denominator or numerator if the denominator is 1, or 0 if the numerator is zero

Provide the following private member functions:

reduce a void function that accepts no arguments and reduces the rational number

gcd a function that returns an int and returns the greatest common divisor of its two int parameters num1 and num2 (body given below) this uses Euclids algorithm

while (num1 != num2)

if (num1 > num2)

num1 = num1 - num2;

else

num2 = num2 - num1;

return num1;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

Students also viewed these Databases questions