nyoj 467 中缀式变后缀式 (栈)

中缀式变后缀式

时间限制: 1000 ms  |  内存限制:65535 KB
难度: 3
 
描述
人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更“习惯于”后缀式,关于算术表达式的中缀式和后缀式的论述一般的数据结构书都有相关内容可供参看,这里不再赘述,现在你的任务是将中缀式变为后缀式。
 
输入
第一行输入一个整数n,共有n组测试数据(n<10)。
每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式的中缀式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。
数据保证除数不会为0
输出
每组都输出该组中缀式相应的后缀式,要求相邻的操作数操作符用空格隔开。
样例输入
2
1.000+2/4=
((1+2)*5+1)/4=
样例输出
1.000 2 4 / + =
1 2 + 5 * 1 + 4 / =
 1 /**
 2     分析:
 3         Ⅰ、建立stack (放操作符)、queue (放操作结果)
 4         Ⅱ、isdigit (s [i]) || s [i] == '.' 直接 queue.push (s [i])
 5         Ⅲ、s [i] == '(' 入栈
 6         Ⅳ、s [i] == ')' 将 '('  以上的所有运算符出栈 (入队列),最后将 '(' 出栈
 7         Ⅴ、遇到操作符判断其和栈顶元素的关系
 8             Ⅴ(①)、如果 priority (stack.top()) >= priority (s [i]), 出栈 (入队列)
 9         Ⅵ、将stack中除 '#' 以外所有的运算符出栈(入队列)
10 **/

核心代码:

 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++代码实现(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 (' '); // 运算符间空格 
36                     sta.pop ();
37                     c = sta.top ();
38                 }
39                 sta.pop ();
40             } else {
41                 char c = sta.top ();
42                 while (!sta.empty() && priority (c) >= priority (s [i])) {
43                     que.push (c);
44                     que.push (' '); // 运算符间空格 
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                  // 数字与运算符间空格 
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 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值