[C++] Create the code to implement and test the – – = * *= / /= and == operators
ID: 3877063 • Letter: #
Question
[C++]
Create the code to implement and test the – –= * *= / /= and == operators
// Rational.cpp : Defines the entry point for the console application.
// Create a Rational class defination
// Rational(numerator, denominator)
//
// #include "stdafx.h" // only for Microsoft Visual Studio C++
#include "Rational.h" // double quotes = find file in project folder
#include <iostream> // angle brackets = find file in compiler folder
using namespace std;
// function prototypes
void initializeNumbers(Rational &, Rational &, Rational &);
void displayNumbers(const char *, Rational, Rational, Rational);
int main(int argc, char* argv[])
{
// class object
// | |
// V V
Rational n1;
Rational n2;
Rational n3;
cout << endl << endl << "**** n2 = n2.add(n3); n2.display(); // n2 should become 17/12" << endl;
initializeNumbers(n1, n2, n3);
displayNumbers("Before", n1, n2, n3);
n2 = n2.add(n3); // n2 + n3 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
displayNumbers("After ", n1, n2, n3);
cout << "n2.display() shows only n2 ";
n2.display(); // using the display( ) member function
cout << endl;
cout << endl << endl << "**** n1 = n2 + n3; // n1 should become 17/12. Others unchanged" << endl;
initializeNumbers(n1, n2, n3);
displayNumbers("Before", n1, n2, n3);
n1 = n2.operator+(n3); // n2 + n3 = 3/4 + 2/3 = 9/12 + 8/12 = 17/12
displayNumbers("After ", n1, n2, n3);
cout << endl << endl << "**** n1 = n2 += n3; // n1 and n2 should become 17/12. Others unchanged" << endl;
initializeNumbers(n1, n2, n3);
displayNumbers("Before", n1, n2, n3);
n1 = n2 += n3;
displayNumbers("After ", n1, n2, n3);
/*
cout << endl << endl << "**** n1 = n2 - n3; // n1 should be 1/12. Others unchanged" << endl;
initializeNumbers (n1, n2, n3);
displayNumbers("Before", n1, n2, n3);
n1 = n2 - n3; // n2 - n3 = 3/4 - 2/3 = 9/12 - 8/12 = 1/12
displayNumbers("After ", n1, n2, n3);
cout << endl << endl << "**** n1 = n2 -= n3; // n1 and n2 should be 1/12. Others unchanged" << endl;
initializeNumbers (n1, n2, n3);
displayNumbers("Before", n1, n2, n3);
n1 = n2 -= n3;
displayNumbers("After ", n1, n2, n3);
*/
cout << endl << endl << "**** Rational number to double. 1/12 displays as 0.0833333" << endl;
cout << "double(n2) = " << double(n2) << endl;
cout << endl;
system("PAUSE");
return 0;
}
// Initialize each of the variables before testing each rational operator
void initializeNumbers(Rational &n1, Rational &n2, Rational &n3)
{
n1 = Rational(); // 0 no arguments
n2 = Rational(3, 4); // 3/4
n3 = Rational(2, 3); // 2/3
}
// Display each of the rational numbers using the friend function <<
void displayNumbers(const char *msg, Rational n1, Rational n2, Rational n3)
{
cout << msg << " " << n1 << " " << n2 << " " << n3 << endl;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
// #include "stdafx.h"
#include <iostream>
#include "Rational.h"
using namespace std;
// By using the default parameter settings in Rational.h, this
// constructor also provides the default constructor Rational()
Rational::Rational(int num, int denom)
{
setRational(num, denom); // set numerator and denominator, reduce fraction, fix the sign
}
// Helper function to fix a zero denominator and fix the sign if denominator is negative
Rational Rational::setRational(int n, int d) // helper function
{
numerator = n;
denominator = d;
// if denominator == 0 then set it = 1
if (denominator == 0)
denominator = 1;
if (denominator < 0) // if denominator is neg, multiply num and denom by -1
{
numerator = -numerator; // fix sign of numerator +/-
denominator = -denominator; // denominator always +
}
int gcd = GCD(numerator, denominator);
if (denominator != 0)
{
numerator /= gcd;
denominator /= gcd;
}
return *this; // return the current object
}
// find the lowest common divisor using a recursive function
int Rational::GCD(int v1, int v2)
{
if (v2 == 0) return v1;
else return GCD(v2, v1%v2);
}
Rational Rational::add(Rational right)
{
int newNumerator;
int newDenominator;
newNumerator = numerator * right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object and return it
return Rational(newNumerator, newDenominator);
}
// the operator+ method does the same thing as the add method
Rational Rational::operator+ (Rational right)
{
// create local (temporary) variables
int newNumerator;
int newDenominator;
// compute the result and save in the local variables
// the current object's numerator and denominator are not changed
newNumerator = numerator * right.denominator + right.numerator*denominator;
newDenominator = denominator * right.denominator;
// create a new Rational object with the result and return it
return Rational(newNumerator, newDenominator);
}
Rational Rational::operator+= (Rational right)
{
// the current object is updated with the result of the +=
numerator = numerator * right.denominator + right.numerator*denominator;
denominator = denominator * right.denominator;
// fix the sign, reduce the fraction and return the current object
return setRational(numerator, denominator);
}
Rational::operator double() const // convert Rational to double and return
{
return double(numerator) / double(denominator);
}
// Display a Rational number using the display() member method
void Rational::display()
{
cout << numerator << '/' << denominator;
}
// Display a Rational number using << and a friend function.
// Friend functions are not part of the class and their code must be
// declared outside of the class with no :: Scope Resolution Operator.
// All function arguments must have their class defined
ostream &operator<< (ostream &out, Rational const &r)
{
out << r.numerator << '/' << r.denominator;
return out; // This is to keep the stream flowing
}
----------------------------------------------------------------------------------------------------------------
#pragma once
#ifndef RATIONAL_H // if this compiler macro is not defined
#define RATIONAL_H // then define it so this file will not be processed again
// #include "stdafx.h" // use only for Microsoft Visual Studio C++
#include <iostream>
using namespace std;
class Rational
{
// Friend functions are actually declared outside the scope of the
// class but have the right to access public and private data and
// member function members that belong to the class. The friend
// function below gives the << operator for ostreams (including cout)
// the ability to output a Rational object by accessing its member data.
friend ostream &operator<< (ostream &out, Rational const &r);
public:
Rational(int num = 0, int denom = 1); // also provides default constructor
Rational add(Rational right);
Rational operator+ (Rational right); // + addition operator
Rational operator+= (Rational right); // += addition assignment operator
void display();
operator double() const; // convert Rational to double
private:
int numerator;
int denominator;
// helper functions are private and not accessible by the main program
int GCD(int v1, int v2);
Rational setRational(int n, int d);
};
#endif
Explanation / Answer
/*
* Rational.h
* header file for Rational class
*/
#ifndef RATIONAL_H_
#define RATIONAL_H_
//Rational class definition
class Rational {
public:
Rational();
Rational ( int n = 0, int d = 1); //default case
int adding( const Rational& a); // a) adding two rational numbers
int subtracting(const Rational& s); // b) subtracting two rational numbers
int multiplying(const Rational& m); // c) multiplying two rational numbers
int dividing(const Rational& b); // d) dividing two rational numbers
void printRational(); // e) printing in form of a/b, where a is numerator and b is denominator
void printFloat(); // f) printing in floating point format
private:
int numerator;
int denominator;
void reduce();
};
#endif /* RATIONAL_H_ */
/*
* Rational.cpp
*
*/
#include <iostream>
#include <iomanip>
#include "Rational.h"
using namespace std;
Rational::Rational(int n, int d) {
numerator = d<0 ? -n:n; // to make sure that if a negative number is entered, it will be in the numerator
denominator = d<0 ? -d:n;
reduce();
}
Rational Rational::adding ( const Rational& a) {
Rational r;
r.numerator = a.numerator * denominator + a.denominator * numerator;
r.numerator = a.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::subtracting (const Rational& s) {
Rational r;
r.numerator = s.numerator * denominator - s.denominator * numerator;
r.numerator = s.denominator * denominator;
r.reduce();
return r;
}
Rational Rational::multiplying (const Rational& m){
Rational r;
r.numerator = m.numerator * numerator;
r.numerator = m.denominator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
Rational Rational::dividing (const Rational& b){
Rational r;
r.numerator = b.denominator * numerator;
r.numerator = b.numerator * denominator;
if (denominator != 0)
r.reduce();
return r;
}
void Rational::printRational(){
if (denominator = 0)
cout<< "Error" <<endl;
else{
cout << numerator << "/" << denominator << endl;
}
}
void Rational::printFloat(){
if (denominator = 0)
cout << "Error" <<endl;
else
cout << numerator / denominator << "." << numerator%denominator <<endl; //float(numerator) float(denominator) ????????
}
void Rational::reduce(){
int n = numerator < 0 ? -numerator : numerator;
int d = denominator;
int largest = n > d ? n : d;
int gcd = 0;
for (int i = largest; i>= 2; i--)
if(numerator % i == 0 && denominator % i == 0){
gcd = i;
break;
}
if (gcd != 0) {
numerator /= gcd;
denominator /= gcd;
}
}
/*
* assign8.cpp
* program to test Rational class
*/
#include <iostream>
#include "Rational.h"
#include "Rational.cpp"
using namespace std;
int main(){
Rational x(-3/6), y(-25/40), z, w(9);
//adding
x.printRational();
cout<< " + ";
y.printRational();
z = x.adding(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
// subtracting
x.printRational();
cout<< " - ";
y.printRational();
z = x.subtracting(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
// multiplying
x.printRational();
cout<< " * ";
y.printRational();
z = x.multiplying(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
//dividing
x.printRational();
cout<< " / ";
y.printRational();
z = x.adding(y);
cout<< " = ";
z.printRational();
cout << endl;
z.printRational();
cout<< " = ";
z.printFloat();
cout << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.