Question: //This program has to be done in C++, and the instructions provided has to be strictly followed. This program must be done through serparate compilation.
//This program has to be done in C++, and the instructions provided has to be strictly followed. This program must be done through serparate compilation. Total 2 files = (1cpp + 1 header) namely, Rational.h and Rational.cpp. It will be easier for me to understand if you kindly put a comment before the starting of each fiile. TIA for your answer, I really appreciate it. Design, implement and test a C++ class to work with real numbers as rational numbers.
The data members are the numerator and denominator, stored as integers.
Implement the operators:<< and >> (i.e., input and output).
Rational numbers are read and written as an integer, followed by a slash, followed by an integer.
Either (or both) of the numerator and the denominator may be input as negative integers. The following are all possible inputs: 1/2, -1/-2, -1/2 and 1/-2.
It's a design decision as to when to "normalize" a rational number. Should it always be normalized? Or is that only a choice to be made when we dispaly it? We will maintain the numbers in their normalized form. Thus when they are first created, they must be stored as normalized and any operation that might change a number must also normalize it. What does "normalize" mean? The numerator and denominator will be in "lowest terms" and only the numerator may be negative. For example, if we were given as input 4/-8, we would store and display it as -1/2. Simililarly -8/-6 would turn into 4/3.
+= Implement as a member function.
+ (i.e., addition) Implement as a non-member function using the += operator. Do not make + a friend. (Note there is no reason to.)
==
!= Implement as a non-member, but not as a friend.
++ and --
Member for ++
Non-member, non-friend for --
Both pre- and post-.
Make it possible to write if (r) {}, where r is a Rational number and the test will evaluate to false if the numerator is zero and true otherwise.
After implementing and testing your code
Use separate compilation
Place the class in the CS2124 namespace.
We provide two files
testRational.cpp to test your class.
gcd.cpp: to compute the greatest common divisor of two non-negative integers, that should be useful for writing the normalize function. If you're clever, try to write this function yourself before looking at ours.
You need to write the files Rational.h and Rational.cpp, satisfying the above requirements and any additional requirements indicated in the test program.
If time allows:
<
<=, > and >= Implement as non-member, but not as a friend For your convinience I have copy pasted the contents of the attached files by commenting their respective names. The created program must use the testRational.cpp for compiling and running. // testRational.cpp
#include
#include "Rational.h"
using namespace std;
using namespace CS1124;
int main() {
Rational a, b;
cout << "Input two rational numbers. ";
cout << "a: ";
cin >> a;
cout << "b: ";
cin >> b;
Rational one = 1;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "one = " << one << endl;
cout << "a += b: " << (a += b) << endl; // Implement as member
cout << "a = " << a << endl;
// Implement as non-member, but not a friend
cout << "a + one: " << (a + one) << endl;
cout << "a == one: " << boolalpha << (a == one) << endl;
// How does this work?
cout << "1 == one: " << boolalpha << (1 == one) << endl;
// Do not implement as friend.
cout << "a != one: " << boolalpha << (a != one) << endl;
cout << "a = " << a << endl;
cout << "++a = " << (++a) << endl;
cout << "a = " << a << endl;
cout << "a++ = " << (a++) << endl;
cout << "a = " << a << endl;
cout << "--a = " << (--a) << endl;
cout << "a = " << a << endl;
cout << "a-- = " << (a--) << endl;
cout << "a = " << a << endl;
cout << "++ ++a = " << (++ ++a) << endl;
cout << "a = " << a << endl;
cout << "-- --a = " << (-- --a) << endl;
cout << "a = " << a << endl;
cout << "a++ ++ = " << (a++ ++) << endl;
cout << "a = " << a << endl;
// Even though the above example, (a++ ++), compiled, the
// following shouldn't.
// cout << "a-- -- = " << (a-- --) << endl;
// cout << "a = " << a << endl;
if (Rational(1)) {
cout << "1 is true" << endl;
} else {
cout << "1 is false" << endl;
}
if (Rational(0)) {
cout << "0 is true" << endl;
} else {
cout << "0 is false" << endl;
}
}
//gcd.cpp int greatestCommonDivisor(int x, int y) { while (y != 0) { int temp = x % y; x = y; y = temp; } return x; } //For the comment below : The expectation is simple. You just have to create a program that overloads above mentioned operators and runs the way written on the //testcode. Thank You.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
