The prefix infix suffix expression in computer is an important application of data structure stack, they are the notation of the expression, but the relative position is not the same as the name implies, the prefix expression refers to the symbols are in the operand before the infix expression refers to operators are operands, after the suffix expression is after. Such as:
(a+b) *c-d is an infix expression
-*+ABCD is a prefix expression
ab+c*d-is a suffix expression
Infix expression is commonly used by us is also a common notation, but for the computer processing is very complex, is generally converted to a prefix or suffix expression before the calculation.
First, the use of suffix expression evaluation:
The computer uses the suffix expression evaluation process to analyze the following:
To Infix (3+4) *5-6 as an example, the conversion to the suffix is 34+5*6-
Scan from left to right, when the number is encountered, the number is pushed into the stack, when the operator is encountered, the top two digits in the stack pop up, the corresponding operation, and the results into the stack. Repeat until the right end of the expression.
Scan from left to right. The number 3, 4 into the stack;
Meet operator +, pop 3, 4, calculate 7 into stack
Continue scanning, 5 into the stack
With operator *, the 7, 5 pop-up calculation, get 35 into the stack
Continue scanning, meet 6 into the stack
Encounter operator-, pop-up two 35,6, calculate 29 into stack
To the right end, stop, result 29
The key here is how to convert infix expressions to suffix expressions:
1, initialization of two stacks, operator stack S1 and data stack S2
2, from left to right scan infix expression, meet the number of direct into the stack S2
3, meet operator, compare operator with S1 stack top operator precedence
3-1, if the top of the S1 stack is empty, or is or is the opening parenthesis, the operator will be encountered directly into the stack S1
3-2, if the top of the S1 stack is not empty and is not a left parenthesis, and the operator encountered is higher than the S1 priority, then the operator directly into the stack S1 (note that conversion to the prefix expression is higher or the same priority, and here does not include the same situation)
3-3, if the top of the S1 stack is a closing parenthesis or greater priority. S1 the operator out of the stack into the S2, and then back to 3-1
4, meet the parentheses
4-1, if it is to meet the opening parenthesis, then the left parenthesis operator directly into the stack S1
4-2, if the right bracket is met, then the right parenthesis operator directly into the stack S1
5, repeat 2-4 until to the right of the expression
6. Eject the remaining operators in the S1 to the S2
7, in turn, pop-up elements in the S2 and output, the results of the reverse is the infix expression corresponding to the suffix expression (conversion to the prefix expression without reverse order).
The infix expression exp is converted to a suffix expression, the OP operator stack is used, and the other is stored in an array.
const int N=20; Vector<char> Inexptopost (char a[],int N) {vector<char> res; char st[n]; int top=-1; int i=0; char c=a[i]; whil E (c!= ' ") { switch (c) { case ' (': {top++;st[top]=c;break;} Case ') ': {while (st[top]!= ' (') {res.push_back (st[top]); top--}
top--;
break; } case ' + ': case '-': {while (top>-1& &st[top]!= ' (') {res.push_back (st[top]); top--}
Top++;st[top]=c;
break; } case ' * ': case '/': { &n bsp; while (top>-1&&st[top]!= ' && (st[top]== ' * ' | | st[top]== '/')) {Res.puSh_back (St[top]); top--;}
top++;st[top]=c;
break; } default:{ while (c>= ' 0 ' &&c<= ' 9 ') {Res.push _back (c); i++;c=a[i];}
Load multiple digits of integer i--; res.push_back (' # ')///////End with logo presentation } }
I++;c=a[i]; } while (top>-1) {Res.push
_back (st[top--]);
return res; }
A value stack st is used in the calculation of the suffix expression:
Float Getkey_post (vector<char> arr) {int num_stack[n];int top1=-1; int len=arr.size (); int i=0; char c=arr[i]; int s
= 0; while (I<len) { //if (top1-1==-1) {cout<< ' out of index '; return 0;} C=arr[i]; Switch (c) { case ' + ': {num_stack[top1-1]=num_stack[top1-1]+num_stack[top1];top1--;
Break } case '-': {num_stack[top1-1
]=num_stack[top1-1]-num_stack[top1];top1--;break; } case ' * ': {num_stack[top1-1
]=num_stack[top1-1]*num_stack[top1];top1--;break; } case '/': {if (num_stack[ top1]==0) {cout<< "divisor is 0" <<endl;return 0;}
Num_stack[top1-1]=num_stack[top1-1]/num_stack[top1];top1--;break; } case ' # ': break; default:{ s=0 while (c>= ') 0 ' &&c<= ' 9 ') {s=10*s+c-' 0 '; i++;c=arr[i];} top1++;num_stack[
top1]=s;i--; }
} i++;
} return NUM_STACK[TOP1]; }
Second, the use of the prefix expression evaluation
To Infix (3+4) *5-6 For example, the conversion to prefix is-*+3456
Evaluate by prefix expression: Search from right to left, meet the number into the stack, meet the operator when the top two digital 3+4=7, calculate a good 7 into the stack, meet the operator *, the first two to calculate the stack 7*5=35, then into the stack, meet operators-, out of the stack for Operation 35-6=29, the stack to save the results.
The process of converting an infix expression to a prefix:
1, initialization of two stacks, operator stack S1 and data stack S2
2, from right to left to scan infix expression, meet the number of direct into the stack S2
3, meet operator, compare operator with S1 stack top operator precedence
3-1, if the top of the S1 stack is empty, or is or is the parentheses will meet the operator directly into the stack S1
3-2, if the top of the S1 stack is not empty and is not a left parenthesis, and the operator encountered is higher than the S1 priority or the same, then the operator directly into the stack S1.
3-3, if the top of the S1 stack is a left parenthesis or greater priority. S1 the operator out of the stack into the S2, and then back to 3-1
4, meet the parentheses
4-1, if the right bracket is met, then the left parenthesis operator directly into the stack S1
4-2, if it is to meet the opening parenthesis, then the right parenthesis operator directly into the stack S1
5, repeat 2-4 until the left of the expression
6. Eject the remaining operators in the S1 to the S2
7, in turn, pop-up S2 elements and output, the result is infix expression corresponding to the prefix expression.
vector<char> inexptopre (char a[],int N) {vector<char> res; char num_stack[n];int top1=-1;
Char Op_stack[n];int top2=-1;
int i=n-1;
Char C=a[i];
while (i>=0) {switch (c) {case ') ': {top2++;op_stack[top2]=c;break;}
Case ' (': {while (op_stack[top2]!= ') ') {top1++;num_stack[top1]=op_stack[top2];top2--}
top2--;
Break Case ' + ': Case '-': {while (top2>-1&&op_stack[top2]!= ') ' && (op_stack[top2]== ' * ' | |
op_stack[top2]== '/')) {top1++;num_stack[top1]=op_stack[top2];top2--}
Top2++;op_stack[top2]=c;
Break
Case ' * ': Case '/': {top2++;op_stack[top2]=c;
Break
} default:{while (c>= ' 0 ' &&c<= ' 9 ') {top1++;num_stack[top1]=c;i--;c=a[i];}
top1++;num_stack[top1]= ' # '; i++;
} i--;if (i<0) break;
C=a[i]; while (top2>-1) {top1++;num_stack[top1]=op_stack[top2--];} for (i=top1;i>=0;i--) Res.push_back (num_stack[i))
;
return res; }
Finally, the final result is derived from the prefix sequence:
int GetValue (char str[]) {int s=0; int i,j; int len=0; for (i=0;str[i]!= '; i++) len++; for (i=0,j=len-
1;i<j;i++,j--) {char t=str[i];str[i]=str[j];str[j]=t;} i=0;
while (str[i]!= ') {s=s*10+str[i]-' 0 ';
i++;
return s; Float Getkey_pre (vector<char>arr) {int st[n],top=-1; int len=arr.size (); int i=len-1,j; char c=arr[i]; int s; char
Tmp[n];
while (i>=0) {c=arr[i];
Switch (c) {case ' + ': {st[top-1]=st[top]+st[top-1];top--;break;}
Case '-': {st[top-1]=st[top]-st[top-1];top--;break;}
Case ' * ': {st[top-1]=st[top]*st[top-1];top--;break;}
Case '/': {if (st[top-1]==0) {cout<< "divisor is 0"; return 0;}
St[top-1]=st[top]/st[top-1];top--;break;
Case ' # ': break;
default:{s=0;
j=0;
while (c>= ' 0 ' &&c<= ' 9 ') {tmp[j++]=c;i--;c=arr[i];}
tmp[j]= ' n ' s=getvalue (TMP);
top++;st[top]=s;i++;
}} i--;
return st[top]; }