Transform the algebraic expression with brackets into RPN form (Reverse Polish Notation). Two-argument operators: +, -, *, /, ^ (priority from the lowest to the highest), brackets ( ). Operands: only letters: a,b,...,z. Assume that there is only one RPN form (no expressions like a*b*c).
INPUT
t [the number of expressions <= 100] expression [length <= 400] [other expressions]
Text grouped in [ ] does not appear in the input file.
OUTPUT
The expressions in RPN form, one per line.
Example
Input:
3 (a+(b*c)) ((a+b)*(z+x)) ((a+t)*((b+(a+c))^(c+d)))
Output:
abc*+ ab+zx+* at+bac++cd+^*
扯淡
思路:
总的想法是通过运算符栈和字符栈来解决问题。
入栈操作:将运算符和字符分别压入运算符栈和字符栈
出栈操作:运算符正常pop出栈,字符栈pop弹出两个字符,并且与新弹出的运算符合并成新的字符压入字符栈
遍历字符串,分别判断三种情况
-
当是字符时,字符进行入栈操作,这里有个特殊情况,当这个字符是最后一个字符时,进行完入栈操作后接着进行出栈操作
-
当字符为’)’时,进行出栈操作
-
当字符为运算符时,运算符执行入栈操作
遍历而结束后运算符栈为空,而字符栈内剩一个,就是转换完后的字符串