Pages

Related Posts with Thumbnails
Infix to Postfix – C Programming And Algorithom >> Codes-N-Tricks
Content feed Comments Feed

Infix to Postfix – C Programming And Algorithom

Posted by Soham Saturday, September 11, 2010

This Proram makes an Infix expression to its Postfix form.

Infix to postfix
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. #define size 10
  5.  
  6. struct stack
  7. {
  8.   char stkel[size];
  9.   int tos;
  10. };
  11.  
  12. struct stack stk;
  13. int ele;
  14. char infix[50],output[50];
  15.  
  16. void push(struct stack *ps,char ele);
  17. char pop(struct stack *ps);
  18. void show_stack(struct stack *ps);
  19. void show_output(char output[]);
  20. int prec(char);
  21.  
  22. int main()
  23. {
  24.     int i=0,j=0,k=0,length;
  25.     char temp;
  26.     struct stack *ps;
  27.     FILE *fp;
  28.  
  29.     fp=fopen("data1.txt","r");
  30.     if(fp==NULL)
  31.     {
  32.         printf("\nError Openning File\n");
  33.         exit(1);
  34.     }
  35.     else
  36.         printf("Data from the file data1.txt has been taken.\n");
  37.  
  38.     ps=&stk;
  39.     ps->tos=-1;
  40.  
  41.     while(!feof(fp))
  42.     {
  43.         ps->tos=-1;j=0;
  44.         fscanf(fp,"%s\n",infix);
  45.         printf("\nThe infix expresson is %s\n",infix);
  46.         length=strlen(infix);
  47.     
  48.         printf("\nInp\t\tPostfx\t\t\tStk\n");
  49.         
  50.         for(i=0;i<length;i++)
  51.         {
  52.                 printf("%c\t\t",infix[i]);//Input string Printed
  53.                                 //Numbers are added to the out put string
  54.                 if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!='$' && infix[i]!=')' && infix[i]!='(' )
  55.                 {
  56.                     output[j]=infix[i];
  57.                     j++;
  58.                 }
  59.                                 //If an operator or a bracket is encountered
  60.                 else
  61.                 {
  62.                     if(ps->tos==-1) //If there are no elements in the stack,the operator added to stack
  63.                     {
  64.                     push(ps,infix[i]);
  65.                     }
  66.                     else
  67.                     {       //Operators or pushed or poped based on the precedence
  68.                         if(infix[i]!=')' && infix[i]!='(')
  69.                         {
  70.                             if(prec(infix[i]) <= prec(ps->stkel[(ps->tos)]))
  71.                             {
  72.                             temp=pop(ps);
  73.                             output[j]=temp;
  74.                             j++;
  75.                             push(ps,infix[i]);
  76.                             }
  77.                             else
  78.                             {
  79.                               push(ps,infix[i]);
  80.                             }
  81.                         }
  82.                         else    //if bracket occurs
  83.                         {
  84.                             if(infix[i]=='(')
  85.                             {
  86.                               push(ps,infix[i]);
  87.                             }
  88.                             if(infix[i]==')')
  89.                             {
  90.                               temp=pop(ps);
  91.                               while(temp!='(')
  92.                               {
  93.                                 output[j]=temp;
  94.                                 j++;
  95.                                 temp=pop(ps);
  96.                               }
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.                 output[j]='\0';
  102.                 show_output(output);
  103.                 printf("\t\t\t");
  104.                 show_stack(ps);
  105.    
  106.  
  107.         }
  108.  
  109.         //rest of the elemets of stack added to postfix string
  110.         while(ps->tos!=-1)
  111.         {
  112.             output[j++]=pop(ps);
  113.             output[j]='\0';
  114.             printf("\t\t");
  115.             show_output(output);
  116.             printf("\t\t\t");
  117.             show_stack(ps);
  118.         }
  119.  
  120.         printf("\nThe postfix expression is\t");
  121.          for(i=0;i<length;i++)
  122.             printf("%c",output[i]);
  123.         printf("\n");
  124.     }
  125.     return 0;
  126.  
  127. }
  128. //Functions for operations on stack
  129.  
  130. void push(struct stack *ps,char ele)
  131. {
  132.     ps->tos++;
  133.     ps->stkel[ps->tos]=ele;
  134. }
  135.  
  136. char pop(struct stack *ps)
  137. {
  138.     char var;
  139.     var=ps->stkel[ps->tos];
  140.     ps->tos--;
  141.     return var;
  142. }
  143.  
  144. //showing postfix string
  145. void show_output(char output[])
  146. {
  147.     int i;
  148.     for(i=0;output[i]!='\0';i++)
  149.         printf("%c",output[i]);
  150. }
  151.  
  152. //showing the elements of stack
  153. void show_stack(struct stack *ps)
  154. {
  155.     int x=0;   
  156.     if(ps->tos>-1)
  157.         while(x<=ps->tos)
  158.         {
  159.             printf("%c",ps->stkel[x]);
  160.             x++;
  161.         }
  162.     printf("\n");
  163. }
  164.  
  165. //Function to get the precedence of an operator
  166.  
  167. int prec(char symbol)
  168. {
  169.  
  170. if(symbol== '(')
  171. return 0;
  172. if(symbol== ')')
  173. return 0;
  174. if(symbol=='+' || symbol=='-')
  175. return 1;
  176. if(symbol=='*' || symbol=='/')
  177. return 2;
  178. if(symbol=='^')
  179. return 3;
  180. //return 0;
  181. }
Further discussion will be carried out later.  Any Suggestion is solicited.

0 Responses to Infix to Postfix – C Programming And Algorithom

Post a Comment

SyntaxHighlighter

E-Mail Subscription

Enter your email address:

Subscribe & get Regular E-Mail Updates From Codes-N-Tricks.
Delivered by FeedBurner

Share with Orkut

Loading

Recent Posts

About Us