Infix style, variable prefix style, suffix Dar style (text plus illustration)

In order to explain the benefits of suffix expressions, let's first take a look at how the computer uses suffix expressions to calculate the final result 20.

Suffix expression: 9 3 1-3*+ 10 2/+

  • Rule: traverse each number and symbol of the expression from left to right, push the stack when it encounters a number, and push the two numbers at the top of the stack when it encounters a symbol, perform operations, and push the result of the operation to the stack until The result is finally obtained.

Here are the detailed steps:

1. Initialize an empty stack . This 桟 is used to enter and exit the number to be calculated.

2. The first three in the suffix expression are all numbers, so 9, 3, and 1 are pushed onto the stack.

3. Next is the minus sign "-", so pop 1 from the stack as a subtract, and pop 3 as a subtract, and calculate 3-1 to get 2, and then push 2 into the stack.

4. Then the number 3 is pushed onto the stack.

5. The multiplication "*" follows, which means that 3 and 2 are popped from the stack, and 2 is multiplied by 3 to get 6, and 6 is pushed into the stack.

6. The following is the addition "+", so find 6 and 9 to find, 9 and 6 are added to get 15, and 15 is pushed into the stack.

7. Then the two numbers 10 and 2 are pushed onto the stack.

8. Next is the symbol. Therefore, the top 2 and 10 are popped from the stack, and 10 is divided by 2 to get 5, and 5 is pushed onto the stack.

9. The last one is the symbol "+", so 15 and 5 are found and added to get 20, and 20 is pushed onto the stack.

10. The result is 20 pops from the stack and the stack becomes empty.

 

 

中缀变后缀:
#include <stdio.h>
#include <string.h>
char s[1000];
char a[1000];
int main()
{
    int i, n, len, e=0, j;
    memset(s, '\0', sizeof(s));
    scanf("%s", a);
    len = strlen(a);
    for(i=0; i<len-1; i++)
    {
        if(a[i]>='a'&&a[i]<='z')
            printf("%c", a[i]);
        else
        {
            if(e==0)
            {
                s[e++] = a[i];
            }
            else
            {
                if(a[i]=='(')
                {
                    s[e++] = a[i];
                }
                else if(a[i]=='+'||a[i]=='-')
                {
                    if(s[e-1]=='(')
                    {
                        s[e++] = a[i];
                    }
                    else if(s[e-1]=='*' || s[e-1]=='/' ||s[e-1]=='+' || s[e-1]=='-')
                    {
                        printf("%c", s[e-1]);
                        s[e-1] = a[i];
                    }
                }
                else if(a[i]=='*'||a[i]=='/')
                {
                    if(s[e-1]=='+' || s[e-1]=='-' || s[e-1]=='(')
                    {
                        s[e++] = a[i];
                    }
                    else if(s[e-1]=='*'||s[e-1]=='/')
                    {
                        printf("%c", s[e-1]);
                        s[e-1] = a[i];
                    }
                }
                else if(a[i]==')')
                {
                    for(j=e-1; j>=0; j--)
                    {
                        if(s[j]=='(')
                        {
                            e = j;
                            break;
                        }
                        printf("%c", s[j]);
                    }

                }
            }
        }
    }
    for(i=e-1; i>=0; i--)
    {
        printf("%c", s[i]);
    }
    printf("\n");
    return 0;
}

 

 

Illustration of infix expression and postfix expression

August 13, 2015 22:34:58

Reading number: 3853

The four arithmetic expressions we usually see are all infix expressions. This method of expression is suitable for human reading, but not suitable for computer calculations, because when the multiplication and division signs appear after the plus and minus signs, the following plus and minus signs may be calculated first. , After adding parentheses, it is even more troublesome. The computer can realize the calculation very conveniently through the reverse Polish formula.

The computer realizes the four arithmetic operations mainly in two steps:

 

  1. Convert the given infix expression form to postfix expression form
  2. Perform calculations using postfix expressions

The following is the graphic process of the above two steps (this is actually the graphic process in the book "Dahua Data Structure")

Reprinted from: Converting infix expressions to postfix expressions

                Graphical suffix expression calculation process

 

1. Infix expression to postfix expression

The infix expression "9+(3-1)*3+10/2" is converted to the postfix expression "9 3 1-3*+ 10 2/+"

Rule: Traverse each number and symbol of the infix expression from left to right. If it is a number, it will be output and become a part of the suffix expression; if it is a symbol, the priority with the top symbol of the stack is judged, which is the right parenthesis or priority If the level is lower than the top-finding symbol (multiplication and division priority addition and subtraction), the top elements of the stack will be found and output in turn, and the current symbol will be pushed onto the stack until the final output of the suffix expression.

