Converts a general expression into a reverse polish expression.
Brackets are used to reduce the difficulty.
Example
Input:3(a+(b*c))((a+b)*(z+x))((a+t)*((b+(a+c))^(c+d)))Output:abc*+ab+zx+*at+bac++cd+^*
It is easy to understand its features:
1. letters must be output.
2. Run the operation symbol to the stack.
3. When brackets ')' are encountered, an operator is used to exit the stack. Note that not all operators exit the stack.
The difference between the operations without parentheses is whether to determine the priority of the symbol.
#include
#include
#include
using namespace std;int TransformTheExpression(){int T = 0, c = 0, id = 0;scanf("%d\n", &T);char buffer[40200];char res[40200];stack
stk;if ((c = fread(buffer, 1, 40200, stdin)) > 0){for (int i = 0; i < c; i++){if ('(' == buffer[i]) continue;if ( buffer[i] == '+' || buffer[i] == '-' ||buffer[i] == '*' || buffer[i] == '/' || buffer[i] == '^'){stk.push(buffer[i]);}else if (buffer[i] == ')'){res[id++] = stk.top();stk.pop();}elseres[id++] = buffer[i];}}fwrite(res, sizeof(char), id, stdout);return 0;}