1. Algorithm Ideas convert to suffix: Iterates the infix expression from left to right, encountered operand, output, encountered operator, current operator priority greater than greater than or equal to operator of the current operator, the current operator into the stack. conversion to prefix: from right to left to iterate infix expression, encountered operand, output, encountered operator, the current operator priority is greater than or equal to the top of the stack operator priority, into the stack, otherwise, The top priority of the pop-up stack is greater than the current operator, and the current operator is in the stack. --refer to the URL
The above method needs to first define the operator precedence, of course, can be defined. But this can be a lot of trouble, so it's better to use parentheses to qualify. This makes it unnecessary to write a decision-priority function.
2. infix expression to suffix expression' (', ' + ', '-', ' * ', '/' into the stack
') ' in the output stack to ' (' element
Other characters direct outputattached code:
void Intopast (char* str)// infix to suffix { int slen = strlen (str);//character length int top =-1; Stack top top++; for (int i = 0; i < Slen; i++) //Positive order { char c = str[i]; Extract one character switch (c) //Compare { //' (', ' + ', '-', ' * ', '/' into the stack //') ' in the output stack to ' (' element //other character direct output Case ' (': stacks[top++].s = c; break; Case ') ': while (stacks[--top].s! = ' (') cout << stacks[top].s; break; Case ' + ': stacks[top++].s = c; break; Case '-': stacks[top++].s = c; break; Case ' * ': stacks[top++].s = c; break; Case '/': stacks[top++].s = c; break; Default: cout << C; } } cout << Endl;}
3. infix expression to prefix expression' ) ', ' + ', '-', ' * ', '/' into the stack
' (' Save Stack to ') ' element
Save other characters directly reverse the saved string output is the prefix expression attached code:
void Intoprev (char* str)//infix to prefix {int slen = strlen (str);//character length int top =-1; Stack top top++; Char Res[max_length]; int num = 0; for (int i = slen-1; I >= 0; i--)//reverse order {char c = str[i]; Extract one Character switch (c)//Compare {//') ', ' + ', '-', ' * ', '/' into the stack//' (' Save Stack to ') ' element//other character straight Save case ') ': Stacks[top++].s = C; Break Case ' (': while (stacks[--top].s! = ') ') res[num++] = STACKS[TOP].S; Break Case ' + ': Stacks[top++].s = C; Break Case '-': Stacks[top++].s = C; Break Case ' * ': Stacks[top++].s = C; Break Case '/': Stacks[top++].s = C; Break default:res[num++] = c; }}//reverse the saved string output is the prefix expression for (int i = num-1; I >= 0; i--) cout << res[i]; cout << Endl;}
4. SOURCE program
#include <iostream> #include <cstring>using namespace std;int const MAX_LENGTH = 100;typedef struct{char s; }stacks; Stack structure definition stacks stacks[max_length]; stack void Intopast (char* str)//infix to suffix {int slen = strlen (str);//character length int top =-1; Stack top top++; for (int i = 0; i < Slen; i++)//positive order {char c = str[i]; Extract one Character switch (c)//Compare {//' (', ' + ', '-', ' * ', '/' into the stack//') ' in the output stack to ' (' elements//other characters straight Connect output case ' (': Stacks[top++].s = C; Break Case ') ': while (stacks[--top].s! = ' (') cout << stacks[top].s; Break Case ' + ': Stacks[top++].s = C; Break Case '-': Stacks[top++].s = C; Break Case ' * ': Stacks[top++].s = C; Break Case '/': Stacks[top++].s = C; Break Default:cout << C; }} cout << Endl;} void Intoprev (char* str)//infix to prefix {int slen = strlen (str);//character length int top =-1; Stack top top++; Char Res[max_length]; int num = 0; for (int i = slen-1; I >= 0; i--)//reverse order {char c = str[i]; Extract one Character switch (c)//Compare {//') ', ' + ', '-', ' * ', '/' into the stack//' (' Save Stack to ') ' element//other character straight Save case ') ': Stacks[top++].s = C; Break Case ' (': while (stacks[--top].s! = ') ') res[num++] = STACKS[TOP].S; Break Case ' + ': Stacks[top++].s = C; Break Case '-': Stacks[top++].s = C; Break Case ' * ': Stacks[top++].s = C; Break Case '/': Stacks[top++].s = C; Break default:res[num++] = c; }}//reverse the saved string output is the prefix expression for (int i = num-1; I >= 0; i--) cout << res[i]; cout << Endl;} int main () {//test data char str1[] = "((a+b) *c)"; Char str2[] = "((A * (b+c)) *d)"; Intopast (STR1); Intopast (STR2); Intoprev (STR1); Intoprev (STR2); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
infix expressions to prefix expressions and suffix expressions