I don't want you to write my program, but please I have no time for riddles. Genuinely help me like "you need to use a switch statement instead of an if-else statement", or "put the if else in the class and call it in the main file" etc...
thanks in advance..heres my code so far (with instruction on the comment section on top of the class file
class
/********************************
* rational_number.cpp
* Luis Silva
*
*********************INSTRUCTIONS******************************
* Create a constructor that:
* prevents zero as a denominator
* reduces a fraction
* avoids neg denominator
* Overload the addition, substraction, multiplication, and division
operators
* Overload the relational and equality operators
**************************OUTPUT*****************************
Fraction One
Enter numerator: 3
Enter denominator: 2
Error: "invalid input try again".
Enter numerator: 3
Enter denominator: 0
Error: "invalid input try again".
Enter numerator: 3
Enter denominator: -9
Error: "invalid input try again".
Enter numerator: 2
Enter denominator: 4
Fraction Two
Enter numerator: 3
Enter denominator: 5
The result from their sum is: 11 / 10
The result from their substration is: -1 / 10
The result from their multiplication is: 3 / 10
The result from their division is: 5 / 6
*************************************************************/
#include "rational_number.h"
#include <iomanip>
#include <cstdlib>
using namespace std;
//initializing constructor
Rational::Rational()
{
init();
}
//copy constructor.
Rational::Rational(const Rational& r)
{
copy(r);
}
//equal operator check for self assignment and makes a copy or moves on
Rational& Rational::operator=(const Rational& rhs)
{
if(this != &rhs) // Check for self-assignment
copy(rhs);
return *this;
}
//input overloaded function creates input and checks for conditions
istream& operator>>(istream& in, Rational& r)
{
while(in)
{
cout << "Enter numerator: ";
in >> r.num;
cout << "Enter denominator: ";
in >> r.denom;
if(r.denom > 0 && r.denom > r.num)
{
r.reduce();
break;
}
else if (r.denom == 1)
break;
cerr << "\nError: \"invalid input try again\".\n\n";
}
cout << endl;
return in;
}
//output overloaded function is assigned to c
ostream& operator<<(ostream& out, const Rational& r)
{
return out << r.num << " / " << r.denom;
}
//optimizing operator overload
Rational& Rational::operator+=(const Rational& rhs)
{
num = (num * rhs.denom) + (rhs.num * denom);
denom *= rhs.denom;
return *this;
}
Rational operator+(const Rational& lhs, const Rational& rhs)
{
Rational sum(lhs);
sum += rhs;
return sum;
}
Rational& Rational::operator-=(const Rational& rhs)
{
num = (num * rhs.denom) - (rhs.num * denom);
denom *= rhs.denom;
return *this;
}
Rational operator-(const Rational& lhs, const Rational& rhs)
{
Rational diff(lhs);
diff -= rhs;
return diff;
}
Rational& Rational::operator*=(const Rational& rhs)
{
num *= rhs.num;
denom *= rhs.denom;
return *this;
}
Rational operator*(const Rational& lhs, const Rational& rhs)
{
Rational prod(lhs);
prod *= rhs;
return prod;
}
Rational& Rational::operator/=(const Rational& rhs)
{
num *= rhs.denom;
denom *= rhs.num;
return *this;
}
Rational operator/(const Rational& lhs, const Rational& rhs)
{
Rational quot(lhs);
quot /= rhs;
return quot;
}
Rational& Rational::operator==(const Rational& rhs)
{
num == rhs.num;
denom == rhs.denom;
return *this;
}
Rational operator==(const Rational& lhs, const Rational& rhs)
{
Rational same(lhs);
same == rhs;
return same;
}
//**********HELPER FUNCTIONS************
//function find gcd for proper reduction
int Rational::gcd(int x, int y)
{
for(int i = min(x, y); i > 1; --i)
{
if((x % i == 0) && (y % i == 0))
return i;
}
return 0;
}
//funtion that simplifies result
void Rational::reduce()
{
int factor = gcd(num, denom);
if(factor != 0)
{
num /= factor;
denom /= factor;
}
}
//Initiliazing function
void Rational::init()
{
num = denom = 0;
}
// Copy a Rational number into this Rational object.
void Rational::copy(const Rational& r)
{
num = r.num;
denom = r.denom;
}
main
/********************************
* main.cpp
* Luis Silva
*
* main file to run class and header
*************************************************************/
#include "rational_number.h"
using namespace std;
int main()
{
//creating objects for each fraction
Rational a, b, c;
//allowing input
cout << "\tFraction One\n\n";
cin >> a;
cout << "\tFraction Two\n\n";
cin >> b;
//computation of fractions
c = a + b;
cout << "The result from their sum is: " << c << endl;
c = a - b;
cout << "The result from their substration is: " << c << endl;
c = a * b;
cout << "The result from their multiplication is: " << c << endl;
c = a / b;
cout << "The result from their division is: " << c << endl;
/*
if (a = B)/>
cout << "Both fractions are the same " << endl;
else
cout << "Both fractions are not the same " << endl;
*/
}
header
/***************************
* rational_number.h
* Luis Silva
*
* header file for rational.cpp
****************************/
#ifndef RATIONAL_NUMBER_H
#define RATIONAL_NUMBER_H
#include <iostream>
struct Rational
{
public:
//declaration of construtors
Rational(); //empty constructor
Rational(const Rational&); //copy constructor
Rational& operator=(const Rational&); //overload constructor
// Rational input and output functions using overloaded operators:
friend std::istream& operator>>(std::istream& in, Rational& r);
friend std::ostream& operator<<(std::ostream& out, const Rational& r);
// Rational overloaded operator functions:
Rational& operator+=(const Rational& rhs);
friend Rational operator+(const Rational& lhs, const Rational& rhs);
Rational& operator-=(const Rational& rhs);
friend Rational operator-(const Rational& lhs, const Rational& rhs);
Rational& operator*=(const Rational& rhs);
friend Rational operator*(const Rational& lhs, const Rational& rhs);
Rational& operator/=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
Rational& operator==(const Rational& rhs);
friend Rational operator==(const Rational& lhs, const Rational& rhs);
/*
//Relational operators ==, !=, >, <, >=, <=
Rational& operator!=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
Rational& operator>=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
Rational& operator<=(const Rational& rhs);
friend Rational operator/(const Rational& lhs, const Rational& rhs);
//logical operators !,&&,||
*/
private:
int num, denom;
const char* errMsg;
// Helper functions:
void init();
int gcd(int, int);
void reduce();
void copy(const Rational&);
};
#endif // RATIONAL_NUMBER_H

New Topic/Question
Reply



MultiQuote




|