C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 340,161 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 4,030 people online right now. Registration is fast and FREE... Join Now!




Infix to postfix

the following propgram can convert an infix expression to a postfix expression using stacks

Submitted By: frog
Actions:
Rating:
Views: 32,033

Language: C++

Last Modified: July 30, 2006
Instructions: the program assumes that the infix expression contains only single lettered variables

Snippet


  1. // the program is used to convert a infix expression to a postfix expression
  2.  
  3. #include<iostream.h>
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<string.h>
  7. #include<stdlib.h>
  8.  
  9.  
  10.  
  11. const int size =50;
  12. char infix[size],postfix[size],stack[size];
  13. int top=-1;
  14.  
  15. int precedence(char ch);   // function to get the precedence of the operator
  16. char pop()//function to pop an element from the stack
  17. char topelement()// returns the top element of the stack
  18. void push(char ch)// pushes an element into the stack
  19.  
  20.  
  21.  
  22. int main()
  23. {
  24.      char ele,elem,st[2];
  25.      int prep,pre,popped,j=0,chk=0;
  26.      strcpy(postfix," ");
  27.          
  28.      gets(infix);
  29.      
  30.      for(int i=0;infix[i]!=0;i++)
  31.           {
  32.                   if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')     
  33.                        postfix[j++]=infix[i];
  34.                   else if(infix[i]=='(')
  35.                       {
  36.                          elem=infix[i];
  37.                          push(elem);
  38.                       }
  39.                   else if(infix[i]==')')
  40.                       {
  41.                          while(popped=pop() != '(')
  42.                              postfix[j++]=popped;
  43.                       }
  44.                   else
  45.                       {
  46.                          elem=infix[i];
  47.                          pre=precedence(elem);//stores the precedence of operator coming frm infix
  48.                          ele=topelement();
  49.                          prep=precedence(ele);//stores the precedence of operator at the top of the stack
  50.                        
  51.                          if(pre > prep)
  52.                            push(elem);                                         
  53.                            
  54.                          else
  55.                            {
  56.                                 while(prep >= pre)
  57.                                   {
  58.                                      if(ele=='#')
  59.                                        break;
  60.                                      popped=pop();
  61.                                      ele=topelement();
  62.                                      postfix[j++]=popped;
  63.                                      prep=precedence(ele);
  64.                                    }
  65.                                    push(elem);
  66.                             }
  67.                          }
  68.              }
  69.              
  70.           while((popped=pop())!='#')
  71.               postfix[j++]=popped;
  72.           postfix[j]='\0';
  73.          
  74.           cout<<"\n post fix :"<<postfix<<endl;
  75.            
  76.            system("pause");
  77.            return 0;
  78. }
  79.  
  80. int precedence(char ch)
  81. {
  82.        switch(ch)
  83.           {
  84.                case '^' : return 5;
  85.                case '/' : return 4;
  86.                case '*' : return 4;                                           
  87.                case '+' : return 3;
  88.                case '-' : return 3;
  89.                default  : return 0;
  90.           }
  91. }
  92.  
  93. char pop()                  //function to pop the element from the stack
  94. {
  95.      char ret;
  96.      if(top!=-1)
  97.        {  ret =stack[top];
  98.           top--;
  99.           return ret;
  100.        }
  101.      else
  102.         return '#';
  103. }
  104.                          
  105. char topelement()          // function to return top element from the stack without popping
  106. {     
  107.       char ch;
  108.       if(top!=-1)
  109.         ch=stack[top];
  110.       else
  111.          ch='#';
  112.        return ch;
  113. }
  114.  
  115. void push(char ch)          // function to push an element in the stack
  116. {
  117.      if(top!=size-1)
  118.          {
  119.             top++;
  120.             stack[top]= ch;
  121.          }
  122. }         
  123.                                
  124.  

Copy & Paste


Comments

fairy 2008-03-16 06:07:30

this is a horrible code it will drive the reader mad can't you just reduce the code?????

javadmotahari 2008-08-08 08:49:56

omniaelsafty 2009-01-17 09:50:58

THANKS ALOOOT

sam442000 2009-04-08 00:03:27

brilliant

pavankvvd 2009-08-08 10:27:39

nfix.c:6: error: variably modified ‘infix’ at file scope infix.c:6: error: variably modified ‘postfix’ at file scope infix.c:6: error: variably modified ‘stack’ at file scope infix.c: In function ‘main’: infix.c:18: error: ‘for’ loop initial declaration used outside C99 mode infix.c:100: error: expected declaration or statement at end of input CAN ANYONE SOLVE THIS ERROR FOR THE ABOVE CODE

jl880703 2009-11-17 19:45:21

i made some modification over this code where can i post it


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month