c++ - Infix to Postfix code conversion -


i have problem convert infix postfix code takes characterinfix input not show postfix output please tell me whats problem.i have been trying solve found no problem in full if find problem.and other thing how may add {} , [] brackets ?

#include <iostream> #include<string.h> using namespace std;  template <class t> class node {     public:         t info;         node *ptrnext;         node *ptrprevious;         node()         {             info = 0;             ptrnext = 0;             ptrprevious = 0;         }         node(t e, node *n, node *p)         {             info = e;             ptrnext = n;             ptrprevious = p;         } };  template <class t> class dlinkedlist {     private:         node<t> *head;         node<t> *tail;     public:         dlinkedlist()         {             head = 0;             tail = 0;         }         bool isempty();         void addtohead(t e);         void addtotail(t e);         void deletefromhead();         void deletefromtail();         void display();         t gethead();         int numofnodes();         ~dlinkedlist(){             if(!isempty())             {                 while(head!=0){             if(head==tail)             {                 delete head;                 head=0;                 tail=0;             }             else{                 node<t>  *ptrtemp=head;                 head=head->ptrnext;                 head->ptrprevious=null;                 delete ptrtemp;              }           }             }         } };  template <class t> bool dlinkedlist<t>::isempty() {     if (head == 0)     {         return true;     }     else     {         return false;     } }  template <class t> void dlinkedlist<t>::addtohead(t e) {     node<t> *ptrnode = new node<t>(e,0,0);     if(isempty())     {         head = ptrnode;         tail = ptrnode;     }     else     {         ptrnode->ptrnext = head;         head->ptrprevious = ptrnode;         ptrnode->ptrprevious = 0;         head = ptrnode;     } }  template <class t> void dlinkedlist<t>::addtotail(t e) {     node<t> *ptrnode = new node<t>(e,0,0);     if(isempty())     {         head = ptrnode;         tail = ptrnode;     }     else     {         tail->ptrnext = ptrnode;         ptrnode->ptrprevious = tail;         ptrnode->ptrnext = 0;         tail = ptrnode;     } }  template <class t> void dlinkedlist<t>::deletefromhead() {     if(!isempty())     {         if(head == tail)         {             delete head;             head = 0;             tail = 0;         }         else         {             node<t> *ptrtemp = head;             head = head->ptrnext;             head->ptrprevious = 0;             delete ptrtemp;         }     } }  template <class t> void dlinkedlist<t>::deletefromtail() {     if(!isempty())     {         if(head == tail)         {             delete tail;             head = 0;             tail = 0;         }         else         {             node<t> *ptrtemp = tail;             tail = tail->ptrprevious;             tail->ptrnext = 0;             delete ptrtemp;         }     } }  template <class t> void dlinkedlist<t>::display() {     if(!isempty())     {         node<t> *ptrtemp = head;         while(ptrtemp->ptrnext != 0)         {             cout<<ptrtemp->info;             ptrtemp = ptrtemp->ptrnext;         }         cout<<ptrtemp->info<<endl;     } }  template <class t> t dlinkedlist<t>::gethead() {      if(isempty())     {         return 0;     }     else     {     return head->info;     } }  template <class t> int dlinkedlist<t>::numofnodes() {     if(isempty())     {         return 0;     }     else     {         int count = 0;         node<t> *ptrtemp = head;         while(ptrtemp->ptrnext != 0)         {             count++;             ptrtemp = ptrtemp->ptrnext;         }         count++;         return count;      } }  template <class t> class stack:public dlinkedlist<t> {     private:         int maxstacksize;     public:         stack()         {             maxstacksize = 10;         }         bool isempty();         bool isfull();         void push(t e);         t pop();         void display();         t topvalue(); };  template <class t> bool stack<t>::isempty() {       bool r= dlinkedlist<t>::isempty();     return r; }  template <class t> bool stack<t>::isfull() {     int totalnodes = dlinkedlist<t>::numofnodes();     if(totalnodes == maxstacksize)     {         return true;     }     else     {         return false;     } }  template <class t> void stack<t>::push(t e) {      if( isfull() )     {     cout<<"stack full "<<endl;     }     else     {         dlinkedlist<t>::addtohead(e);     }  }  template <class t> t stack<t>::pop() {      if(isempty())     {          return 0;     }     else     {         t n = dlinkedlist<t>::gethead();         dlinkedlist<t>::deletefromhead();         return n;     }  }  template <class t> void stack<t>::display() {       if( isempty() )     {         cout<<"stack empty!!"<<endl;     }     else     {         dlinkedlist<t>::display();     }  }  template<class t> t stack<t>::topvalue() {     t temp;     temp=dlinkedlist<t>::gethead();     return temp; }  int main() {     stack<char> obj;     char input[20];     cout<<"enter values \n";     cin>>input;     int size= strlen(input);     for(int i=0; <size ; i++)     {           //======= + or - operators=========         if(input[i]=='+' || input[i]=='-')         {           if(obj.topvalue()=='+' || obj.topvalue()=='-')           {  //======= case-1=======               cout<<obj.pop();               obj.push(input[i]);           }           else if(obj.topvalue()=='*' || obj.topvalue()=='/')           {               //======= case-2=========               cout<<obj.pop();               if(obj.topvalue()=='*' || obj.topvalue()=='/')               {                   cout<<obj.pop();               }               obj.push(input[i]);            }           else if(obj.topvalue()=='(')           {               //======= case-3=========               obj.push(input[i]);           }           else if(obj.isempty())           {               //======= case-4=========               obj.push(input[i]);           }          }         // ++++++++++  * , / operators ++++++++         else if(obj.topvalue()=='*' || obj.topvalue()=='/')         {             if(obj.topvalue()=='+' || obj.topvalue()=='-')             {                  //======= case-1=========                 cout<<obj.pop();                 obj.push(input[i]);             }             else if(obj.isempty())             {                  //======= case-2=========                 obj.push(input[i]);             }             else if(obj.topvalue()=='(')             {                  //======= case-3=========                 obj.push(input[i]);             }             else             {                  //======= case-4=========                 obj.push(input[i]);             }          }         // ++++++++++ opening bracket  ++++++++         else if (obj.topvalue()=='(')         {             obj.push(input[i]);         }         // ++++++++++ closing bracket ++++++++         else if(input[i] != ')')         {             while(obj.topvalue() != '(')             {                 cout<<obj.pop();             }             obj.pop();         }         // ++++++++++ operands ++++++++         else         {             cout<<input[i];         }     }     // ++++++++++ print values stack , empty it++++++++     while(!obj.isempty())     {         cout<<obj.pop();      }        return 0;   }   >  

you have made mistake in following line:

else if (input[i] != ')') 

due program going infinite loop.

it needs be:

else if (input[i] == ')') 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -