The method of evaluating inverse Polish expression in C language
This article mainly explains "the method of evaluating the inverse Polish expression in c language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "c language inverse Polish expression evaluation method"!
Title
The value of the expression is calculated according to the inverse Polish representation.
Valid operators include +, -, *, /. Each Operand can be an integer or another inverse Polish expression.
Description:
Integer division retains only the integer portion.
A given inverse Polish expression is always valid. In other words, the expression always produces a valid value and there is no case where the divisor is 0.
Example 1:
Enter: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: (2 + 1) * 3) = 9
Example 2:
Enter: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Enter: ["10", "6", "9", "3", "+", "- 11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation:
((10 * (6 / (9 + 3) *-11)) + 17) + 5
= (10 * (6 / (12 *-11)) + 17) + 5
= (10 * (6 /-132)) + 17) + 5
= (10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Problem-solving ideas
Use the data structure stack to solve this problem.
Traverse the array from the back to the back
If you encounter a number, press it into the stack.
When you encounter a symbol, take out the two numbers at the top of the stack and press the result into the stack.
After traversing the entire array, the number at the top of the stack is the final answer.
Animation demonstration
The animation shows that GIF loads a little slowly. Please wait a moment to load and display ^ _ ^.
Reference code
At this point, I believe you have a deeper understanding of "c language inverse Polish expression evaluation method". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!