Polynomial Class with rational coefficients - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Polynomial Class with rational coefficients

DamienCurrDamienCurr Posts: 2Member
Good evening!

I am currently working with a program that does basic arithmetic with polynomials that can have rational coefficients. I was able to come up with seperate classes and implementation files for my two classes, Polynomial.h and Rational.h. I can run two different programs, one for each class, fine. I am trying to figure out how to use both classes in order to handle the rational coefficients.

The related files:

[code]

// Polynomial.h
//#include "Rational.h"
class Polynomial
{
public:
static const int maxTerms = 10; // maximum number of terms
Polynomial(); // constructor
Polynomial operator+(const Polynomial &) const; // addition
Polynomial operator-(const Polynomial &) const; // subtraction
Polynomial operator*(const Polynomial &) const; // multiplication
Polynomial &operator=(const Polynomial &); // assignment
Polynomial &operator+=(const Polynomial &);
Polynomial &operator-=(const Polynomial &);
Polynomial &operator*=(const Polynomial &);
void enterTerms();
void printPolynomial() const;
int getNumTerms() const;
int getTermExp(int) const;
int getTermCoeff(int) const;
void setCoeff(int, int);
~Polynomial(); // destructor

private:
int numTerms;
int exp[maxTerms]; // exponent array
int coeff[maxTerms]; // coefficients array
static void polyCombine(Polynomial &); // combine commn terms
}; // end class Polynomial

//=====================================================================

// Polynomial.cpp
#include
#include
#include "Polynomial.h"
//#include "Rational.h"
using namespace std;

Polynomial::Polynomial()
{
for(int t = 0; t < maxTerms; t++)
{
coeff[t] = 0;
exp[t] = 0;
} // end for
numTerms = 0;
} // end Polynomial constructor

Polynomial Polynomial::operator+(const Polynomial &r) const // addition
{
Polynomial temp;
bool expExists;
int s;

// process element with a zero exponent
temp.coeff[0] = coeff[0] + r.coeff[0];

// copy right arrays into temp object; s will be used to keep
// track of first open coefficient element
for(s = 1; (s < maxTerms) && (r.exp[s] != 0); s++)
{
temp.coeff[s] = r.coeff[s];
temp.exp[s] = r.exp[s];
} // end for
for(int x = 1; x < maxTerms; x++)
{
expExists = false; // assume exponent will not be found

for(int t = 1; (t < maxTerms) && (!expExists); t++)
if(exp[x] == temp.exp[t])
{
temp.coeff[t] += coeff[x];
expExists = true; // exponent found
} // end if
// exponent was found, insert into temp
if(!expExists)
{
temp.exp[s] = exp[x];
temp.coeff[s] += coeff[x];
s++;
} // end if
} // endfor
return temp;
} // end operator+

Polynomial Polynomial::operator-(const Polynomial &r) const // subtraction
{
Polynomial temp;
bool expExists;
int s;

// process element with zero exponent
temp.coeff[0] = coeff[0] - r.coeff[0];

// copy left arrays into temp object; s will be used to keep
// track of first open coefficient element
for(s = 1; (s < maxTerms) && (exp[s] != 0); s++)
{
temp.coeff[s] = coeff[s];
temp.exp[s] = exp[s];
} // end for
for (int x = 1; x < maxTerms; x++)
{
expExists = false;

for(int t = 1; (t < maxTerms) && (!expExists); t++)

if(r.exp[x] == temp.exp[t])
{
temp.coeff[t] -= r.coeff[x];
expExists = true;
}
if(!expExists)
{
temp.exp[s] = r.exp[x];
temp.coeff[s] -= r.coeff[x];
s++;
} // end if
} // end for
} // end operator- function

Polynomial Polynomial::operator*(const Polynomial &r) const // multiplication
{
Polynomial temp;
int s = 1;

for(int x = 0; (x < maxTerms) && (x == 0 || coeff[x] != 0); x++)

for(int y = 0; (y < maxTerms) && (y == 0 || r.coeff[y] != 0); y ++)

if(coeff[x] * r.coeff[y])

if((exp[x] == 0) && (r.exp[y] == 0))
temp.coeff[0] += coeff[x] * r.coeff[y];
else
{
temp.coeff[s] = coeff[x] * r.coeff[y];
temp.exp[s] = exp[x] + r.exp[y];
s++;
} // end else
polyCombine(temp); // combine common terms
return temp;
} // end operator* function


Polynomial &Polynomial::operator=(const Polynomial &r) // assignment
{
exp[0] = r.exp[0];
coeff[0] = r.coeff[0];

for(int s = 1; s < maxTerms; s++)
{
if(r.exp[s] != 0)
{
exp[s] = r.exp[s];
coeff[s] = r.coeff[s];
}
else
{
if(exp[s] == 0)
break;

exp[s] = 0;
coeff[s] = 0;
} // end else
} // end for
return *this;
} // end operator= function

Polynomial &Polynomial::operator+=(const Polynomial &r)
{
*this = *this + r;
return *this;
} // end operator+= function

Polynomial &Polynomial::operator-=(const Polynomial &r)
{
*this = *this -r;
return *this;
} // end operator-= function

Polynomial &Polynomial::operator*=(const Polynomial &r)
{
*this = *this * r;
return *this;
}// end operator*= function

