习题3.11 表达式转换 (25point(s))
算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。
Example:
#include <iostream>
#include <cstring>
using namespace std;
#define MaxSize 32
void print(char *b, char *e)
{
static int space = 0;
if(b == NULL) return ;
if(space) cout << ' ';
else space = 1;
while(b < e) cout << *(b++);
}
int main()
{
char str[MaxSize];
cin >> str;
char stack[MaxSize];
int top = 0;
char *p = str;
char *operand = NULL;
while(1) {
char ch = *p;
if(ch == '\0') {
print(operand, p);
while(top) cout << ' ' << stack[--top];
break;
} else {
if(strchr("+-*/()", ch)) {
if(ch == '(') {
stack[top++] = ch;
} else if(ch == ')') {
print(operand, p);
operand = NULL;
while(top && stack[top-1] != '(')
cout << ' ' << stack[--top];
if(top) --top;
} else if(ch == '*' || ch == '/') {
print(operand, p);
operand = NULL;
while(top && (stack[top-1] == '*' || stack[top-1] == '/'))
cout << ' ' << stack[--top];
stack[top++] = ch;
} else if(ch == '+' || ch == '-') {
if(p == str || ( strchr("+-*/(", p[-1]) )) {
if(operand == NULL) operand = p;
} else {
print(operand, p);
operand = NULL;
while(top && (stack[top-1] != '('))
cout << ' ' << stack[--top];
stack[top++] = ch;
}
}
} else if(operand == NULL) operand = p;
}
p++;
}
return 0;
}
思路:
Partially Accepted。
运算数前有正负号?明明处理了,这表达有点违背直觉,是+123,-456这样子吗?