Some reasons for point deductions % Reason Completely and correctly solved exerc
ID: 3752039 • Letter: S
Question
Some reasons for point deductions % Reason Completely and correctly solved exercises result in 100% points Exercise incorrect-exercise missing Use a single file/directory for each exercise Keep backups of functioning versions Be prepared to discuss (and defend) your sollutions 10% Function: example use ming 20% Function: contract missing 0% Function: exercise description missing 80% Function: function mang (incorrect-missing) 80% Function: tests missing Design a class rational, which adds fractions as fundamentall type into the C++ type system. Retain your design sketches and notes and submit them together with your solutions in electronic form (as well as in a platform agnostic read-only format) To achieve a seamless integration of the class rational its operators need to be overloaded and you are required to decide which. Please, follow the format skeleton at end. Finally, be as conat correct as possible! Exercise 1.1 Implement a standard (or default) constructor, a unary (or copy constructor), a binary constructor taking numerator and denominator of fraction as arguments, a unary (or in-place) and a binary operator for addition and subtraction. Provide a means for rational object to be displayed on the console by implementing operatorExplanation / Answer
// File Name: Rational.h
#ifndef Rational_H
#define Rational_H
#include <iostream>
using namespace std;
// Class Rational definition
class Rational
{
public:
// Data member for numerator and denominator
int numerator;
int denominator;
// Default constructor
Rational();
// Input redirection operator overloaded
friend ostream& operator<<(ostream &os, const Rational &f);
// Output redirection operator overloaded
friend istream& operator >> (istream &is, Rational &f);
// Overloading + operator
// Addition operator overloaded
Rational operator + (Rational f);
// Overloading - operator
// Subtraction operator overloaded
Rational operator - (Rational f);
// Overloading * operator
// Function to perform multiplication operation and return the result object
Rational operator * (Rational f);
// Overloading / operator
// Function to return the object after division
Rational operator / (Rational f);
// Overloading > operator
//Returns true if both numerator and denominator cross multiplication is greater
bool operator > (Rational f);
// Overloading < operator
//Returns true if both numerator and denominator cross multiplication is less
bool operator < (Rational f);
// Overloading = operator
// Returns the object after assignment
bool operator == (Rational f);
// Function to minimize
void reduce();
};// End of class
#endif
-----------------------------------------------------------------------------------------
#include "Rational.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
// Default constructor
Rational::Rational()
{
numerator = denominator = 1;
}// End of default constructor
// Input redirection operator overloaded
ostream& operator<<(ostream &os, const Rational &f)
{
// Stores numerator, operator symbol (/) and denominator in os stream
os<<f.numerator<<" / " <<f.denominator;
// Returns output stream
return os;
}// End of function
// Output redirection operator overloaded
istream& operator >> (istream &is, Rational &f)
{
// To store symbol
char ch;
// Stores numerator in is stream
is>>f.numerator;
// To discard fraction symbol
is>>ch;
// Stores denominator in is stream
is>>f.denominator;
// Returns input stream
return is;
}// End of function
// Overloading + operator
// Addition operator overloaded
Rational Rational::operator + (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Add both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator + f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading - operator
// Subtraction operator overloaded
Rational Rational::operator - (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator
// and multiplies the parameter object numerator with implicit object denominator
// Subtracts both the result and store it in the temporary object numerator
t.numerator = numerator * f.denominator - f.numerator * denominator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading * operator
// Function to perform multiplication operation and return the result object
Rational Rational::operator * (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Multiplies the implicit object numerator with parameter object denominator and stores it in temporary object numerator
t.numerator = numerator * f.numerator;
// Multiply implicit object denominator with parameter object denominator and store it in the temporary object denominator
t.denominator = denominator * f.denominator;
// Return the result object
return t;
}// End of function
// Overloading / operator
// Function to return the object after division
Rational Rational::operator / (Rational f)
{
// Creates a temporary object of Rational class
Rational t;
// Checks for divide by zero error
if(f.denominator == 0 || denominator == 0)
{
cout<<" Cannot divide by zero";
exit(0);
}// End of if condition
// Otherwise denominator is not zero
else
{
// Multiply implicit object numerator with parameter object denominator and store it in the temporary object numerator
t.numerator = numerator * f.denominator;
// Multiply parameter object numerator with implicit object denominator and store it in the temporary object denominator
t.denominator = f.numerator * denominator;
// Return the result object
return t;
}// End of else
}// End of function
// Overloading > operator
// Returns true if both numerator and denominator cross multiplication is greater
bool Rational::operator > (Rational f)
{
return (numerator * f.denominator > denominator * f.numerator);
}// End of function
// Overloading < operator
// Returns true if both numerator and denominator cross multiplication is less
bool Rational::operator < (Rational f)
{
return (numerator * f.denominator < denominator * f.numerator);
}// End of function
// Overloading = operator
// Returns the object after assignment
bool Rational::operator == (Rational f)
{
if((numerator == f.numerator) && (denominator == f.denominator))
return true;
else
return false;
}// End of function
// Function to minimize
void Rational::reduce()
{
// Loops from 2 to denominator value
for(int x = 2; x < denominator; x++)
{
// Checks if x is divisible by both numerator and denominator
if((numerator % x == 0) && (denominator % x == 0))
{
// Divide numerator by x and store the quotient in numerator
numerator = numerator / x;
// Divide denominator by x and store the quotient in denominator
denominator = denominator / x;
}// End of if condition
}// End of for loop
// Displays the result
cout<<numerator<<"/"<<denominator;
}// End of function
// Displays the menu for Operator
void selectOperator()
{
cout<<" + for Addition - for Subtraction * for Multiplication / for Division
> - for greater than < for less than == for check equals $ - for exit.";
}// End of function
// Main method
int main()
{
// To store operator symbol
string operatorSymbol;
// Creates two Rational class object
Rational f1, f2;
// Creates an object to store result
Rational t;
// Loops till operator symbol is not "$"
do
{
// Displays the menu for operator symbol
selectOperator();
// Accepts the symbol
cout<<" Enter the expression symbol: ";
cin>>operatorSymbol;
// Checks if operator symbol is "$" exit the program
if(operatorSymbol == "$")
exit(0);
cout<<" Example First fraction 2/3 + second fraction 3/2 ";
// Accepts first fraction
cout<<"Enter an First Fraction: ";
cin>>f1;
// Accepts second fraction
cout<<" Enter an Second Fraction: ";
cin>>f2;
// Checks if the operator symbol is "+"
if(operatorSymbol == "+")
{
// Overloads the + operator to perform addition and store the result in t
t = f1 + f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "-"
else if(operatorSymbol == "-")
{
// Overloads the - operator to perform subtraction and store the result in t
t = f1 - f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "*"
else if(operatorSymbol == "*")
{
// Overloads the * operator to perform multiplication and store the result in t
t = f1 * f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is "/"
else if(operatorSymbol == "/")
{
// Overloads the / operator to perform division and store the result in t
t = f1 / f2;
// Displays the result
cout<<f1<<" "<<operatorSymbol<<" "<<f2<<" = "<<t;
// Calls the function to reduce the fraction
cout<<" Reduced Fraction -> ";
t.reduce();
}// End of if condition
// Otherwise checks if the operator symbol is ">"
else if(operatorSymbol == ">")
{
// Overloads the > operator to check if object f1 is greater than f2
if(f1 > f2)
// If true display the result
cout<<" "<<f1<<" is greater than "<<f2;
// Otherwise f1 is not greater than f2
else
// If false display the result
cout<<" "<<f2<<" is greater than "<<f1;
}// End of if condition
// Otherwise checks if the operator symbol is "<"
else if(operatorSymbol == "<")
{
// Overloads the < operator to check if object f1 is less than f2
if(f1 < f2)
// If true display the result
cout<<" "<<f1<<" is smaller than "<<f2;
// Otherwise f1 is not less than f2
else
// If false display the result
cout<<" "<<f2<<" is smaller than "<<f1;
}// End of if condition
// Otherwise checks if the operator symbol is "=="
else if(operatorSymbol == "==")
{
// Overloads the == operator to check if object f1 is equals to f2
if(f1 == f2)
// If true display the result
cout<<f1<<" is equals to "<<f2;
// Otherwise f1 is not equals to f2
else
// If false display the result
cout<<f1<<" is not to "<<f2;
}// End of if condition
// Otherwise display invalid symbol
else
cout<<" Invalid symbol";
}while(1); // End of do while
}//End of main
Sample Output:
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: +
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/2
Enter an Second Fraction: 10/3
12 / 2 + 10 / 3 = 56 / 6
Reduced Fraction -> 28/3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: -
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 18/3
Enter an Second Fraction: 4/8
18 / 3 - 4 / 8 = 132 / 24
Reduced Fraction -> 22/4
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: *
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 1/2
Enter an Second Fraction: 3/4
1 / 2 * 3 / 4 = 3 / 8
Reduced Fraction -> 3/8
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: /
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 18/2
Enter an Second Fraction: 6/3
18 / 2 / 6 / 3 = 54 / 12
Reduced Fraction -> 9/2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: >
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 8/2
Enter an Second Fraction: 6/1
6 / 1 is greater than 8 / 2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: <
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 12/3
Enter an Second Fraction: 2/7
2 / 7 is smaller than 12 / 3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: ==
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 1/2
Enter an Second Fraction: 1/2
1 / 2 is equals to 1 / 2
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: ==
Example First fraction 2/3 + second fraction 3/2
Enter an First Fraction: 3/2
Enter an Second Fraction: 1/3
3 / 2 is not to 1 / 3
+ for Addition
- for Subtraction
* for Multiplication
/ for Division
> - for greater than
< for less than
== for check equals
$ - for exit.
Enter the expression symbol: $
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.