Calculation of suffix expressions [C language] [data structure]

What is a postfix expression?

Reverse Polish Expression - Alchetron, The Free Social Encyclopedia

Postfix expressions are also known as (reverse Polish expressions). Let’s first look at our common ones: 2 * 5 - ( 3 + 9 ) / 6, which is actually an infix expression; writing it as a suffix expression is:

2 5 * 3 9 + 6 / - (each character separated by a space).

How does infix expression change to postfix expression? ? Why do you write like this? ?

Infix expression to postfix expression is actually very simple:

1. First add parentheses to the pair of operands of each operator. (+, -, *, / are binary operators)

2. Put the operator in each parenthesis to the right of the parentheses (this is why it is called a suffix expression, you mentioned that it is a prefix expression before the parentheses).

3. Remove all parentheses. For the convenience of observation, each number/operator is separated by a space

First answer why you want to write this way. This is actually for the convenience of the computer to calculate. Let’s start with the above formula. If it is calculated by a human, you will know that you need to calculate the brackets first, then calculate the multiplication and division, and then calculate the addition and subtraction. But computers are not so "smart". If you give it to the computer to calculate, you have to write it in the form of a suffix expression.

This is understood, so how to calculate it?

The suffix expression is to facilitate "brainless calculation". The calculation of infix expressions needs to be prioritized. In the calculation process of suffix expressions, you don’t need to worry about so much. You can directly calculate when you encounter operators (it should be noted that if you subtract '-' or divide '/', It is necessary to use the result that is popped first as the divisor, and the one that is popped later as the dividend, which is reflected in the following code), in short, there is a rule === "" "The last pop element (+, -, *, /) first pop element". (The consideration here is still relatively simple, ignoring the case that the divisor may be 0).

See the picture below for details

 Program source code:

#include <stdio.h>
#include <stdlib.h>

#define Size 100
//堆栈结构体类型
typedef struct stack {
    int top;
    int nums[Size];
} St;

//函数声明
void ruzhan(St *S, int x);

int chuzhan(St *S);

int main() {
    St S;S.top = -1;
    int sign = 0, x, x1, x2;
    char c;
    scanf("%c", &c);   //接收一个字符
    while (c != '@') { //接收字符直到遇到终止符号
        if (c == ' ' && sign == 1) {
            sign = 0;
            ruzhan(&S, x);
        }
        if (c >= '0' && c <= '9') {
            if (sign != 1) {
                x = (int) (c - '0');
                sign = 1;
            } else {
                x = x * 10 + (int) (c - '0');
            }
            //-----------------------------------
        } else {
            switch (c) {   //判断是否为运算符
                case '+':
                    ruzhan(&S, chuzhan(&S) + chuzhan(&S)); //出栈两次计算其相加结果并入栈
                    break;
                case '-':
                    x1 = chuzhan(&S);    //减数
                    x2 = chuzhan(&S);    //被减数
                    ruzhan(&S, x2 - x1); //计算差
                    break;
                case '*':
                    ruzhan(&S, chuzhan(&S) * chuzhan(&S)); //计算乘法
                    break;
                case '/':
                    x1 = chuzhan(&S);    //除数
                    x2 = chuzhan(&S);    //除数
                    if (x1 == 0) {       //除数为0退出
                        exit(0);
                    }   //合法则将其商入栈
                    ruzhan(&S, x2 / x1); //计算商
                    break;
            }
        }
        scanf("%c", &c);   //继续接收字符
    }
    printf("后缀表达式计算结果:%d", chuzhan(&S));   //输出结算结果
}

int chuzhan(St *S) {
    St *p = S;
    if (p->top < 0) {
        exit(0);
    } else {
        return p->nums[(p->top)--];
    }
}

void ruzhan(St *S, int n) {
    St *p = S;
    if (p->top >= Size - 1) {
        return;
    } else {
        (p->top)++;
        p->nums[p->top] = n;
    }
}

Forgot to say, 2 * 5 - ( 3 + 9 ) / 6 evaluates to 8;

Calculate its suffix expression using the program just written: 2 5 * 3 9 + 6 / - . The results must be consistent; the running results are shown in the figure below:

 

Regarding the following piece of code, its function is to convert the input continuous numeric characters into numbers with corresponding digits, such as the string "123", and convert it into the number one hundred and twenty-three. Without the following algorithm support, the program can only calculate the range of operands from -9 to +9.

 

if (c == ' ' && sign == 1) {
   sign = 0;
   ruzhan(&S, x);
}
if ( c <= '9'&&c >= '0' ) {
    if (sign != 1) {
        x = (int) (c - '0');
        sign = 1;
    } else {
        x = x * 10 + (int) (c - '0');   //累加和
    }
} 

In fact, it is quite different from the way of judging words: [C Language] Enter a line of strings and count the number of words in it_.鲜肉博客-CSDN博客

Guess you like

Origin blog.csdn.net/weixin_64811333/article/details/127775572