波兰式,逆波兰式
某校数据结构课一道OJ题
题目描述
表达式有三种表示方法,分别为:
前缀表示(波兰式):运算符+操作数1+操作数2
中缀表示:操作数1+运算符+操作数2
后缀表示(逆波兰式):操作数1+操作数2+运算符
例如:a +b * (c -d ) - e/f
波兰式:-+a*b-cd/ef (运算符在操作数的前面,用递归计算波兰式)
中缀式:a+b*c-d-e/f
逆波兰式:abcd-*+ef/ (运算符在操作数的后面,用栈计算逆波兰式)
中缀表示就是原表达式去掉扣号。
根据表达式求波兰式、逆波兰式都是教材第三章表达式求值的思想。
求波兰式,需要操作数栈(注意不是计算结果入栈,有计算式入栈),运算符栈。区别在于从后往前扫描表达式,‘(’ 换成’)’,’(‘换成‘)’。栈顶运算符优先级>新读入运算符优先级出栈,表3.1中的相同运算符优先级>(从左往右计算)改为<,例如栈顶为‘+‘,新读入的为‘+’,则栈顶优先级<新读入的优先级。
求逆波兰式,只需要运算符栈。操作数直接输出,操作符按表3.1优先级顺序出栈,输出。
输入表达式,求其波兰式和逆波兰式。
输入
测试次数
每组测试数据一行,一个合法表达式
输出
对每组测试数据,输出两行
第一行,表达式的波兰表示
第二行,表达式的逆波兰表示
不同组测试数据间以空行分隔。
样例输入
1 2 3
| 2 4+2*3-10/5 12+3*5+(2+10)*5
|
样例输出
1 2 3 4 5
| \- + 4 * 2 3 / 10 5 4 2 3 * + 10 5 / -
+ + 12 * 3 5 * + 2 10 5 12 3 5 * + 2 10 + 5 * +
|
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| #include <iostream> #include <string> #include <stack> using namespace std;
#define space ' '
int getPriority(char a) { switch (a) { case '*': case '/': return 2; case '+': case '-': return 1; case '(': case ')': return 0; default: return 0; } }
bool isNumber(char temp) { return (temp != '+' && temp != '-' && temp != '*' && temp != '/' && temp != '(' && temp != ')'); }
void reservePolish(string expression) { stack<char> operatorStack; char ch;
bool theLastCharactorIsNumber = false; bool theFirst = true; for (int i = 0; i < expression.size(); i++) { char ch = expression[i];
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { if (operatorStack.empty() || operatorStack.top() == '(') operatorStack.push(ch); else { while (getPriority(operatorStack.top()) >= getPriority(ch)) { if (!theFirst) cout << space;
cout << operatorStack.top(); operatorStack.pop();
if (operatorStack.empty()) break; } operatorStack.push(ch); } theLastCharactorIsNumber = false; theFirst = false; } else if (ch == '(') { operatorStack.push(ch); theLastCharactorIsNumber = false; } else if (ch == ')') { while (!operatorStack.empty()) { if (operatorStack.top() == '(') { operatorStack.pop(); break; } else { if (!theFirst) cout << space;
cout << operatorStack.top(); operatorStack.pop(); } } theLastCharactorIsNumber = false; theFirst = false; } else { if (theLastCharactorIsNumber || theFirst) cout << ch << flush; else cout << space << ch << flush;
theLastCharactorIsNumber = true; theFirst = false; } } while (!operatorStack.empty()) { if (!theFirst) cout << space;
cout << operatorStack.top(); operatorStack.pop(); } cout << endl; }
void polish(string expression) { stack<char> operatorStack; stack<char> output; char ch;
bool theLastCharactorIsNumber = false;
for (int i = expression.size() - 1; i >= 0; i--) { char ch = expression[i];
if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { if (operatorStack.empty() || operatorStack.top() == ')') operatorStack.push(ch); else { while (getPriority(operatorStack.top()) > getPriority(ch)) { output.push(space); output.push(operatorStack.top()); operatorStack.pop();
if (operatorStack.empty()) break; } operatorStack.push(ch); } } else if (ch == ')') { operatorStack.push(ch); } else if (ch == '(') { while (!operatorStack.empty()) { if (operatorStack.top() == ')') { operatorStack.pop(); break; } else { output.push(space); output.push(operatorStack.top()); operatorStack.pop(); } } } else { if (!theLastCharactorIsNumber) { output.push(space); }
output.push(ch); } theLastCharactorIsNumber = isNumber(ch); } while (!operatorStack.empty()) { output.push(space); output.push(operatorStack.top()); operatorStack.pop(); }
while (!output.empty()) { char temp = output.top();
if (temp == space) { output.pop(); if (output.empty()) { cout << endl; return; } else { cout << space << flush; continue; } } cout << temp << flush; output.pop(); } cout << endl; }
int main() { int tryTimes; cin >> tryTimes;
for (int i = 0; i < tryTimes; i++) { string expression; cin >> expression;
polish(expression); reservePolish(expression);
if (i != tryTimes - 1) { cout << endl; } } return 0; }
|
参考资料:
https://blog.csdn.net/linraise/article/details/20459751
https://zhuanlan.zhihu.com/p/141207867
本文地址: https://www.chimaoshu.top/DS栈—波兰式,逆波兰式/
版权声明:本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议,转载请注明出处。