Infix expression to postfix expression (MS) 10000 (KB) 1754/4417 infix expression is a general arithmetic or logical formula representation method, operator is in infix form in the middle of the operand (example: 3 + 4), infix expression is commonly used in arithmetic representation. The suffix expression does not contain parentheses, the operator is placed after two operands, and all calculations are made strictly from left to right in the order in which the operators appear (no longer consider precedence rules for operators, such as: (2 + 1) * 3, which is 2 1 + 3 *. Using the stack structure, the infix expression is converted to a suffix expression. (Test data element is a single character)
input
Infix expression
Output
Suffix expression
Sample Input
A + (B-C/D) *e
Sample Output
abcd/-e*+
#include <iostream> #include <stdio.h> using namespace std;
int main () {int top=-1;
Char a[50];//array simulation stack char ch;
while ((Ch=getchar ()) = ' \ n ')//one character reads {if (' a ' <=ch&&ch<= ' z ') cout<<ch;//if the character is directly output.
else if (top==-1) a[++top]=ch;//if the stack is empty, enter the operator directly into the else if (ch== ' (') a[++top]=ch;
else if (ch== ') ') //Consider parentheses, the parentheses change the precedence of the operator {while (a[top]!= ' (') {cout<<a[top--];
} top--; } else if (ch== ' * ' | | ch== '/')//multiplication The priority of the method is greater than the addition and subtraction {if (a[top]== ' + ' | | a[top]== '-' | |
a[top]== ' (')//if the stack top is added or subtracted or parentheses into the stack.
A[++top]=ch; else if (a[top]== ' * ' | |
a[top]== '/')//if the stack top is the multiplication method, the output stack top, and then into the stack.
{cout<<a[top--];a[++top]=ch; }} else if (ch== ' + ' | |
ch== '-')//plus subtraction priority is small {if (a[top]== ')//Consider the stack top is not the parentheses a[++top]=ch; else if (a[top]== ' + ' | |
a[top]== '-')//stack top is add and subtract, on the output, and then into the stack {cout<<a[top--];
A[++top]=ch; } else if (a[top]== ' * ' | | a[top]== '/')//stack top is multiplication, consider whether the previous bit of the top of the stack is empty {cout<<a[top--];//first output stack top if (top!=0) cout<<a[top--];//continue output stack top a[++top]=ch;//stack}}} while (top>=0) if it is not empty cout<<a[top--];
return 0; }
stack: same idea
#include <iostream> #include <cstdio> #include <stdlib.h> using namespace std;
typedef struct {char data[105];
int top;
}sqlist; void push (char e,sqlist *&l) {l->data[++l->top]=e;} void Pop (SqList *&l) {if (l->data[l->top]== ' ('
) l->top--;
else cout<<l->data[l->top--];
} int main () {char ch;
SqList *l;
l->top=-1;
while ((Ch=getchar ()) = ' \ n ') {if (' A ' <=ch&&ch<= ' z ') cout<<ch;
else if (l->top==-1) push (ch,l);
else if (ch== ' (') push (ch,l);
else if (ch== ') ') {while (l->data[l->top]!= ' (') {pop (L);
} pop (L); } else if (ch== ' * ' | | ch== '/') {if (l->data[l->top]== ' + ' | | l->data[l->top]== '-' | |
l->data[l->top]== ' (') push (ch,l); else if (l->data[l->top]== ' * ' | |
l->data[l->top]== '/') {pop (L);
Push (CH,L); }} else if (ch== ' + ' | | |
ch== '-') {if (l->data[l->top]== ' (') push (ch,l);
else {pop (L);
Push (CH,L);
} }} while (L->top!=-1) pop (l);
return 0; }