void Polynomial::enterTerms()
{
bool found = false;
int c, e, term;

cout << endl << "Enter number of polynomial terms: ";
cin >> numTerms;

for(int n = 0; n < maxTerms && n < numTerms; n++)
{
cout << endl << "Enter coefficient: ";
cin >> c;
cout << "Enter exponent: ";
cin >> e;

if(c != 0)
{
// exponent of zero are forced into first element
if(e == 0)
{
coeff[0] += c;
continue;
} // end if
for(term = 1; (term < maxTerms) && (coeff[term] !=0); term++)

if(e == exp[term])
{
coeff[term] += c;
exp[term] = e;
found = true; // existing exponent updated
} // end if
if(!found) // add term
{
coeff[term] += c;
exp[term] = e;
} // end if
} // end outer if
} // end outer for
} // end enterTerms function

void Polynomial::printPolynomial() const
{
int start;
bool zero = false;

if(coeff[0]) // output constraints
{
cout << coeff[0];
start = 1;
zero = true; // at least one term exists
}
else
{
if(coeff[1])
{
cout << coeff[1] << 'x';
if((exp[1] != 0) && (exp[1] != 1))
cout << '^' << exp[1];
zero = true; // at least one term exists
} // end if
start = 2;
} // end else

// output remaining polynomial terms
for(int x = start; x < maxTerms; x++)
{
if(coeff[x] != 0)
{
cout << showpos << coeff[x] << noshowpos << 'x';

if((exp[x] != 0) && (exp[x] != 1))
cout << '^' << exp[x];

zero = true; // at least one term exists
} // end if
} // end for
if(!zero) // no terms exist in the polynomial
cout << '0';
cout << endl;
} // end printPolynomial function

int Polynomial::getNumTerms() const
{
return numTerms;
} // end getTerms function

int Polynomial::getTermExp(int term) const
{
return exp[term];
} // end getTermExp function

int Polynomial::getTermCoeff(int term) const
{
return coeff[term];
} // end getTermCoeff function

void Polynomial::setCoeff(int term, int coeffs)
{
if(coeff[term] == 0) // no term at this location
cout << "No term at this location, cannot see term." << endl;
else // otherwise set term
coeff[term] = coeffs;
} // end setCoeff function

void Polynomial::polyCombine(Polynomial &w)
{
Polynomial temp = w;

// zero out elements of w
for(int x = 0; x < maxTerms; x++)
{
w.coeff[x] = 0;
w.exp[x] = 0;
}
for(int x = 1; x < maxTerms; x++)
{
for(int y = x + 1; y < maxTerms; y++)
if(temp.exp[x] == temp.exp[y])
{
temp.coeff[x] += temp.coeff[y];
temp.exp[y] = 0;
temp.coeff[y] = 0;
}
}
w = temp;
} // end polyCombine function

Polynomial::~Polynomial() // destructor
{
// empty
}

[/code]


[code]


// Rational.h
#include <iostream>
using namespace std;
class Rational
{
private:
int num;
int denom;

public:
void setNum(int);
void setDenom(int);
int getNum();
int getDenom();

int gcd();
void simplify();

Rational operator+(Rational);
Rational operator-(Rational);
Rational operator*(Rational);
Rational operator/(Rational);
bool operator==(Rational);
bool operator!=(Rational);
bool operator>(Rational);
bool operator<(Rational);
bool operator>=(Rational);
bool operator<=(Rational);
};

//=====================================================================

// Rational.cpp
#include <iostream>
#include "Rational.h"
using namespace std;

void Rational::setNum(int n)
{
num = n;
}

void Rational::setDenom(int n)
{
denom = n;
}

int Rational::getNum()
{
return num;
}
int Rational::getDenom()
{
return denom;
}

int Rational::gcd()
{
int x = num;
int y = denom;
int temp;

while (y)
{
temp = y;
y = x % y;
x = temp;
}
return x;
}

void Rational::simplify()
{
int gcdNum = gcd();

if(gcdNum != 0)
{
num = num / gcdNum;
denom = denom / gcdNum;
}
}

Rational Rational::operator+(Rational passed)
{
Rational Result;

Result.num = num * passed.denom + denom * passed.num;
Result.denom = denom * passed.denom;

return Result;
}

Rational Rational::operator-(Rational passed)
{
Rational Result;

Result.num = num * passed.denom - denom * passed.num;
Result.denom = denom * passed.denom;

return Result;
}

Rational Rational::operator*(Rational passed)
{
Rational Result;

Result.num = num * passed.num;
Result.denom = denom * passed.denom;

return Result;
}


Rational Rational::operator/(Rational passed)
{
Rational Result;
Result.num = num * passed.denom;
Result.denom = denom * passed.num;

return Result;
}

bool Rational::operator==(Rational passed)
{
simplify();
passed.simplify();

return (passed.num == num) && (passed.denom == denom);
}

bool Rational::operator!=(Rational passed)
{
simplify();
passed.simplify();

return (passed.num != num) && (passed.denom != denom);
}

bool Rational::operator>(Rational passed)
{

simplify();
passed.simplify();

return (num * passed.denom) > (passed.num * denom);
}

bool Rational::operator<(Rational passed)
{
simplify();
passed.simplify();

return (num * passed.denom) < (passed.num * denom);
}

bool Rational::operator>=(Rational passed)
{

simplify();
passed.simplify();

return (num * passed.denom) >= (passed.num * denom);
}

bool Rational::operator<=(Rational passed)
{
simplify();
passed.simplify();

return (num * passed.denom) <= (passed.num * denom);
}

//=====================================================================

[/code]


Suggestions?

Thanks!
Sign In or Register to comment.