A lot of people may not be able to perform the four arithmetic operations tomorrow. let's take a look at the example of four arithmetic operations in php, where the infix expression is converted to the suffix expression. if you need to know more, let's take a look. four arithmetic expressions, which we use in writing is called the infix expression, while the calculator is even better... A lot of people may not be able to perform the four arithmetic operations tomorrow. let's take a look at the example of four arithmetic operations in php, where the infix expression is converted to the suffix expression. if you need to know more, let's take a look.
Four arithmetic expressions, which we use in writing, are called infix expressions, while calculators prefer suffix expressions, bracket priorities, addition, subtraction, multiplication, division, and so on, which make it difficult to enclose four expressions in operations, this introduces a computer-preferred format, called a suffix expression. This article uses PHP code to implement the logic of converting an infix expression to a suffix expression.
In this article, PHP is used as the code environment. some people will say that it is good to write expressions directly in advanced languages. they will calculate, but why and how do they calculate them? we still need to convert the infix expression into a suffix expression, therefore, the code in this article only simulates a logic.
For example:The traditional four arithmetic expressions (infix expressions) are 9 + (3-1) * 3 + 10/2, and the corresponding suffix expression is 9 3 1-3 * + 10 2/+.
Conversion logic:One character and one character input. if it is a number, it is output directly. if it is a left bracket, it is directly written into the stack. if it is a right brace, it starts to exit the stack until the first left brace is encountered, if it is addition, subtraction, multiplication, division, it is judged that if the top of the stack is also a symbol, and the priority of the input symbol is not higher than the symbol priority of the top of the stack, all are out of the stack, otherwise the input symbol is in the stack.
0) {$ key = (count ($ stack)-1); if (in_array ($ stack [$ key], array ('+ ','-', '*', '/') {// if (checkPriority ($ str, $ stack [$ key]) with a higher priority than the stack top symbol. = 1) {for ($ I = $ key; $ I >=0; $ I --) {if ($ stack [$ I] = '(') {break;} $ newStrList. = $ stack [$ I]. ''; unset ($ stack [$ I]); $ stack = array_values ($ stack );}}} // The symbol $ stack [] = $ str;} else {$ stack [] = $ str ;}} /*** determine the operator priority * @ param $ operatorA * @ param $ operatorB * @ return A is greater than B, return 1, and return 0 if A is equal to B, if A is less than B, return-1 */function checkPriority ($ operatorA, $ operatorB) {switch ($ operatorA) {case '+': case '-': if ($ operat OrB = '+' | $ operatorB = '-') {return 0 ;} else if ($ operatorB = '*' | $ operatorB = '/') {return-1;} break; case '*': case '/': if ($ operatorB = '+' | $ operatorB = '-') {return 1 ;} else if ($ operatorB = '*' | $ operatorB = '/') {return 0;} break; default: exit ('error ');}} // stack $ stack = array (); // expression to be converted $ strList = '9 + (3-1) * 3 + 10/2 '; // New Expression $ newStrList = ''; $ strList = explo De ('', $ strList); foreach ($ strList as $ str) {if ($ str! = '') {Suffix ($ str, $ stack, $ newStrList) ;}// array reverse while ($ s = array_pop ($ stack) {$ newStrList. = $ s. '';} echo $ newStrList;
Permanent address:
Reprint at will ~ Please bring the tutorial URL ^