My project is to create a polynomial with rational coeffients. I am stuck at this point with 2 error messages, one being "illegal indirection" in "function operato<<" . I think that my constructors are not correct. Deadline is fast-approaching. Any help would be appreciated.
class Polynomial
{
int *coeff;
int nCoeff;
public:
Polynomial(const int = 0);
Polynomial(Polynomial &);
Polynomial(const int, int *);
~Polynomial();
const int getDegree();
void shrink();
int evaluate(int);
Polynomial operator+( const Polynomial & ) const; // addition
Polynomial operator-( const Polynomial & ) const; // subtraction
Polynomial operator*( const Polynomial & ) const; // multiplication
Polynomial& operator=(const Polynomial &a);
int operator[](unsigned int index);
friend bool operator==(const Polynomial &, const Polynomial &);
Polynomial& operator++(){
++coeff[0];
return *this;
}
Polynomial& operator++(int unused){
++coeff[0];
return *this;
}
};
class RationalNumber
{
Polynomial *n;
Polynomial *d;
public:
RationalNumber( int = 0, int = 1); // constructor
RationalNumber(Polynomial&);
~RationalNumber();
RationalNumber operator+( const RationalNumber& ) const; // addition
RationalNumber operator-( const RationalNumber& ) const; // subtraction
RationalNumber operator*( const RationalNumber& ) const; // multiplication
RationalNumber operator/( const RationalNumber& ) const; // division
// relational operators
bool operator>( const RationalNumber& ) const; // greater than
bool operator<( const RationalNumber& ) const; // less than
bool operator>=( const RationalNumber& ) const; // greater than or equal to
bool operator<=( const RationalNumber& ) const; // less than or equal to
// equality operators
bool operator==( const RationalNumber & ) const; // equality operator
// inequality operator
bool operator!=( const RationalNumber &right ) const;
friend ostream& operator<< (ostream&, RationalNumber&); // output
void printRational() const; // display rational number
private:
int numerator; // private variable numerator
int denominator; // private variable denominator
void reduction(); // function for fraction reduction
int gcd( int a, int b )const;
}; // end class RationalNumber
// Polynomial constructor
Polynomial::Polynomial(const int a){
coeff = new int[1];
coeff[0] = a;
nCoeff = 1;
}
Polynomial::Polynomial(const int a, int *c){
nCoeff = a + 1;
coeff = new int[a+1];
for (int i = 0;i < (a+1); ++i){
coeff[i] = c[i];
}
}
Polynomial::Polynomial(Polynomial &p){
if (this != &p){
if(nCoeff >= 0) delete []coeff;
int size = p.getDegree();
coeff = new int[size+1];
for (int i = 0; i <= size; ++i) coeff[i] = p.coeff[i];
nCoeff = p.nCoeff;
}
}
Polynomial::~Polynomial(){
delete[]coeff;
}
const int Polynomial::getDegree(){
int i = nCoeff - 1;
while (coeff[i] == 0) --i;
return (i >= 0) ? i : 0;
}
void Polynomial::shrink(){
int i = getDegree();
while(coeff[i] == 0) --i;
int *temp = new int[i+1];
for (int j = 0; j <= i; ++j) temp[j] = coeff[j];
if (i < 0){
coeff = new int[1];
coeff[0] = 0;
nCoeff = 0;
}else{
coeff = new int[i+1];
for (int j = 0; j <= i; ++j) coeff[j] = temp[j];
}
}
int Polynomial::evaluate(int a){
double result = 0;
for (int i = 0; i < nCoeff; ++i) result += coeff[i] * pow((double)a,
(double)i);
return (int)result;
}
// RationalNumber constructor
RationalNumber::RationalNumber( int n, int d )
{
if (d == 0) {
denominator = 1;
numerator = 0;
} else if ( d < 0 ) {
denominator = -d;
numerator = -n;
} else {
denominator = d;
numerator = n;
}
reduction();
}
RationalNumber::~RationalNumber()
{
}
// returns greatest common divisor
int RationalNumber::gcd(int a, int b)const{
int temp = 0;
if ( a == 0)
return b;
else if (b == 0)
return a;
else if (a < b)
{
temp = b;
b = a;
a = temp;
return (gcd(b, a% b));
}
else
return (gcd(b, a% b));
} // end funtion gcd
// overloaded + operator
RationalNumber RationalNumber::operator+( const RationalNumber &operand2 ) const
{
RationalNumber t;
t.numerator = numerator * operand2.denominator; // (ad)
t.numerator += denominator * operand2.numerator; // (ad + bc)
t.denominator = denominator * operand2.denominator; // formula (ad + bc)/(bd)
t.reduction();
return t;
}
// overloaded - operator
RationalNumber RationalNumber::operator-( const RationalNumber &operand2 ) const
{
RationalNumber t;
t.numerator = numerator * operand2.denominator; // (ad)
t.numerator -= denominator * operand2.numerator; // (ad - bc)
t.denominator = denominator * operand2.denominator;//formula (ad - bc)/(bd)
t.reduction();
return t;
}
// overloaded * operator
RationalNumber RationalNumber::operator*( const RationalNumber &operand2 ) const
{
RationalNumber t;
t.numerator = numerator * operand2.numerator; // (ac)
t.denominator = denominator * operand2.denominator; // formula (ac)/(bd)
t.reduction();
return t;
}
// overloaded / operator
RationalNumber RationalNumber::operator/( const RationalNumber &operand2 ) const
{
RationalNumber t;
t.numerator = numerator * operand2.denominator;// (ad)
t.denominator = denominator * operand2.numerator; // formula (ad)/(bc)
t.reduction();
return t;
}
// overloaded > operator
bool RationalNumber::operator>( RationalNumber const &operand2 ) const
{
return (( numerator*operand2.denominator) > (denominator*operand2.numerator));
}
// overloaded < operator
bool RationalNumber::operator<( RationalNumber const &operand2 ) const
{
return (( numerator*operand2.denominator) < (denominator*operand2.numerator));
}
// overloaded >= operator
bool RationalNumber::operator>=( RationalNumber const& operand2 ) const
{
return (( numerator*operand2.denominator) >= (denominator*operand2.numerator));
}
// overloaded <= operator
bool RationalNumber::operator<=( RationalNumber const &operand2 ) const
{
return (( numerator*operand2.denominator) <= (denominator*operand2.numerator));
}
// overloaded == operator
bool RationalNumber::operator==( RationalNumber const& operand2 ) const
{
return (( numerator*operand2.denominator) == (denominator*operand2.numerator));
}
// overloaded != operator
bool RationalNumber::operator!=( RationalNumber const &operand2 ) const
{
return !( *this == operand2 );
}
// function printRational definition
ostream& operator<< (ostream& out, RationalNumber& r)
{
Polynomial *zero = new Polynomial(0);
if (*(r.n) == *zero){
out << "0";
}else{
out << *(r.n);
out << " / ";
out << *(r.d);
}
return out;
}
void RationalNumber::printRational() const
{
if ( numerator == 0 ) // print fraction as zero
cout << numerator;
else if ( denominator == 1 ) // print fraction as integer
cout << numerator;
else
cout << numerator << '/' << denominator;
} // end function printRational definition
// function reduction definition
void RationalNumber::reduction()
{
int factor = gcd(numerator, denominator);
numerator = numerator / factor;
denominator = denominator / factor;
} // end function reduction