Let's take a look at this process in detail below.

1. Initialize an empty stack to use symbols in and out of the stack.

2. The first character is the number 9, output 9, followed by the symbol "+", into the stack.

3. The third character is "(", which is still a symbol, because it is only a left parenthesis and has not been matched yet, so it is pushed into the stack.

4. The fourth character is the number 3, output, the total expression is 9 3, followed by "-" into the stack.

5. Next is the number 1, output, the total expression is 9 3 1, followed by the symbol ")", at this time, we need to match the previous "(", so the top of the stack is popped out and output until " (" until it is popped out of the stack. At this time, there is only "-" above the left bracket, so "-" is output, and the total output expression is 9 3 1-

6. Next is the number 3, output, the total expression is 9 3 1-3. It is followed by the symbol "*", because the symbol at the top of the stack at this time is the "+" sign, and the priority is lower than the "*", so it is not output and is pushed into the stack.

7. After the symbol "+", the current stack top element has a higher priority than this "+", so the elements in the stack are popped out and output (there is no lower priority than the "+" sign, so all are popped out ), the total output expression is 9 3 1-3 * +. Then the current symbol "+" is pushed onto the stack. In other words, the "+" at the bottom of the stack in the first 6 pictures refers to the "+" after the beginning 9 in the infix expression, and the "+" at the bottom of the stack (also the top of the stack) in the following figure refers to " The last "+" in 9+(3-1)*3+".

8. Immediately after the number 10, output, the total expression becomes 9 3 1-3 * + 10.

9. The last number 2, output, the total expression is 9 3 1-3*+ 10 2

10. Since it has reached the end, all symbols in the stack are popped out of the stack and output. The final output of the suffix expression result is 9 3 1-3*+ 10 2/+

  • From the derivation just now, you will find that in order for the computer to have the ability to process our usual standard (infix) expressions, the most important two steps are:

  1. Convert infix expressions into postfix expressions (the symbols used by the stack to enter and exit operations).
  2. Operate the postfix expression to get the result (the number used by the stack to enter and exit the operation).

The whole process makes full use of the last-in-first-out feature of the search. If you understand it well, you actually understand the data structure of the stack.

2. Evaluation of postfix expressions

Suffix expression: 9 3 1-3*+ 10 2/+

 

  • Rule: traverse each number and symbol of the expression from left to right, push the stack when it encounters a number, and push the two numbers at the top of the stack when it encounters a symbol, perform operations, and push the result of the operation to the stack until The result is finally obtained.

 

Here are the detailed steps:

1. Initialize an empty stack . This 桟 is used to enter and exit the number to be calculated.

2. The first three in the suffix expression are all numbers, so 9, 3, and 1 are pushed onto the stack.

3. Next is the minus sign "-", so pop 1 from the stack as a subtract, and pop 3 as a subtract, and calculate 3-1 to get 2, and then push 2 into the stack.

4. Then the number 3 is pushed onto the stack.

5. The multiplication "*" follows, which means that 3 and 2 are popped from the stack, and 2 is multiplied by 3 to get 6, and 6 is pushed into the stack.

6. The following is the addition "+", so find 6 and 9 to find, 9 and 6 are added to get 15, and 15 is pushed into the stack.

7. Then the two numbers 10 and 2 are pushed onto the stack.

8. Next is the symbol. Therefore, the top 2 and 10 are popped from the stack, and 10 is divided by 2 to get 5, and 5 is pushed onto the stack.

9. The last one is the symbol "+", so 15 and 5 are found and added to get 20, and 20 is pushed onto the stack.

10. The result is 20 pops from the stack and the stack becomes empty.

#include <stdio.h>
#include <string.h>
char s[1000];
char a[1000];
int main()
{
    int i, n, len, x, sum;
    scanf("%s", a);
    memset(s, '\0', sizeof(s));
    len = strlen(a);
    x = 0;
    sum = 0;
    for(i=0; i<len-1; i++)
    {
        if(a[i]>='0'&&a[i]<='9')
        {
            s[x++] = a[i]-48;
        }
        else
        {
            if(a[i]=='-')
            {
                sum = s[x-2] - s[x-1];
                s[x-2] = sum;
                x--;
            }
            else if(a[i]=='+')
            {
                sum = s[x-2] + s[x-1];
                s[x-2] = sum;
                x--;
            }
            else if(a[i]=='*')
            {
                sum = s[x-2] * s[x-1];
                s[x-2] = sum;
                x--;
            }
            else if(a[i]=='/')
            {
                sum = s[x-2]/s[x-1];
                s[x-2] = sum;
                x--;
            }
        }
    }
    printf("%d\n", sum);
    return 0;
}

