I have to write a polynomial class linked list program and i
I have to write a polynomial class linked list program and i do not know what is wrong with my code. Can someone please help. It is about adding, multiplying and subtraction polynomials. c++. please change it as needed
#ifndef POLY_H
 #define POLY_H
 #include <iostream>
 #include <iomanip>
 #include <ostream>
 using namespace std;
 struct TermNode
 {
 int exp; //exponent
 double coef; //coefficient
 TermNode * next;
 };
 class PolyType
 {
 public:
 PolyType();
 PolyType(int r, double c);
 PolyType(const PolyType &rhs);
 ~PolyType();
 PolyType operator=(const PolyType &); //assignment
 PolyType operator+ (const PolyType &rhs) const; //returns sum of parameter + self
 PolyType operator* (const PolyType &rhs) const;
 PolyType differ();
 PolyType integr();
 friend ostream & operator<< (ostream & out, const PolyType & rhs); //coefficients printed to a 2 decimal places
 private:
 TermNode *polyPtr;
 };
 PolyType::PolyType() {
 polyPtr = nullptr;
 }
 PolyType::~PolyType() {
 delete polyPtr;
 polyPtr = nullptr;
 }
 PolyType::PolyType(int r, double c) {
 polyPtr = new TermNode();
 polyPtr->exp = c;
 polyPtr->coef = r;
 }
 PolyType::PolyType(const PolyType & rhs) {
 polyPtr = rhs.polyPtr;
 }
 PolyType PolyType::operator+(const PolyType & rhs) const {
 PolyType temp;
 if (this->polyPtr->exp >= rhs.polyPtr->exp) {
   temp.polyPtr = this->polyPtr;
   temp.polyPtr->next = rhs.polyPtr;
 }
 else {
   temp.polyPtr = rhs.polyPtr;
   temp.polyPtr->next = this->polyPtr;
 }
 return temp;
 }
 PolyType PolyType::operator=(const PolyType &rhs)
 {
 delete polyPtr;
 TermNode * orig = rhs.polyPtr;
 TermNode * newP = nullptr;
 polyPtr = nullptr;
 while (orig != nullptr)
 {
   if (newP == nullptr)
   {
    polyPtr = new TermNode;
    polyPtr->exp = orig->exp;
    polyPtr->coef = orig->coef;
    polyPtr->next = nullptr;
    newP = polyPtr;
   }
   else
   {
    newP->next = new TermNode;
    newP = newP->next;
    newP->exp = orig->exp;
    newP->coef = orig->coef;
    newP->next = nullptr;
   }
   orig = orig->next;
 }
 return *this;
 }
 PolyType PolyType::operator*(const PolyType &rhs) const
 {
 PolyType result;
 TermNode * ptr = polyPtr;
 while (ptr != nullptr)
 {
   result.polyPtr->exp = ptr->exp * rhs.polyPtr->exp;
   result.polyPtr->coef = ptr->coef * rhs.polyPtr->coef;
   ptr = ptr->next;
 }
 return result;
 }
 ostream & operator<<(ostream & out, const PolyType & rhs) {
 TermNode *currentPtr = rhs.polyPtr;
 out << currentPtr->coef << \"^\" << currentPtr->exp;
 currentPtr = currentPtr->next;
 while (currentPtr != nullptr) {
   out << \" + \" << currentPtr->coef << \"^\" << currentPtr->exp;
   currentPtr = currentPtr->next;
 }
 return out;
 }
 PolyType PolyType::integr() {
 PolyType temp;
 TermNode *currentPtr = this->polyPtr;
 TermNode *tempPtr = temp.polyPtr;
 while (currentPtr != nullptr) {
   tempPtr = new TermNode();
   tempPtr->coef = currentPtr->coef / (currentPtr->exp + 1);
   tempPtr->exp = currentPtr->exp + 1;
   currentPtr = currentPtr->next;
   tempPtr = tempPtr->next;
 }
 return temp;
 }
 PolyType PolyType::differ() {
 PolyType temp;
 TermNode *currentPtr = this->polyPtr;
 TermNode *tempPtr = temp.polyPtr;
 while (currentPtr != nullptr) {
   tempPtr = new TermNode();
   tempPtr->coef = currentPtr->coef * currentPtr->exp;
   tempPtr->exp = currentPtr->exp - 1;
   currentPtr = currentPtr->next;
   tempPtr = tempPtr->next;
 }
 return temp;
 }
 #endif
 int main() {
 PolyType poly1; //makes a null polynomial
 PolyType poly2(2, 3); // makes the polynomial 2.00x^3
 PolyType 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
 system(\"pause\");
 return 0;
 }
Solution
I have written it for addition and substraction. Hope you\'ll get an idea:
#include<iostream.h>
 #include<conio.h>
 #include<process.h>
// Creating a NODE Structure
 struct node
 {
 int coe,exp; // data
 struct node *next; // link to next node and previous node
 };
// Creating a class Polynomial
 class polynomial
 {
 struct node *start,*ptrn,*ptrp;
 public:
 void get_poly(); // to get a polynomial
 void show(); // show
 void add(polynomial p1,polynomial p2); // Add two polynomials
 void subtract(polynomial p1,polynomial p2); //Subtract 2 polynomials
 };
void polynomial::get_poly() // Get Polynomial
 {
 char c=\'y\';
 ptrn=ptrp=start=NULL;
 while(c==\'y\' || c==\'Y\')
 {
 ptrn=new node;
 ptrp->next=ptrn;
 if(start==NULL)
    start=ptrn;
 ptrp=ptrn;
 cout<<\"
 Enter the coefficient: \";
 cin>>ptrn->coe;
 cout<<\"Enter the exponent: \";
 cin>>ptrn->exp;
 ptrn->next=NULL;
 cout<<\"Enter y to add more nodes: \";
 cin>>c;
 }
 return;
 }
 void polynomial::show() // Show Polynomial
 {
 struct node *ptr;
 ptr=start;
 while(ptr!=NULL)
 {
 cout<<ptr->coe<<\"X^\"<<ptr->exp<<\" + \";
 ptr=ptr->next;
 }
 cout<<\" \";
 }
void polynomial::add(polynomial p1,polynomial p2) // Add Polynomials
 {
 struct node *p1ptr,*p2ptr;
 int coe,exp;
 ptrn=ptrp=start=NULL;
 p1ptr=p1.start;
 p2ptr=p2.start;
 while(p1ptr!=NULL && p2ptr!=NULL)
 {
 if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
 {
    coe=p1ptr->coe+p2ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    p2ptr=p2ptr->next;
 }
 else if(p1ptr->exp>p2ptr->exp)
 {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
 }
 else if(p1ptr->exp<p2ptr->exp)
 {
    coe=p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
 }
 ptrn=new node;
 if(start==NULL)
    start=ptrn;
 ptrn->coe=coe;
 ptrn->exp=exp;
 ptrn->next=NULL;
 ptrp->next=ptrn;
 ptrp=ptrn;
 } // End of While
 if(p1ptr==NULL)
 {
 while(p2ptr!=NULL)
 {
    coe=p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
 }
 }
 else if(p2ptr==NULL)
 {
 while(p1ptr!=NULL)
 {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
 }
 }
 } // End of addition
// Subtract two polynomials
 void polynomial::subtract(polynomial p1,polynomial p2) // Subtract
 {
 struct node *p1ptr,*p2ptr;
 int coe,exp;
 ptrn=ptrp=start=NULL;
 p1ptr=p1.start;
 p2ptr=p2.start;
 while(p1ptr!=NULL && p2ptr!=NULL)
 {
 if(p1ptr->exp==p2ptr->exp) // If coefficients are equal
 {
    coe=p1ptr->coe-p2ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    p2ptr=p2ptr->next;
 }
 else if(p1ptr->exp>p2ptr->exp)
 {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
 }
 else if(p1ptr->exp<p2ptr->exp)
 {
    coe=0-p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
 }
 ptrn=new node;
 if(start==NULL)
    start=ptrn;
 ptrn->coe=coe;
 ptrn->exp=exp;
 ptrn->next=NULL;
 ptrp->next=ptrn;
 ptrp=ptrn;
 } // End of While
 if(p1ptr==NULL)
 {
 while(p2ptr!=NULL)
 {
    coe=0-p2ptr->coe;
    exp=p2ptr->exp;
    p2ptr=p2ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
 }
 }
 else if(p2ptr==NULL)
 {
 while(p1ptr!=NULL)
 {
    coe=p1ptr->coe;
    exp=p1ptr->exp;
    p1ptr=p1ptr->next;
    ptrn=new node;
    if(start==NULL)
    start=ptrn;
    ptrn->coe=coe;
    ptrn->exp=exp;
    ptrn->next=NULL;
    ptrp->next=ptrn;
    ptrp=ptrn;
 }
 }
 } // End of subtraction
 int main()
 {
 clrscr();
 polynomial p1,p2,sum,diff;
 cout<<\"First Polynomial.
 \";
 p1.get_poly();
 cout<<\"
 Second polynomial.
 \";
 p2.get_poly();
 clrscr();
 cout<<\"
 The First polynomial is: \";
 p1.show();
 cout<<\"
 The second polynomial is: \";
 p2.show();
 cout<<\"
The sum of two polynomials is: \";
 sum.add(p1,p2);
 sum.show();
 cout<<\"
The difference of two polynomials is: \";
 diff.subtract(p1,p2);
 diff.show();
 getch();
 return 0;
 }
Kindly provide feedback.







