m2skills

in2pre c

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