Infix expression: An expression of the operand + operator + operand form that we have touched before
Postfix expression: Does not contain parentheses, the operator is placed after two operands, all calculations in the order in which operators appear, strictly from left to right (no longer consider the precedence rules of operators, such as: (2 + 1) * 3, that is 2 1 + 3 *
The infix expression is converted to the code of the suffix expression:
void Postfix (char e[],char f[])//e is an infix expression f is a suffix expression.
{
int i = 0,top = 0,j = 0;
Char opst[100];
top = 0;
Opst[top] = ' # ';
top++;
while (e[i]! = ' # ')
{
if ((e[i] >= ' 0 ' && e[i] <= ' 9 ') | | e[i] = '. ')
F[j++] = e[i++];
else if (e[i] = = ' 0 ')
i++;
else if (e[i] = = ' (')
{
opst[top++] = E[i];
}
else if (e[i] = = ') ')
{
t = top-1;
while (opst[t]! = ' (')
{
F[j++] = Opst[--top];
t = top-1;//if it appears (then () should be played, of course, before the priority of the () E[i] has gone to the first part, and now just
Take some more priority than the lower ones.
The effect is essentially the same as the last while loop, where the final processing is done internally, while () where the entire expression is
The final clearing of the floor.
}
}
else if (Is_operation[i])
{
F[j++] = ';//separate 2 operands with a space, as 2*3 will read an operand after it has been read, and if it encounters an operator, add a space.
The next operand and the previous operand are separated by a space.
while (opst[top-1) >= Priority[e[i])
F[j++] = opst[--top];//The operator in buyers is assigned to the suffix expression when the operator that has just read is less than or equal to buyers.
opst[top++] = e[i++];//The read operator into the buyers when the operator reading is greater than buyers.
Because Opst[0] is the lowest priority ' # ' It is bound to execute into buyers operations.
}
}
while (top)//infix expression after all read, operator buyers there are operating costs.
F[j++] = Opst[--top];
}
/* Idea: If 3+5*2 read 2 numbers first, + press-in buyers cannot be placed in the suffix expression at the beginning, because it is not known whether it is to be calculated first. Then when the 3 5 operator is not sure if you can first
Operation, read the next number 2, and then read the 5 and 2 operators * greater than the previous buyers top + then into the buyers. Since the last reading of the entire infix expression, the operator needs to buyers out buyers, so
Here you can see that the operator buyers priority is from small to large arrangement, according to the law of buyers, priority first-out. The idea of satisfying the suffix expression.
Deeper understanding: According to the algorithm of the suffix expression, 2 numbers plus a symbol can be calculated. And infix is the operand operator operand to calculate, so they
The conversion can look like this: first read 2 operands in the prefix, because this is the basic operation must, in the middle of the operator of the 2 operand into a buyers, and then read the 3rd operand,
At this point, the third operand and the second operand into the operational state, between which there is an operator, whether the operator into the buyers to see its priority, if lower than equal to the previous
Buyers the top operator, put the operator in the buyers to the suffix expression. If the operator you just read takes precedence over the top of the stack, you can put the symbol you just read into buyers. (Because in 3
In the decency of the operands and the 2 operators, regardless of their form, the priority of the 2 operators can be judged first, and when the Operation Mr. Foo appears and its precedence is higher, obviously we
You can get him to write the suffix expression, because he can do it first, because he's definitely going to do the Mr. Foo operation, which is lower than his priority and appears behind him. But if the higher-priority
The operator in the back, you can not first operation, because it is possible to have a higher priority outside this form, so the 2 operators will be in the buyers, with priority from low to high order,
And then compared with the operator in the back, so reciprocating. The hardest thing to think about here is that once an operator has a higher priority than the operator behind him, you can start with whatever else you want.
Operations on both sides of the operand. )
*/
Helper Code:
int is_operation (char op)//Determines whether a character is an operator.
{
Switch (OP)
{
Case ' + ';
Case '-';
Case ' * ';
Case '/';
return 1;
Default:return 0;
}
}
int priority (char op)//The precedence of the operator.
{
Switch (OP)
{
Case ' # ': return-1;
Case ' (': return 0;
Case ' + ':
Case '-': return 1;
Case ' * ':
Case '/': return 2;
default:return-1;
}
}
infix expression--suffix expression