Question

In: Computer Science

c++ please Your task is to write the implementation for a class of polynomial operations. Your...

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 

Solutions

Expert Solution

#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;
}


Related Solutions

Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that...
Task 1: [10 Marks] Write a function “reverse” in your queue class (linked list implementation) that reverses the whole queue. In your driver file (main.cpp), create an integer queue, push some values in it, call the reverse function to reverse the queue and then print the queue.
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. public class Polynomial { // P is a linear array of Terms private Term[ ] P; … // Re-implement the six methods of Polynomial (including the constructor) … }
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials...
Task : (Please Answer in C#) Class Polynomials The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List L; // Creates an empty list L of polynomials public Polynomials ( ) { … } // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) { … } // Inserts polynomial p into L public void Insert (Polynomial p) {...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of...
write a c++ program to perform the following operations on stack of Integers (Array Implementation of Stack with maximum size MAX) (i) Push an Element on to stack (ii) Pop an Element from stack (iii) Demonstrate how stack can be used to check Palindrome (iv) Display the status of stack (v) Exit
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class...
Using the following code write the following instructions in C++ Implementation: 1. Re-implement your String class to use a dynamically allocated array for storage. It will be a NULL terminating charater array. 2. This dynamic version of the String will only allocate exactly the amount of memory necessary to store the characters. That is, the length will be the same as the capacity. However, the size of the dynamic array needs to have an extra char for the NULL terminator....
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
Your task is to write a program in C or C++ that calculates the total amount...
Your task is to write a program in C or C++ that calculates the total amount of money a person has made over the last year. Your program will prompt the user for dollar amounts showing how much the user has made per job. Some users will have had more than one job, make sure your program allows for this. The program will print out the total made (all jobs) and will print out the federal and state taxes based...
Task 4 The class Polynomials is a collection of polynomials of either implementation stored in an...
Task 4 The class Polynomials is a collection of polynomials of either implementation stored in an instance of the generic library class List. class Polynomials { private List<Polynomial> L; // Creates an empty list L of polynomials public Polynomials ( ) { … } (1 mark) // Retrieves the polynomial stored at position i in L public Polynomial Retrieve (int i) (1 mark) { … } // Inserts polynomial p into L public void Insert (Polynomial p) (1 mark) {...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that...
C++ Task: You are to write a class called Monomial, using filenames monomial.h and monomial.cpp, that will allow creation and handling of univariate monomials, as described below. Monomial Description Each monomial object has following properties: Each monomial has a coefficient and a power. Monomial power is always positive. Monomial power is always integer. Monomials cannot be added if they have different powers Class Details The single constructor for the Monomial class should have 2 parameters: an floating point coefficient value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT