I have created 3 separate files but having issues with rationalnumber.cpp saying 'Rational' is not a class or namespace name. Any suggestions?
Below is what should happen.....but I'm stuck
thanks
the RationalNumber class should overload the stream insertion (<<) and stream extraction (>>) operators. The stream extraction operator should prevent a 0 denominator in a fraction, reduce fractions that are not in reduced form, and prevent negative denominators. Negative or 0 denominators should be set to 1.
Sample Output 1 (user input is in red):
Enter a Rational Number (n/d): 1/3
Enter a Rational Number (n/d): 2/4
1/3 + 1/2 = 5/6
1/3 - 1/2 = -1/6
1/3 * 1/2 = 1/6
1/3 / 1/2 = 2/3
1/3 <= 1/2 according to the overloaded > operator
1/3 < 1/2 according to the overloaded >= operator
1/3 < 1/2 according to the overloaded < operator
1/3 <= 1/2 according to the overloaded <= operator
1/3 != 1/2 according to the overloaded == operator
1/3 != 1/2 according to the overloaded != operator
Sample Output 2 (user input is red):
Enter a Rational Number (n/d): 2/2
Enter a Rational Number (n/d): 1/0
Invalid denominator: 0
1/1 + 1/1 = 2/1
1/1 - 1/1 = 0/1
1/1 * 1/1 = 1/1
1/1 / 1/1 = 1/1
1/1 <= 1/1 according to the overloaded > operator
1/1 >= 1/1 according to the overloaded >= operator
1/1 >= 1/1 according to the overloaded < operator
1/1 <= 1/1 according to the overloaded <= operator
1/1 == 1/1 according to the overloaded == operator
1/1 == 1/1 according to the overloaded != operator
driver.cpp
#include <iostream>
#include <iomanip>
using namespace std;
#include "RationalNumber.h"
#include "RationalNumber.cpp"
int main()
{
// RationalNumber c( 1, 3 ), d( 2, 4 ), x;
RationalNumber c, d, x;
// test overloaded stream extraction operator
cout << "Enter a Rational Number (n/d): ";
cin >> c;
cout << "Enter a Rational Number (n/d): ";
cin >> d;
// x = c + d; // test overloaded operators + and =
cout << c << " + " << d << " = " << x << endl;
// x = c - d; // test overloaded operators - and =
cout << c << " - " << d << " = " << x << endl;
// x = c * d; // test overloaded operators * and =
cout << c << " * " << d << " = " << x << endl;
// x = c / d; // test overloaded operators / and =
cout << c << " / " << d << " = " << x << endl;
// test overloaded > operator
cout << c << ( ( c > d ) ? " > " : " <= " ) << d
<< " according to the overloaded > operator\n";
// test overloaded >= operator
cout << c << ( ( c >= d ) ? " >= " : " < " ) << d
<< " according to the overloaded >= operator\n";
// test overloaded < operator
cout << c << ( ( c < d ) ? " < " : " >= " ) << d
<< " according to the overloaded < operator\n";
// test overloaded <= operator
cout << c << ( ( c <= d ) ? " <= " : " > " ) << d
<< " according to the overloaded <= operator\n";
// test overloaded == operator
cout << c << ( ( c == d ) ? " == " : " != " ) << d
<< " according to the overloaded == operator\n";
// test overloaded != operator
cout << c << ( ( c != d ) ? " != " : " == " ) << d
<< " according to the overloaded != operator\n";
return 0;
} // end main
RationalNumber.cpp
#include <iostream>
#include <iomanip>
//using std::cout;
//using std::istream;
//using std::ostream;
//using std::endl;
using namespace std;
#include "RationalNumber.h"
Rational::Rational(int numerator, int denominator)
{
this->numerator = numerator;
//this->denominator = denominator;
if(denominator ==0)
{
this->denominator = 1;
}// end if
reduction();
}
bool RationalNumber::operator>(RationalNumber &number)
{
return (double) numerator / denominator > (double) number.numerator / number.denominator;
}
bool RationalNumber::operator>=(RationalNumber &number)
{
return (double) numerator / denominator >= (double) number.numerator / number.denominator;
}
bool RationalNumber::operator<(RationalNumber &number)
{
return (double) numerator / denominator < (double) number.numerator / number.denominator;
}
bool RationalNumber::operator<=(RationalNumber &number)
{
return (double) numerator / denominator <= (double) number.numerator / number.denominator;
}
bool RationalNumber::operator==(RationalNumber &number)
{
return (double) numerator / denominator == (double) number.numerator / number.denominator;
}
bool RationalNumber::operator!=(RationalNumber &number)
{
return (double) numerator / denominator != (double) number.numerator / number.denominator;
}
RationalNumber &RationalNumber::operator=(RationalNumber &number)
{
numerator = number.numerator;
denominator = number.denominator;
return *this;
}
istream &operator>>(istream &in, RationalNumber &number)
{
char slash;
in >> number.numerator >> slash >> number.denominator;
if(number.denominator == 0)
{
cout<<"invalid denominator/n";
number.denominator = 1;
}
return ∈
}
ostream &operator<<(ostream &out, RationalNumber &number)
{
out << number.numerator << '/' << number.denominator;
return &out;
}
void Rational::printRational()
{
cout << numerator << "/"<< denominator;
}//end printRational
void Rational::printRationalAsDouble()
{
cout << (double) numerator / denominator;
}// end printRationalAsDouble
void Rational::reduction()
{
int largest;
largest = numerator > denominator ? numerator : denominator;
int gcd = 0; // greatest common divisor
for ( int loop = 2; loop <= largest; loop++ )
if ( numerator % loop == 0 && denominator % loop == 0 )
gcd = loop;
if (gcd != 0)
{
numerator /= gcd;
denominator /= gcd;
} // end if
} // end function reduction
Rational::Rational()
{
numerator = 0;
denominator = 1;
}//end rational()
RationalNumber.h
#ifndef RATIONALNUMBER_H
#define RATIONALNUMBER_H
#include <iostream>
//using std::istream;
//using std::ostream;
//class
class RationalNumber
{
friend istream &operator>>(istream&, RationalNumber&);
friend ostream &operator<<(ostream&, RationalNumber&);
public:
RationalNumber(int,int);
RationalNumber();
void printRationalNumber();
RationalNumber &operator=(RationalNumber&);
bool operator>(RationalNumber&);
bool operator>=(RationalNumber&);
bool operator<(RationalNumber&);
bool operator<=(RationalNumber&);
bool operator==(RationalNumber&);
bool operator!=(RationalNumber&);
void printRationalNumberAsDouble();
void reduction();
private:
int numerator;
int denominator;
};
#endif