3. Infix type to prefix type:

Infix to prefix

1+((2+3)×4)-5

 

Follow the steps below:
(1) Initialize two stacks: the operator stack S1 and the stack S2 for storing intermediate results;
(2)  Scan the infix expression from right to left ;


(3) When encountering an operand, press it into S2;


(4) When encountering an operator, compare its priority with the S1 stack top operator:
(4-1) If S1 is empty, or the stack top operator is the right parenthesis ")", enter this operator directly Stack;
(4-2) Otherwise, if the priority is higher or equal to the operator on the top of the stack, the operator is also pushed into S1;
(4-3) Otherwise, the operator on the top of the S1 stack is popped and pushed into In S2, go to (4-1) again to compare with the new top of stack operator in S1;


(5) When encountering a parenthesis:
(5-1) If it is a right parenthesis ")", then directly push S1;
(5-2) If it is a left parenthesis "(", then the operators at the top of the S1 stack will be popped up in turn, And press S2 until it encounters the closing parenthesis, then discard this pair of parentheses;


(6) Repeat steps (2) to (5) until the leftmost side of the expression;


(7) Pop up the remaining operators in S1 and push them into S2
in turn ; (8) Pop up the elements in S2 in turn and output, and the result is the prefix expression corresponding to the infix expression.

 

#include <stdio.h>
#include <string.h>
char s[1000];
char s1[1000];
char s2[1000];
char s3[1000];
int main()
{
    int i, n, len, x1, x2, j;
    scanf("%s", s);
    len = strlen(s);
    memset(s1, 0, sizeof(s1));
    memset(s2, 0, sizeof(s2));
    x1 = 0;
    x2 = 0;
    for(i=len-2; i>=0; i--)
    {
        if(s[i]>='a'&&s[i]<='z')
        {
            s2[x2++] = s[i];
        }
        else if((s[i]=='+'||s[i]=='-')&&(s1[x1-1]=='*'||s1[x1-1]=='/'))
        {
            s2[x2++] = s1[x1-1];
            s1[x1-1] = s[i];
        }
        else if(s[i]=='(')
        {
            x1--;
            while(s1[x1]!=')')
            {
                s2[x2++] = s1[x1--];
            }
            s1[x1] = 0;
        }
        else
        {
            s1[x1++] = s[i];
        }
    }
    for(i=x1-1; i>=0; i--)
    {
        s2[x2++] = s1[i];
    }
    for(i=x2-1; i>=0; i--)
    {
        printf("%c", s2[i]);
    }
    printf("\n");

    for(i=0; i<len-1; i++)
    {
        if(s[i]!='('&&s[i]!=')')
            printf("%c", s[i]);
    }
    printf("\n");

    int e = 0;
    for(i=0; i<len-1; i++)
    {
        if(s[i]>='a'&&s[i]<='z')
            printf("%c", s[i]);
        else
        {
            if(e==0)
            {
                s3[e++] = s[i];
            }
            else
            {
                if(s[i]=='(')
                {
                    s3[e++] = s[i];
                }
                else if(s[i]=='+'||s[i]=='-')
                {
                    if(s3[e-1]=='(')
                    {
                        s3[e++] = s[i];
                    }
                    else if(s3[e-1]=='*' || s3[e-1]=='/' ||s3[e-1]=='+' || s3[e-1]=='-')
                    {
                        printf("%c", s3[e-1]);
                        s3[e-1] = s[i];
                    }
                }
                else if(s[i]=='*'||s[i]=='/')
                {
                    if(s3[e-1]=='+' || s3[e-1]=='-' || s3[e-1]=='(')
                    {
                        s3[e++] = s[i];
                    }
                    else if(s3[e-1]=='*'||s3[e-1]=='/')
                    {
                        printf("%c", s3[e-1]);
                        s3[e-1] = s[i];
                    }
                }
                else if(s[i]==')')
                {
                    for(j=e-1; j>=0; j--)
                    {
                        if(s3[j]=='(')
                        {
                            e = j;
                            break;
                        }
                        printf("%c", s3[j]);
                    }

                }
            }
        }
    }
    for(i=e-1; i>=0; i--)
    {
        printf("%c", s3[i]);
    }
    printf("\n");
    return 0;
}


 

 

Guess you like

Origin blog.csdn.net/weixin_42137874/article/details/81315522