C language implements scientific calculator (algorithmic idea)

    The purpose of the program design is that the user can enter an infix expression and calculate the result after pressing Enter.

      First of all, we must know how to convert an infix expression into a postfix (prefix) expression. An infix expression is an expression that is easy for humans to understand, but it is very difficult for a computer to calculate an infix expression. So we first convert the infix expression to a postfix expression or convert it to a prefix expression and then calculate it.

      The infix expression distinguishes the priority of operators. Since there are priorities between different operators, there is a problem of combination of operations between operations of the same priority. So simply counting from left to right is not possible. Prefix and postfix expressions, on the other hand, are insensitive to symbol precedence, and they both have no parenthesis operators. Postfix expressions are read from front to back, and the difference between prefix expressions is that they are read from back to front. So we generally convert infix expressions into postfix expressions to calculate.

      Here we use postfix expressions to explain. Expression conversion generally uses a stack (referred to as "stack"), which can be a sequential stack or a chained stack. Here, the sequential stack is used to explain. In the process of expression conversion, the operand is directly output when it encounters an operand, so the operand The order will not change, only the order of the operators will be changed, so the expression conversion uses the symbol stack to save the operators.

      The basic process of converting an infix expression into a postfix expression by the application stack is: read each element of the infix expression from the beginning to the end, and the operation rules are divided into the following five types:

                    (1) Direct output when the operand is encountered.

                    (2) When the left parenthesis is encountered, it is directly pushed onto the stack.

                   (3) If the right parenthesis is encountered, it indicates that the infix expressions in the parentheses have been scanned, and the operators at the top of the stack are popped off the stack in turn until the left parenthesis is encountered, and then the left parenthesis is also popped, but no output ( The closing parenthesis is not pushed onto the stack).

                   (4) When an operator is encountered, compare it with the operation level of the symbol at the top of the stack. If the operation level of the current operator is greater than the operation level of the operator at the top of the stack, it will be pushed into the stack, and if it is less than or equal to, it must be popped out of the stack first, and then the current operator will be pushed. The operator pushes the stack.

                   (5) If the calculation of the infix expression is completed, all the remaining operators in the stack will be popped off the stack.

        A key to the above processing process is the setting of different operator priorities. In program implementation, a number can be used to represent the operator priority. The higher the priority, the larger the value. Here I put the left parenthesis The priority is set to 1, the plus and minus signs are set to 2, the multiplication and division signs are set to 3, and the right parenthesis is set to 4 (the right parenthesis can be set without priority).


        After converting to a postfix expression, the calculation is relatively simple. The evaluation uses a data stack to save the operand, and the operand should not be directly output when the operand is encountered, but the data stack is entered when the operand is encountered. Take two data from the data stack and perform the operation. After the operation is completed, the result is pushed into the data stack until the end of the expression, and after the end, the final calculation result will be placed on the stack data stack.


Here is the link to the code:

Click to open the link






Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659242&siteId=291194637