Am not able to display the polynomial. Thr some problem in display function. Please help me out in this.

#include "stdafx.h"
#include <iostream>
using namespace std;
//   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); //Subtract2 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<<"\nEnter 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->next!=NULL)
    {
       //ptr=start;

cout<<ptr->coe<<"X^"<<ptr->exp;
       ptr=ptr->next;
       if(ptr->next!=NULL)
     cout<<"+";
    }
    cout<<"\b\b ";
}


/*void polynomial::show()  // Show Polynomial
{
//       cout<<"Inside Show";
         struct node *ptr=start;
         //cout << next ;
    if(ptr)
        {
      cout<<"displaying polynomials"<<"\n";
        if(ptr->next<0)
        cout<<"-";
        while(ptr)
                {
                cout<<ptr->coe<<"X^"<<ptr->exp;
                ptr=ptr->next;
                if(ptr)
           if(ptr->coe > 0)
              cout << " + ";
                else
              cout << " - ";
        }
        cout << "\n";
    }
        else
        {
        cout << "Display Polynomial: Polynomial does not exist\n";
    }
}*/


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.\n";

   p1.get_poly();
   cout<<"\nSecond polynomial.\n";
   p2.get_poly();
   //clrscr();
   cout<<"\nThe First polynomial is: ";
   p1.show();
   cout<<"\nThe second polynomial is: ";
   p2.show();
   cout<<"\n\nThe sum of two polynomials is: ";
  sum.add(p1,p2);
   sum.show();
   cout<<"\n\nThe difference of two polynomials is: ";
   diff.subtract(p1,p2);
   diff.show();
   return 0;

//getch();
}

My guess is that you need to add a constructor which initialized all the pointers to NULL. As it is those pointers are uninitialized and point to some random place in memory and any linked list using them will fail

You have a good general idea of what you are doing. However, you need to pay attention to what type of thing you are handling, and where your pointers currently point.

In your show() function you have improved some. Let me add some commentary that might help you:

void polynomial::show()  // Show Polynomial
{
  struct node *ptr;  // good.
  ptr=start;  // good.
  while(ptr->next!=NULL)  // excellent
  {
    // good. prints the current node's coe, which is an integer,
    // followed by a string "X^",
    // followed by the current node's exp, which is an integer.
    // (See note 1)
    cout<<ptr->coe<<"X^"<<ptr->exp;

    // good. now the current node is the next term in the poly
    ptr=ptr->next;

    // but wait: I still think I am the term I was before the 
    // last statement. I should really be testing if the _current_
    // node pointer is null: if (ptr != NULL)
    // (See note 2)
    if(ptr->next!=NULL)
      // your first attempt here was very good. This could
      // read: if (ptr->coe < 0) cout << " - "; else cout << " + ";
      // remember that we already changed the current term
      // (see note 1)
      cout<<"+";
  }

  // (See note 3)
  cout<<"\b\b ";
}

Note 1
You are already aware that C++ will put a negative sign in front of a negative integer when you print it. Currently your polys will print as: 5X^2 + -4X^1 + 12X^0\b\b You could make it look like you wanted by:
1. before you enter the loop, put a minus sign in front of the entire thing if (start->coe < 0)
2. in the loop, only print the absolute value of ptr->coe
3. at the end of the loop, do as you did before and print a + or - depending on the sign of the next term
(I am presuming you wanted it to look like this: 5X^2 - 4X^1 + 12X^0\b\b Note 2
Pay attention to which term of the poly (or node) that ptr currently points to. I personally find it helpful to get out the construction paper and crayons and draw myself a poly stored in nodes, then use pennies or something to keep track of which node is currently referenced by ptr as I trace my code line by line.


Note 3
Obviously, you already know that x^1 is just x. So you are backing up over the ^1 and overwriting it with spaces.

A better way to do this would be just to check if ptr->exp is one and only print it if it is not: cout << abs( ptr->coe ) << "X"; if (ptr->exp != 1) cout << "^" << ptr->exp; There are two reasons to do it this way:
1. The last term might not be X to a power of 1.
2. Your output device might not understand the \b character (if it were, say, a file).

Likewise, I wonder if it is possible that ptr->coe is zero?
Also, don't forget that x^0 is 1. You might want to add an if in there for that too...


Well, that answers your question. You might want to think some more about your add and subtract functions --you have several fencepost errors in there.

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.