So I need to create two classes one for Monomials. The other for polynomials (built using a linked list of Monomials).
I have the function declarations done and now I need to get into the definitions, which is where I'm stuck.
I think if I can get some direction on one function I'll be able to write the rest. Here is the direction I'm going with it, not sure if this is right. If it isn't can you point me in the right direction? Never used a the iterator thing so that's really throwing me off. Here is my header
#ifndef __POLYNOMIAL_H_
#define __POLYNOMIAL_H_
#include <iostream>
#include <list>
#include "Polynomial.cpp"
#define var = 'x' //varible to use in polynomial
using namespace std;
struct ZERO_T {
static const short ZERO = 0;
};
template <typename NumT>
class Monomial
{
public:
Monomial<NumT>(NumT c = 0, int d = 0) : coeff(c), expo(d)
{ };
NumT coefficient(void) const { return coeff; };
int degree(void) const { return expo; };
void assign_coefficient(const NumT c) { coeff = c; };
void assign_degree(const int d) { expo = d; };
bool operator==(const Monomial<NumT> & m) const
{
return (coeff == m.coeff && expo == m.expo);};
bool operator!=(const Monomial<NumT> & m) const
{return (coeff != m.coeff || expo != m.expo);};
private:
NumT coeff;
int expo;
};
template <typename NumberType>
class Polynomial : public ZERO_T
{
public:
Polynomial<NumberType>(NumberType c = 0, int d = 0)
{
const Monomial<NumberType> m(c, d);
term_list.push_back(m); //creates a ZERO monomial
number_of_terms = 1;
highest_degree = d;
}
Polynomial<NumberType>(const Monomial<NumberType> & m) //type conversion constructor
{
term_list.push_back(m);
number_of_terms = 1;
highest_degree = m.degree();
}
~Polynomial<NumberType>() { } // use default destructors and list's destructor
Polynomial<NumberType>(const Polynomial<NumberType> & rhs) // copy constructor
: term_list(rhs.term_list), number_of_terms(rhs.number_of_terms),
highest_degree(rhs.highest_degree) { }
const Polynomial<NumberType> & operator=(const Polynomial<NumberType> & rhs);
//copy assignment
Polynomial<NumberType> operator+=(const Monomial<NumberType> &m);
Polynomial<NumberType> operator+=(const Polynomial<NumberType> & rhs);
const Polynomial<NumberType> operator+(const Monomial<NumberType> &m)const;
const Polynomial<NumberType> operator+(const Polynomial<NumberType> & rhs) const;
Polynomial<NumberType> operator*=(const Monomial<NumberType> &m);
Polynomial<NumberType> operator*=(const Polynomial<NumberType> & rhs);
const Polynomial<NumberType> operator*(const Monomial<NumberType> &m) const;
const Polynomial<NumberType> operator*(constPolynomial<NumberType> & rhs) const;
const NumberType evaluate(NumberType x);
bool operator==(const Polynomial<NumberType> &p) const;
bool operator!=(const Polynomial<NumberType> &p) const;
const int gethighestdegree();
void read(istream & in = cin);
void print(ostream & out = cout) const;
//static Polynomial<NumberType> ZERO;
private:
list<Monomial<NumberType> > term_list;
//sorted by decreasing degrees
int number_of_terms;
int highest_degree;
void insert_in_poly(Polynomial<NumberType> & p, const Monomial<NumberType> & m);
//helper function
NumberType power(NumberType x, int n);
};
//template<> Polynomial<int> Polynomial<int>::ZERO = 0;
template <typename NumberType>
istream&operator>>(istream & in, Polynomial<NumberType> & rhs);
template <typename NumberType>
ostream& operator<<(ostream & out, const Polynomial<NumberType> & rhs);
#include "polynomial.cpp"
#endif
How would you do the add? Here's what I have so far:
#ifndef __POLYNOMIAL_CPP_
#define __POLYNOMIAL_CPP_
#include <iostream>
#include <cmath>
#include <list>
#include "polynomial.h"
#endif
template<typename NumberType>
const Polynomial<NumberType> Polynomial<NumberType>::operator+(const Monomial<NumberType>& m) const
{
int i = 0;
term_list.sort(); //sort list in decending order
term_list.reverse(); //rearrange into ascending order
Monomial temp; //create a temp object to hold our monomial
temp.coeff = m.coeff;
temp.expo = m.expo;
/////////////////experiments here/////////
list <Monomial<NumberType>>::iterator itr;
itr = p.termlist.begin();
while (itr != p.termlist.end()) //traverse termlist until we reach end
{
if (itr.expo == temp.expo) //if temp exponents meet current list exponent
itr insert(i, NumberType& temp); //insert monomial here
else
i++;
}
Obviously I still need to doctor it up with what if the list is empty and what if the exponent match is never found and such. But for now I just want a simple add monomial to polynomial function to use as an example to build my other functions please.