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

How PHP uses Stack to complete Advanced Calculator

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

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to use PHP stack to complete advanced calculators, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

When the string'3-4-3-2 'appears, there are many ways in our minds to calculate this result, such as eval/ regularization and so on, but today Xiao Meng is telling you about its underlying implementation principle, stack.

The code idea is as follows:

1. A stack of numbers, a stack of symbols, and operators

2. Scan the string in a loop. If it is a number, it will enter the number stack. If it is an operator, it will be divided into the following cases:

(1) if the symbol stack is empty, it will enter the stack directly.

(2) if the priority of the current operator (the operator that is about to be placed on the stack) is less than or equal to the priority of the operator at the top of the symbol stack, two digits will be put out of the stack and then an operator will be calculated in the symbol stack, and then the result of the calculation will be stored in the digital stack and the operator in the symbol stack.

(3) if the precedence of the operator is higher than the operator at the top of the symbol stack, it will go directly into the stack.

3. Calculation result: if the symbol stack is empty, the calculation will be completed, and the calculation steps are as follows: two numbers from the number stack and one symbol stack from the symbol stack, and then stored in the number stack, and then the operation result is in the number stack.

On the code:

Over, it's over.

The above is how PHP uses the stack to complete advanced calculators. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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