Question
C++ Problem I must have the operators for +,-,*,/, set up incorectly. The implemintation file gives me errors, but I dont understand why. The operator
C++ Problem
I must have the operators for +,-,*,/, set up incorectly. The implemintation file gives me errors, but I dont understand why. The operator<< is working properly when tested. Any help would be greatly appreciated, Thank You!
// Rational.h
#ifndef Rational_H
#define Rational_H
#include
using namespace std;
class Rational {
private:
int numerator, denominator;
void reduce(int&, int&);
public:
Rational(int n, int d);
friend ostream& operator<<(ostream& y, const Rational& out);
friend Rational operator+(Rational a, Rational b);
friend Rational operator-(Rational a, Rational b);
friend Rational operator*(Rational a, Rational b);
friend Rational operator/(Rational a, Rational b);
};
#endif
//implemination.cpp
#include #include"Rational.h" using namespace std;
Rational::Rational(int numer, int denom) {
reduce(numer, denom);
numerator = numer;
denominator = denom;
}
void Rational::reduce(int &numer, int &denom) {
if (numer < 0 && denom < 0) {//If both Denominator and Numerator are negative, it equals positive
numer *= -1;
denom *= -1;
}
if (numer < 0) { //If numerator is negative it temporarly makes it positive until end of if-statement
numer *= -1;
for (int r = denom * numer; r > 1; r--) {
if ((denom % r == 0) && (numer % r == 0)) {
denom /= r;
numer /= r;
}
}
numer *= -1;
}
if (denom < 0) {
numer *= -1; //If denominator is negative it formats it so negative comes before numerator at end
for (int r = denom * numer; r > 1; r--) {
if ((denom % r == 0) && (numer % r == 0)) {
denom /= r;
numer /= r;
}
}
denom *= -1;
}
else // If both positive no changes are made
for (int r = denom * numer; r > 1; r--) {
if ((denom % r == 0) && (numer % r == 0)) {
denom /= r;
numer/= r;
}
}
}
ostream& operator<<(ostream& y, const Rational& out ) {
if (out.denominator == 1)
y << out.numerator << endl;
else
y << out.numerator << "/" << out.denominator << endl;
return (y);
}
Rational operator+(Rational a, Rational b) {
a.numerator *= b.denominator;
a.denominator *= b.denominator;
b.numerator *= a.denominator;
b.denominator *= a.denominator;
Rational result(a.numerator+b.denominator / b.denominator);
return result;
}
Rational operator-(Rational a, Rational b) {
a.numerator *= b.denominator;
a.denominator *= b.denominator;
b.numerator *= a.denominator;
b.denominator *= a.denominator;
Rational result(a.numerator + b.denominator / b.denominator);//
return result;
}
Rational operator*(Rational a, Rational b) {
Rational result((a.numerator*b.numerator) / (a.denominator*b.denominator));
return result;
}
Rational operator/(Rational a, Rational b) {
Rational result((a.numerator*b.denominator) / (a.denominator*b.numerator));
return result;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started