Advertisement
nontawat1996

Untitled

Sep 25th, 2011
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. char ope[5]= {'+','-','/','^'};
  5. char f(char a)
  6. {
  7.     if(a==ope[0] || a==ope[1]) return 1;
  8.     if(a==ope[2] || a==ope[3]) return 2;
  9.     else return 3;
  10. }
  11. int main()
  12. {
  13.     char input[500]= {'\0'},output[500]= {'\0'},stack[500]= {'\0'};
  14.     int i,n=0,s1,s2,top1,top2; // s คือ คะแนน ope top1 ใช้กับ stack
  15.     //printf("%s",ope);
  16.     scanf("%s",input);
  17.     for(i=0; i<strlen(input); i++)
  18.     {
  19.         top1=strlen(stack);
  20.         top2=strlen(output);
  21.         if(input[i]>='A' && input[i]<='Z')
  22.         {
  23.             output[n]=input[i];
  24.             n++;
  25.         }
  26.         else if(input[i]!='(' && input[i]!=')')
  27.         {
  28.             if(top1==0) stack[0]=input[i];
  29.             else
  30.             {
  31.                 while(1)
  32.                 {
  33.                     s1=f(input[i]);
  34.                     s2=f(stack[top1-1]);
  35.                     if(s1>s2)
  36.                     {
  37.                         stack[top1]=input[i];
  38.                         break;
  39.                     }
  40.                     else
  41.                     {
  42.                         output[n]=stack[top1-1];
  43.                         n++;
  44.                         stack[top1-1]='\0';
  45.                     }
  46.                 }
  47.             }
  48.         }
  49.         else if(input[i]=='(')
  50.         {
  51.             stack[top1]=input[i];
  52.         }
  53.         else if(input[i]==')')
  54.         {
  55.             input[i]=='\0';
  56.             s1=i-1;
  57.             while(1)
  58.             {
  59.                 if(input[s1]=='(')
  60.                    {
  61.                        input[s1]=='\0';
  62.                        break;
  63.                    }
  64.                    output[n]=input[s1];
  65.                 s1--;
  66.             }
  67.         }
  68.     }
  69.  
  70.     if(strlen(stack)!=0)
  71.     {
  72.         strrev(stack);
  73.     }
  74.     printf("\n\nOutput : %s",output);
  75.     for(i=0;i<strlen(stack);i++)
  76.     {
  77.         if(stack[i]!='(' && stack[i]!=')') printf("%c",stack[i]);
  78.     }
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement