rational-numbers-2
Description
Define a class for rational numbers. Please download the and add your code for submission.
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.
Include:
- a constructor with two arguments that can be used to set the
member variables of an object to any legitimate values. - a constructor that has only a single parameter of type int; call this single
parameter whole_number and define the constructor so that the object
will be initialized to the rational number whole_number/1. - a default constructor that initializes an object to 0 (that is, to 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. - all of the following operators so that they correctly apply to the type
Rational: ==, <, <=, >, >=, +, -, *, and /.
(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.)
Requirements
- DOWNLOAD and add your code for this lab. Do not change any code that’s already inside the test program, but add your class member variables and function definition and implementations
- Follow the ADT (Abstract Data Type) guideline to implement the required class.
- Comment well in your program and any functions you use
Test Cases
1/2, -1/2, 4, 0, -/12 (should cause error)
Demo
Welcome to the Rational World of Wonders!
Please enter a rational number for this calculation:
1/2
1/2 + 1/2 = 1
1/2 - 1/2 = 0
1/2 * 1/2 = 1/4
1/2 / 1/2 = 1
1/2 == 1/2 : true
1/2 < 1/2 : false
1/2 <= 1/2 : true
1/2 > 1/2 : false
1/2 >= 1/2 : true