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 to use java to realize inverse Polish Calculator

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

Share

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

This article will explain in detail how to use java to realize the inverse Polish calculator. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Inverse Polish (suffix expression)

The inverse Polish expression is also called a suffix expression. Inverse Polish representation is a representation method of expression first put forward by J Lukasewicz, a Polish logician in 1929. Later, an expression written in this representation was called an "inverse Polish expression". The inverse Polish expression writes the amount of operation first and the operator at the end.

-extracted from Baidu

Inverse Polish conversion method

I will mainly explain how to convert prefix expressions into suffix expressions. Example: 4, 5, 8, 60, 8, 8, 2, 5, 8, 6, 6, 8, 8, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 4, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,

According to this expression, the corresponding binary tree can be drawn, and then traversed according to the post-order (from bottom to top, left to right and then to root), the result is 4 5 * 8 # 60 + 8 2 / +.

So the inverse Polish expression is 4 5 * 8-60 + 8 2 / +.

Analysis:

The previous implementation of the infix expression uses two stacks, one is the number stack and the other is the symbol stack. In a suffix expression, you only need to use a stack to complete a simple calculation. Traverses the expression, and if a number appears, it is pushed directly into the stack. If there is a symbol, two numbers will pop up from the stack to calculate, and then the calculated value will be put into the stack, the cycle calculation, and finally put into the stack of the value is the result of the expression.

Code implementation

Package cn.mrlij.stack; import java.util.ArrayList;import java.util.List;import java.util.Stack; / * inverse Polish expression Calculator implements * / public class PolandNotation {public static void main (String [] args) {/ / 4 "5-8" 60 # 8 # 8 String expression = "4 5 * 8-60 # 8 2 / +"; List list = getStrList (expression); System.out.println (list); / / calculated value, the result is int res = calc (list) System.out.println (res);} / * traverses the expression and puts the traversal result into the list * @ param exp expression * @ return * / public static List getStrList (String exp) {String arr [] = exp.split (""); / / traversing the string to get the array List list = new ArrayList (); for (String str: arr) {list.add (str);} return list } / / evaluate the expression public static int calc (List list) {/ / create the stack Stack stack = new Stack () that holds the string; / / traverse the list for (int I = 0 I)

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

Development

Wechat

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

12
Report