nyoj 467 infix variable suffix (stack)

infix to suffix

Time Limit: 1000 ms | Memory Limit: 65535 KB
Difficulty: 3
 
describe
People's daily habit is to write arithmetic expressions in infix form, but for machines, they are more "accustomed to" postfix type. The discussion on infix and postfix expressions of arithmetic expressions is generally available in data structure books. For reference, I won't repeat them here, and now your task is to change the infix to the postfix.
 
enter
The first line inputs an integer n, and there are n groups of test data (n<10).
Each set of test data has only one line, which is a character string with a length of no more than 1000, which represents the infix expression of this expression, and each expression ends with "=". This expression contains only the symbols +-*/ and parentheses. The parentheses can be nested. The data guarantees that no negative numbers will appear in the input operands.
The data guarantees that the divisor will not be 0
output
Each group outputs the corresponding postfix expression of the infix expression of the group, and requires adjacent operand operators to be separated by spaces.
sample input
2
1.000+2/4=
((1+2)*5+1)/4=
Sample output
1.000 2 4 / + =
1 2 + 5 * 1 + 4 / =
1  /* *
 2      Analysis:
 3          Ⅰ, build stack (put operator), queue (put operation result)
 4          Ⅱ, isdigit (s [i]) || s [i] == '.' directly queue.push ( s[i])
 5          Ⅲ, s[i] == '(' Push
 6          Ⅳ, s[i] == ')' All operators above '(' are popped (queued), and finally ' ('
 Pop 7          Ⅴ, encounter the operator to judge its relationship with the top element of the stack
 8              Ⅴ(①), if priority (stack.top()) >= priority (s [i]), pop the stack (enter the queue)
 9          Ⅵ. Pop all operators in the stack except '#' (into the queue)
 10  * */

Core code:

 1 /**
 2     for (int i = 0; i < len; ++ i) {
 3         if (isdigit (s [i]) || s [i] == '.')
 4             que.push (s [i]);
 5         else if (s [i] == '(')
 6             sta.push (s [i]);
 7         else if (s [i] == ')') {
 8             char c = sta.top ();
 9             while (c != '(') {
10                 que.push (c);
11                 que.push (' ');
12                 sta.pop ();
13                 c = sta.top ();
14             }
15             sta.pop ();
16         } else {
17             char c = sta.top ();
18             while (priority (c) >= priority (s [i])) {
19                 que.push (c);
20                 que.push (' ');
21                 sta.pop ();
22                 c = sta.top ();
23             }
24         }
25         
26         if (isdigit (s [i]) && (s [i + 1] == '/' || s [i + 1] == '+' ||
27              s [i + 1] == '-' || s [i + 1] == '*' || s [i + 1] == '=' || s [i + 1] == ')')) {
28                 que.push (' ');
29         }
30     } 
31 **/

C/C++ code implementation (AC):

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 int priority (char c) {
 6     if (c == '/' || c == '*') return 3;
 7     if (c == '+' || c == '-') return 2;
 8     if (c == '=') return 1;
 9     return 0;
10 }
11 
12 int main () {
13     int T;
14     scanf ("%d", &T);
15     while (T --) {
16         char s [1004];
17         int len;
18         queue <char> que;
19         stack <char> sta;
20         sta.push ('#');
21         
22         getchar ();
23         scanf ("%s", &s[0]);
24         len = strlen (s);
25         
26         for (int i = 0; i < len; ++ i) {
27             if (isdigit (s [i]) || s [i] == '.') {
28                 que.push (s [i]);
29             } else if (s [i] == '(') {
30                 sta.push (s [i]);
31             } else  if (s [i] == ' ) ' ) {
 32                  char c = sta.top ();
 33                  while (c != ' ( ' ) {
 34                      que.push (c);
 35                      que.push ( '  ' ); // space between operators 
36                      sta.pop ();
 37                      c = sta.top ();
 38                  }
 39                  sta.pop ();
 40              } else {
 41                  charc = sta.top ();
 42                  while (!sta.empty() && priority (c) >= priority (s[i])) {
 43                      que.push (c);
 44                      que.push ( '  ' ); // space between operators 
45                      sta.pop ();
 46                      c = sta.top ();
 47                  }
 48                  sta.push (s [i]);
 49              }
 50              
51              if (isdigit (s [i]) && ( s[i + 1 ] == ' / ' || s[i + 1] == ' + ' ||
 52               s [i + 1 ] == ' - ' || s [i + 1 ] == ' * ' || s [i + 1 ] == ' = ' || s [ i + 1 ] == ' ) ' )) {
 53                   // Space between numbers and operators 
54                  que.push ( '  ' );
 55              }
 56          }
 57          while (!sta.empty () && sta.top () ! = ' #') {
58             que.push (sta.top ());
59             sta.pop ();
60         }
61         while (!que.empty ()) {
62             printf ("%c", que.front ());
63             que.pop ();
64         }
65         printf ("\n");
66     }
67     return 0;
68 } 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209119&siteId=291194637