1. Simple arithmeticAlgorithm: For example, 1X2 + 3/3x4 + 8
The arithmetic above is relatively simple, with no parentheses. It is just a general operation. Now the only thing we need to do is to figure out the priority. In addition to the data structure, we can implement the stack assistant.
The basic algorithm is as follows: there must be two stacks, one for data storage, and the other for operators. If there are numbers, they should be placed in the data stack. If they are operators, the priority needs to be compared, if the priority is lower than that of the operator at the top of the current stack, It is pushed into the stack. If the priority is higher than that of the operator at the top of the stack, the data on both sides of the symbol must be calculated to obtain the result, place the result in the data stack, and the top symbol of the operator remains unchanged. For example, if the operator is lower than the top operator of the stack, You need to calculate the two numbers at the end of the data stack, in this way, the stack priority in the final operator is the same, and the data stack is directly read in sequence. The operator stack can be computed from the beginning.
2. complicated arithmetic questions 1*(2 + 3)/(3*4 + 8)
The basic principle is the same as above. In sequence, the values in the brackets are calculated and pushed to the data stack.