Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of evaluating inverse Polish expression in C language

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report