Blue Bridge cup _ algorithm training _ expression calculation, Blue Bridge expression
The teacher mentioned this problem before learning the stack.
Just thinkingYes:
1. Convert the expression (infix) into a suffix;
2.Suffix calculation.
The idea looks simple, but you still need to pay attention to many issues when coding. Next, I will share my personal practices. I hope you can make some improvements and make common progress!
I. converting an infix to a suffix
I personally use the input side for processing: first set two stacks, one is the suffix stack and the other is the symbol stack. If it is a number, it is directly put into the suffix stack; if it is a symbol:
(1) Compare operator priority. If the top symbol of the symbolic Stack has a higher priority than the symbol to be pushed into the stack, the top element of the symbolic stack will pop up and enter the suffix stack, then compare the top elements of the stack with the elements to be added to the stack until the top element has a lower priority than the elements to be added to the stack. If the top symbol of the symbolic Stack has a low priority, the symbols to be pushed into the stack will be pushed into the symbol stack, which is called the new top element of the stack. The above involves the comparison of operator priority. The implementation code is as follows:
Detailed description of the purple box: We know that '(' has the highest priority. If it is compared with other symbols as the top element of the stack, it will pop up to the suffix stack. However, we do not want the suffix stack to have parentheses, but we want the symbol stack to be retained, because we need to find the ')' following it and pair it with the left bracket. Therefore, we need to agree that if the top element of the stack is left parentheses, the next symbol will be directly added to the stack and will not be compared.
The above is the code for our suffix conversion. In the suffix function (converted to a suffix), we use the input side for processing. If it is a number, we directly go to the stack and call the isNum function. If it is a symbol, we call the isSymbol function.
In the isSymbol function, we do not only judge the priority, because the special symbol of parentheses exists, so we need to give special consideration. When we encounter the right parenthesis, we should pop up the elements in the symbol stack until we encounter the left parenthesis, but the left parenthesis is not popped into the suffix stack.
If it is not the right parenthesis, we will judge the priority. However, before that, we need to set the demarcation mark. Here we use the '#' sign. Why set the delimiter? We know that when reading an expression, the char type is used and read one by one. The problem is that if the number is less than 10, it will not be affected, but if it is greater than or equal to 10, it will be divided into multiple parts for reading. For example, if 23 is read in 2 or 3, in different array units, you do not know whether it is a single-digit or two-digit number, so we need the '#' sign, and the suffix form becomes: 1 #23 #4 # + ...... in this way, it is easy to distinguish the number of operands.
When I did it for the first time, I ignored the problem: I pushed all the remaining elements of the symbol stack into the suffix stack. After comparing priorities, many symbols may not enter the suffix because they are behind the operation order, so they cannot be forgotten.
Here, our suffix conversion is complete. The project is half done, followed by suffix-based computing:
As mentioned above, numbers are differentiated by the '#' sign in the suffix. When we calculate the numbers, we must calculate the operands accurately, into the digital stack (num.
Explanation of the red box: For the suffix after conversion, the method I think of for calculating the operands is to set a first and last pointer, which is an operand between first and last, the distinguishing sign is that there must be several symbols between two operands.
You can understand the code again (maybe my method is too troublesome here, I hope you can give me some advice)
Now we start computing:
It is relatively simple here. If you encounter a number, you can call the cul_num function to calculate the number. If you encounter a number, you can perform a symbol operation. Here, we need to note the first and last settings. In addition, we should also consider special situations. One of the problems I encountered at the time was:
Example: 1 #2 #3-#4... when the '-' minus sign is reached, the program collapsed. Later, I understood the problem. When the last point is to the minus sign, the subtraction function is executed, and then I ++. At this time, it points to the '#' number in the suffix stack, but in the calculate function, there is no corresponding processing for #, so the program crashes. The analysis shows that we don't need #, So I ++ points to the next element in the suffix stack. This is the meaning of "ignored" in the above Code comment.
(I am writing a blog for the first time. I hope you can criticize and correct me if I am not there. I will continue to work hard! Sharing !)