rodan0818

(074BCT035)_INFIX_TO_POSTFIX_CONVERSION

May 10th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #define size 100
  4. char infix [size] , postfix [size] , stack [size];
  5. int tops = -1,topp = -1,i=0;
  6. int precedence (char x)
  7. {
  8.     if (x=='$')return 3;
  9.     else if (x=='/'|| x=='*')return 2;
  10.     else if (x=='-'||x=='+')return 1;
  11.     else return 0;
  12. }
  13. int main()
  14. {
  15.     FILE *fp;
  16.     fp = fopen("074BCT035_LAB1.txt","a");
  17.     printf("Enter Infix Expression \n");
  18.     fprintf(fp,"\n Student Roll Number :074BCT035 \n Infix To Postfix Expression Coversion \n Enter Infix Expression \n");
  19.     scanf("%s",infix);
  20.     for (i = 0 ; infix [i] != '\0' ; i++)
  21.     {
  22.         if (i==0)
  23.         {
  24.             printf("\n Infix Expression          Stack         Postfix Expression \n");
  25.             fprintf(fp,"%s \n",infix);
  26.             fprintf(fp,"\n Infix Expression          Stack         Postfix Expression \n");
  27.         }
  28.         if (isalnum(infix [i]))
  29.         {
  30.             topp ++;
  31.             postfix [topp] = infix [i];
  32.         }
  33.         else if ( infix [i] == ')' )
  34.         {
  35.         while (stack [tops] != '('){
  36.             topp ++;
  37.             postfix [topp] = stack [tops];
  38.             stack [tops] = ' ';
  39.             tops --;
  40.         }
  41.         tops --;
  42.         }
  43.         else {
  44.             if(precedence(infix[i])<precedence(stack[tops])){
  45.                 topp ++;
  46.                 postfix [topp] = stack [tops];
  47.                 tops -- ;
  48.             }
  49.             else{
  50.                 tops ++;
  51.                 stack [tops] = infix [i];
  52.             }}
  53.             printf(" %c                         %s                  %s                 \n",infix [i],stack,postfix);
  54.             fprintf(fp," %c                         %s                  %s                 \n",infix [i],stack,postfix);
  55.             }
  56.             if (tops != -1)
  57.             {
  58.                 while(tops!=-1){
  59.                 topp ++;
  60.                 postfix [topp] = stack [tops];
  61.                 tops--;
  62.             }}
  63.             /*for (int d = 0; topp>=d ; d++)
  64.         {
  65.             printf(" %c                         %s             %s                 \n",postfix [d]);
  66.         } */
  67.         printf("                           Empty             %s                 \n",postfix);
  68.         fprintf(fp,"                           Empty             %s                 \n",postfix);
  69.         fprintf(fp,"\n");
  70.         fclose(fp);
  71.         return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment