m2skills

in2post c

Mar 29th, 2017
3,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.48 KB | None | 0 0
  1. /* Program to perform all types of stack conversions */
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<math.h>
  5. #include<string.h>
  6. #include <stdlib.h>
  7.  
  8. #define MAX 20
  9.  
  10. void push( int ,int );
  11. char pop();
  12. void in2postfix();
  13. int priority ( char );
  14.  
  15. char stk[20],infix[20],postfix[20];
  16. int top=-1;
  17.  
  18.  
  19. int main()
  20. {
  21.     int cont;
  22.     printf("\n Enter the Infix Expression : ");
  23.     scanf("%s",infix);
  24.     in2postfix();
  25.     return 0;
  26. }
  27.  
  28. // method that pushes the elements onto the character stack
  29. void push(int pos)
  30. {
  31.  
  32.     if(top==MAX-1)
  33.     {
  34.         printf("stackfull!!!!");
  35.     }
  36.  
  37.     else {
  38.         top++;
  39.         stk[top] = infix[pos];
  40.     }
  41. }
  42.  
  43. // method that removes character from stack and returns them
  44. char pop()
  45. {
  46.     char ch;
  47.  
  48.     if(top < 0)
  49.     {
  50.         printf("stackempty!!!!");
  51.         exit(0);
  52.     }
  53.     else
  54.     {
  55.         ch=stk[top];
  56.         stk[top]='\0';
  57.         top--;
  58.         return(ch);
  59.     }
  60.     return 0;
  61. }
  62.  
  63. // method that converts String from infix to postfix
  64. // all the strings are assumed to be valid expressions
  65. void in2postfix()
  66. {
  67.     int i=0,j=0;
  68.  
  69.     // iterating while end of string is not found
  70.     while(infix[i]!='\0')
  71.     {
  72.        
  73.         // if an alphabet is found then copy it to the output string
  74.         if(infix[i]>='a' && infix[i]<='z')
  75.         {
  76.             postfix[j]=infix[i];
  77.             j++;
  78.             i++;
  79.         }
  80.        
  81.         // if an opening bracket is found then put it in stack
  82.         else if(infix[i]=='(' || infix[i]=='{'  || infix[i]=='[')      
  83.         {
  84.             push(i);
  85.             i++;
  86.         }
  87.        
  88.         // if an closing bracket is found then
  89.         // keep removing the operators from the stack and add them to postfix string until you find the corresponding opening bracket
  90.         else if(infix[i]==')' || infix[i]=='}'  || infix[i]==']')      
  91.         {
  92.             if(infix[i]==')')
  93.             {
  94.                 while(stk[top]!='(')          /*pop till corresponding opening bracket is found*/
  95.                 {
  96.                     postfix[j]=pop();
  97.                     j++;
  98.                 }
  99.                 pop();
  100.                 i++;
  101.             }
  102.  
  103.             else if(infix[i]==']')
  104.             {
  105.                 while(stk[top]!='[')      /*pop till corresponding opening bracket is found*/
  106.                 {
  107.                     postfix[j]=pop();
  108.                     j++;
  109.                 }
  110.                 pop();
  111.                 i++;
  112.             }
  113.  
  114.             else if(infix[i]=='}')
  115.             {
  116.                 while(stk[top]!='{')      /*pop till corresponding opening bracket is found*/
  117.                 {
  118.                     postfix[j]=pop();
  119.                     j++;
  120.                 }
  121.                 pop();
  122.                 i++;
  123.             }
  124.         }
  125.         // if none of the above cases are satisfied then we surely have an operator
  126.         else            
  127.         {
  128.             // if the stack if empty then we simply put the operator in stack
  129.             if(top==-1)
  130.             {
  131.                 push(i);
  132.                 i++;
  133.             }
  134.  
  135.             // if the priority of current operator is less than or equal to the stack top then
  136.             // pop the stack top and add it to the postfix string
  137.             else if( priority(infix[i]) <= priority(stk[top]))
  138.             {
  139.                 postfix[j]=pop();
  140.                 j++;
  141.                
  142.                 // now if you have an operator that has equal priority as of current operator then pop
  143.                 while(priority(stk[top]) == priority(infix[i])){
  144.                     postfix[j] = pop();
  145.                     j++;
  146.                     if(top < 0) {
  147.                         break;
  148.                     }
  149.                 }
  150.                 push(i);
  151.                 i++;
  152.             }
  153.            
  154.             // if the priority of current operator if high then push it onto the stack
  155.             else if(priority(infix[i]) > priority(stk[top]))
  156.             {
  157.                 push(i);
  158.                 i++;
  159.             }
  160.         }
  161.     }
  162.     // at the end remove all the operators from the stack
  163.     while(top!=-1)
  164.     {
  165.         postfix[j]=pop();
  166.         j++;
  167.     }
  168.     postfix[j]='\0';
  169.     printf("The converted postfix string is : %s ",postfix);
  170. }
  171.  
  172. // method that returns priority for operators according to their precedence
  173. int priority ( char alpha )
  174. {
  175.     if(alpha == '+' || alpha =='-')
  176.     {
  177.         return(1);
  178.     }
  179.  
  180.     if(alpha == '*' || alpha =='/')
  181.     {
  182.         return(2);
  183.     }
  184.  
  185.     if(alpha == '$')
  186.     {
  187.         return(3);
  188.     }
  189.     return 0;
  190. }
Add Comment
Please, Sign In to add comment