Hi, I need help implementing a class that represents a polynomial in C++ languag
ID: 3804806 • Letter: H
Question
Hi,
I need help implementing a class that represents a polynomial in C++ language. I would prefer if the class noted below was used for implemenation.
Must have:
3 constructors
A default constructor (no parameters)
A constructor that takes two parameters: exponent and coefficient
A Copy Constructor
A destructor
A print function
Operators =,+,*
Specification file:
#ifndef CPOLYNOMIAL_H #define CPOLYNOMIAL_H
struct termNode
{
int exp; // exponent
int coef; // coefficient
termNode * next;
};
class CPolynomial
{
public:
CPolynomial (); // default constructor
CPolynomial (int r, int c);
// constructor makes a 1 node polynomial
CPolynomial (const CPolynomial & ); // copy constructor
~ CPolynomial (); // destructor
void print(); // prints out the polynomial in descending order
CPolynomial& operator=(const CPolynomial &); // equals
CPolynomial& operator+ (const CPolynomial & ) const; // returns sum of the parameter + self
CPolynomial& operator* (const CPolynomial & ) const;
private:
termNode *polyPtr;
};
#endif
This woudl be the sample main fucntion:
int main()
{
CPolynomial poly1; //creates a null polynomial
CPolynomial poly2(2, 3); //creates 2x^3 polynomial
CPolynomial poly3(3, 4); //creates 3x^4 polynomial
poly1 = poly2 + poly3; //makes poly1 = 3x^4 + 2x^3
poly1.print(); //prints out 3x^4 + 2x^3
poly3 = poly2 * poly1; //sets poly3 to 6x^7 + 4x^6
return 0;
}
#ifndef CPOLYNOMIAL_H #define CPOLYNOMIAL_H
struct termNode
{
int exp; // exponent
int coef; // coefficient
termNode * next;
};
class CPolynomial
{
public:
CPolynomial (); // default constructor
CPolynomial (int r, int c);
// constructor makes a 1 node polynomial
CPolynomial (const CPolynomial & ); // copy constructor
~ CPolynomial (); // destructor
void print(); // prints out the polynomial in descending order
CPolynomial& operator=(const CPolynomial &); // equals
CPolynomial& operator+ (const CPolynomial & ) const; // returns sum of the parameter + self
CPolynomial& operator* (const CPolynomial & ) const;
private:
termNode *polyPtr;
};
#endif
This woudl be the sample main fucntion:
int main()
{
CPolynomial poly1; //creates a null polynomial
CPolynomial poly2(2, 3); //creates 2x^3 polynomial
CPolynomial poly3(3, 4); //creates 3x^4 polynomial
poly1 = poly2 + poly3; //makes poly1 = 3x^4 + 2x^3
poly1.print(); //prints out 3x^4 + 2x^3
poly3 = poly2 * poly1; //sets poly3 to 6x^7 + 4x^6
return 0;
}
Explanation / Answer
#include<iostream>
#include<cstdlib>
using namespace std;
/** without multipication **/
struct node
{
int exp;
int coef;
node *next;
};
class CPolynomial
{
public:
node *start;
CPolynomial()
{
start = NULL;
}
CPolynomial(int, int);
~CPolynomial();
CPolynomial(const CPolynomial&);
CPolynomial operator+(const CPolynomial&);
node *makenode(int, int);
void module(node **, node **, int, int);
void print();
};
void CPolynomial::print()
{
node *temp = start;
cout<<endl;
while(temp)
{
cout<<temp->coef<<"x^"<<temp->exp;
temp = temp->next;
if(temp)
cout<<" + ";
}
cout<<endl;
}
void CPolynomial::module(node **start, node **s, int c, int e)
{
if(*start == NULL)
{
*start = *s = makenode(c, e);
}
else
{
(*s)->next = makenode(c,e);
}
}
CPolynomial CPolynomial::operator+(const CPolynomial &c)
{
CPolynomial r;
node *a = start, *b = c.start, *s = NULL;
while(a != NULL && b != NULL)
{
if(a->exp > b->exp)
{
module(&(r.start), &s, a->coef, a->exp);
a = a->next;
}
else if(a->exp == b->exp)
{
if(a->coef+b->coef != 0)
{
module(&(r.start), &s,a->coef+b->coef,a->exp);
}
a = a->next;
b = b->next;
}
else
{
module(&(r.start), &s, b->coef, b->exp);
b = b->next;
}
}
while(a != NULL)
{
module(&(r.start), &s, a->coef, a->exp);
a = a->next;
}
while(b != NULL)
{
module(&(r.start), &s, b->coef, b->exp);
b = b->next;
}
return r;
}
CPolynomial::CPolynomial(const CPolynomial& c)
{
node *temp = c.start, *s = NULL;
while(temp)
{
if(start == NULL)
{
start = s = makenode(temp->coef, temp->exp);
}
else
{
s->next = makenode(temp->coef, temp->exp);
s = s->next;
}
temp = temp->next;
}
}
CPolynomial::~CPolynomial()
{
node *temp = NULL;
while(start)
{
temp = start;
start = start->next;
temp->next = NULL;
delete temp;
temp = NULL;
}
}
CPolynomial::CPolynomial(int c, int e)
{
start = makenode(c, e);
}
node *CPolynomial::makenode(int c, int e)
{
node *temp = NULL;
try
{
temp = new node;
}catch(bad_alloc xa)
{
cout<<" Unable to allocate memory ";
exit(-1);
}
temp->exp = e, temp->coef = c, temp->next = NULL;
return temp;
}
int main()
{
CPolynomial p1, p2(2,3), p3(3,4);
p1.print();
p2.print();
p3.print();
CPolynomial p4 = p2 + p3;
p4.print();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.