nyist oj 467 (中缀式变后缀式)

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=467

直接上代码:

  1 #include <cstdio>

  2 #include <cstring>

  3 using namespace std;

  4 

  5 const int N = 1001;

  6 char stack[N];

  7 char op[N];

  8 char s[N];

  9 int top;

 10 int top_op;

 11 

 12 int compare(char a);

 13 void translate();

 14 

 15 int main()

 16 {

 17      int t;

 18      scanf("%d", &t);

 19      while(t--)

 20      {

 21           translate();           

 22     }

 23      return 0;     

 24 }

 25 

 26 int compare(char a)

 27 {

 28      switch(a)

 29     {

 30               case '+':

 31              case '-':

 32                    return 1;

 33              case '*':

 34              case '/':

 35                    return 2;

 36              case '(':

 37                    return 0;

 38              default:

 39                    return -1;          

 40     }     

 41 }

 42 

 43 void translate()

 44 {

 45       scanf("%s", s);

 46       int len = strlen(s) - 1;

 47       top = -1;

 48       top_op = -1;

 49       for(int i = 0; i < len; i++)

 50       {

 51                  if('0' <= s[i] && s[i] <= '9' || s[i] == '.')

 52               {

 53                      top++;

 54                      stack[top] = s[i];          

 55               }           

 56               else if(s[i] == '(')

 57               {

 58                      top_op++;

 59                      op[top_op] = s[i];           

 60               }

 61               else if(s[i] == ')')

 62               {

 63                      while(op[top_op] != '(')

 64                      {

 65                              top++;

 66                              stack[top] = ' ';

 67                              top++;

 68                              stack[top] = op[top_op];

 69                              top_op--;                 

 70                      }   

 71                      top_op--;  

 72               }

 73               else if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')

 74               {

 75                      top++;

 76                      stack[top] = ' ';

 77                      if(top_op != -1)

 78                      {

 79                             while(compare(s[i]) <= compare(op[top_op]))

 80                             {

 81                                    top++;

 82                                    stack[top] = op[top_op];

 83                                    top++;

 84                                    stack[top] = ' ';                            

 85                                    top_op--;                        

 86                            }  

 87                      }

 88                      top_op++;

 89                      op[top_op] = s[i];  

 90               }

 91       }            

 92       while(top_op != -1)

 93       {

 94               top++;

 95               stack[top] = ' ';

 96               top++;

 97               stack[top] = op[top_op];

 98               top_op--;             

 99       }

100       top++;

101       stack[top] = ' ';

102       top++;

103       stack[top] = '=';

104       top++;

105       stack[top] = '\0';

106       printf("%s\n", stack);

107 }

你可能感兴趣的:(OJ)