In: Computer Science
c++ please
Your task is to write the implementation for a class of polynomial operations. Your will write the code for: 2 constructors, a destructor, print, addition, multiplication , differentiation and integration of polynomials. The polynomials will be comprised of linked TermNodes.
struct TermNode
{
int   exp; // exponent
double coef; // coefficient
TermNode * next;
};
class Polynomial
{
  public:
     Polynomial ();              // default constructor 
     Polynomial (int r, int c);                      // constructor makes a 1 node polynomial
    Polynomial(const Polynomial & );    // copy constructor
     ~Polynomial ();                 // destructor
                                           
     Polynomial   operator=(const Polynomial &); // assignment
     Polynomial   operator+ (const Polynomial & ) const;     // returns sum of the parameter + self 
     Polynomial   operator* (const Polynomial & ) const;
     Polynomial   differentiation();
     Polynomial   integration ();// (with 0 as the constant)              
     friend ofstream &   operator<< (ofstream & out, const Polynomial & rhs);           // coefficients printed to 2 decimal places
  private:
     TermNode  *firstTerm;  
     
};
Example Use of functions
Polynomial poly1; //makes a null polynomial 
Polynomial poly2(2,3); // makes the polynomial 2.00x^3 
Polynomial poly3(3,4); // makes the polynomial 3.00x^4 
poly1 = poly2 + poly3; // makes poly1 = 3.00x^4 + 2.00x^3 
cout<<poly1<<endl;  // prints out 3.0x^4 + 2.00x^3 
poly3 = poly2*poly1; // sets poly3 to 6.0x^7+4.00x^6 
poly4 = poly3.differentiation(); // sets poly4 to 42.00x^6+24.00x^5 
poly5 = poly1.integration(); // sets poly5 to .60x^5+.50x^4 
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct TermNode
{
int exp; // exponent
double coef; // coefficient
TermNode * next;
};
class Polynomial
{
public:
Polynomial ();
Polynomial (double r, int c);
Polynomial(const Polynomial & );
~Polynomial ();
Polynomial operator=(const Polynomial &);
Polynomial operator+ (const Polynomial & ) const;
Polynomial operator* (const Polynomial & ) const;
Polynomial differentiation();
Polynomial integration ();
friend ostream & operator<< (ostream & out, const
Polynomial & rhs);
private:
TermNode *firstTerm;
};
// default constructor
Polynomial::Polynomial()
{
   // initialize firstTerm to null
   this->firstTerm = NULL;
}
// Parameterized Constructor
Polynomial::Polynomial(double coeff , int exp)
{
   // declare and define node
   TermNode *node = new TermNode;
   //set exp
   node->exp = exp;
   //set coeff
   node->coef = coeff;
   //set next node to null
   node->next = NULL;
   // initialize firsstTerm to Node
   this->firstTerm = node;
}
// copy constructor
Polynomial::Polynomial(const Polynomial &p)
{
   TermNode *node, *prevNode = NULL, * head = NULL, *
pNode;
   pNode = p.firstTerm;
   while(pNode != NULL)
   {
       node = new TermNode;
       if(head == NULL)
       {
           head =
node;
       }
       if(prevNode != NULL)
       {
          
prevNode->next = node;
       }
       node->exp = pNode->exp;
       node->coef =
pNode->coef;
       node->next = NULL;
       prevNode = node;
       pNode = pNode->next;
   }
   this->firstTerm = head;
}
// Destructor
Polynomial::~Polynomial()
{
   delete firstTerm;
}
// Operator overloading: +
Polynomial Polynomial::operator+(const Polynomial &p)
const
{
   TermNode * term_p1, * term_p2;
   TermNode *pNode,*prevNode = NULL, * head = NULL;
   Polynomial newP;
   // first term node of first polynomial
   term_p1 = this->firstTerm;
   // first term node of second polynomial
   term_p2 = p.firstTerm;
   // both nodes are not null
   while(term_p1!= NULL && term_p2 != NULL)
   {
       pNode = new TermNode;
       pNode->next = NULL;
       // if head is null set head as
current node
       if(head == NULL)
       {
           head =
pNode;
       }
       // if exponents are same add the
terms
       if(term_p1->exp ==
term_p2->exp)
       {
           pNode->exp =
term_p1->exp;
           pNode->coef =
term_p1->coef + term_p2->coef;
           term_p1 =
term_p1->next;
           term_p2 =
term_p2->next;
       }
       // else add the largest exponent to
the list
       // and pass lowest to next
iteration
       else if(term_p1->exp >
term_p2->exp)
       {
           pNode->exp =
term_p1->exp;
           pNode->coef =
term_p1->coef;
           term_p1 =
term_p1->next;
       }
       else
       {
           pNode->exp =
term_p2->exp;
           pNode->coef =
term_p2->coef;
           term_p2 =
term_p2->next;
       }
       // set next of previous node
       if(prevNode != NULL)
       {
          
prevNode->next = pNode;
       }
       // set current node as previous
node
       prevNode = pNode;
   }
   // add the remaining terms to the chain
   while(term_p1 != NULL)
   {
       pNode = new TermNode;
       pNode->next = NULL;
       if(prevNode != NULL)
       {
          
prevNode->next = pNode;
       }
       prevNode = pNode;
       pNode->exp =
term_p1->exp;
       pNode->coef =
term_p1->coef;
       term_p1 = term_p1->next;
   }
   // add the remaining terms to the chain
   while(term_p2 != NULL)
   {
       pNode = new TermNode;
       pNode->next = NULL;
       if(prevNode != NULL)
       {
          
prevNode->next = pNode;
       }
       pNode->exp =
term_p2->exp;
       pNode->coef =
term_p2->coef;
       term_p2 = term_p2->next;
   }
   newP.firstTerm = head;
   return newP;
}
// Operator overloading: *
Polynomial Polynomial::operator*(const Polynomial &p)
const
{
   TermNode * term_p1, * term_p2;
   TermNode *pNode,*prevNode = NULL, * head = NULL;
   Polynomial newP;
   term_p1 = this->firstTerm;
   term_p2 = p.firstTerm;
   // if 1st polynomial is null, assign the second as
result
   if(term_p1 == NULL)
   {
       newP = p;
   }
   // if second is null, assign the first as result
   else if(term_p2 == NULL)
   {
       newP = *this;
   }
   // if both are not null, perform operation
   else
   {
       // for each term in polynomial
1
       while(term_p1 != NULL)
       {
           term_p2 =
p.firstTerm;
           // for each term
in polynomial 2
           while(term_p2 !=
NULL)
           {
          
    pNode = new TermNode;
          
    pNode->next = NULL;
          
    if(head == NULL)
          
    {
          
        head= pNode;
          
    }
          
    if(prevNode != NULL)
          
    {
          
        prevNode->next =
pNode;
          
    }
          
    prevNode = pNode;
          
    // multiply coeffs
          
    pNode->coef =
term_p1->coef*term_p2->coef;
          
    // add exponents
          
    pNode->exp =
term_p1->exp+term_p2->exp;
          
    term_p2 = term_p2->next;
           }
           term_p1 =
term_p1->next;
       }
   }
   newP.firstTerm = head;
   return newP;
}
// Operator overloading: =
Polynomial Polynomial::operator=(const Polynomial &p)
{
   TermNode *node, *prevNode = NULL, * head = NULL, *
pNode;
   pNode = p.firstTerm;
   while(pNode != NULL)
   {
       node = new TermNode;
       node->exp = pNode->exp;
       node->coef =
pNode->coef;
       node->next = NULL;
       if(head == NULL)
       {
           head =
node;
       }
       if(prevNode != NULL)
       {
          
prevNode->next = node;
       }
       // set previous node as next
       prevNode = node;
       // get next node
       pNode = pNode->next;
   }
   this->firstTerm = head;
   return *this;
}
// Operator overlaoding: <<
ostream & operator<<(ostream &out, const Polynomial
&rhs)
{
   TermNode *node;
   node = rhs.firstTerm;
   out <<fixed<<setprecision(2);
   while(node != NULL)
   {
       out <<node->coef;
       if(node->exp!=0)
       {
          
out<<"x^"<<node->exp;
       }
       // get next node
       node = node->next;
       if(node != NULL &&
node->coef > 0)
       {
          
out<<"+";
       }
   }
   return out;
}
Polynomial Polynomial::differentiation()
{
   Polynomial newP;
   TermNode *pNode, *node , *head = NULL,
*prevNode=NULL;
   pNode = this->firstTerm;
   while(pNode != NULL)
   {
       // differentiation performed only
for exponent terms. constants become 0
       // x^1 will become constant
       if(pNode->exp != 0)
       {
           node = new
TermNode;
           if(head ==
NULL)
           {
          
    head = node;
           }
           // set
coefficient;
           node->coef =
pNode->coef*pNode->exp;
           // set exponent
to exponent - 1
           node->exp =
(pNode->exp - 1);
           // set next to
null
           node->next =
NULL;
           // if previous
node is not null , set current node as
           // next to the
previous
           if(prevNode !=
NULL)
           {
          
    prevNode->next = node;
           }
           // set previous
node as next
           prevNode =
node;
       }
       pNode = pNode->next;
   }
   newP.firstTerm = head;
   return newP;
}
Polynomial Polynomial::integration ()
{
   Polynomial newP;
   TermNode *pNode, *node , *head = NULL,
*prevNode=NULL;
   pNode = this->firstTerm;
   while(pNode != NULL)
   {
       node = new TermNode;
       if(head == NULL)
       {
           head =
node;
       }
       // set coefficient;
       node->coef =
pNode->coef/(pNode->exp+1);
       // set exponent to exponent -
1
       node->exp = (pNode->exp +
1);
       node->next = NULL;
       if(prevNode != NULL)
       {
          
prevNode->next = node;
       }
       // set previous node as next
       prevNode = node;
       pNode = pNode->next;
   }
   newP.firstTerm = head;
   return newP;
}
int main()
{
   Polynomial poly1,poly4,poly5; //makes a null
polynomial
   Polynomial poly2(2,3); // makes the polynomial
2.00x^3
   cout<<poly2<<endl;
   Polynomial poly3(3,4); // makes the polynomial
3.00x^4
   poly1 = poly2 + poly3; // makes poly1 = 3.00x^4 +
2.00x^3
   cout<<poly1<<endl; // prints out 3.0x^4 +
2.00x^3
   poly3 = poly2*poly1; // sets poly3 to
6.0x^7+4.00x^6
   cout<<poly3<<endl;
   poly4 = poly3.differentiation(); // sets poly4 to
42.00x^6+24.00x^5
   cout<<poly4<<endl;
   poly5 = poly1.integration(); // sets poly5 to
.60x^5+.50x^4
   cout<<poly5<<endl;
}










