In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how Java calculates the value of inverse Polish expression". In daily operation, I believe many people have doubts about how Java calculates the value of inverse Polish expression. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how Java calculates the value of inverse Polish expression". Next, please follow the editor to study!
Some examples:
["2", "1", "+", "3", "*"]-> (2 + 1) * 3)-> 9 ["4", "13", "5", "/", "+"]-> (4 + (13 / 5))-> 6
Topic: calculate the value of the inverse Polish expression.
Idea: use stack implementation: operands go into the stack; when operators are encountered, operands are off the stack, evaluated, and the results are put on the stack; when repeated, the top of the stack is the value of the expression.
Definition of inverse Polish expression:
Inverse Polish notation (Reverse Polish notation,RPN, or inverse Polish notation) is a mathematical expression introduced by Polish mathematician Janvukasevic in 1920. In inverse Polish notation, all operators are placed after operands, so it is also called suffix notation. Inverse Polish notation does not require parentheses to identify the priority of the operator.
In inverse Polish notation, the operator is placed after the Operand. For example, when expressing "three plus four", write "34 +" instead of "3 + 4". If there is more than one operator, the operator is placed after the second Operand, so the "3-4 + 5" of the regular infix notation is written as "34-5 +" in the inverse Polish notation: 3 minus 4, plus 5. One advantage of using inverse Polish notation is that there is no need to use parentheses. For example, in infix notation, "3-4 * 5" is different from "(3-4) 5", but in suffix notation, the former is written as "345 -", which means "3 (45) −" without ambiguity, and the latter is written as "34-5".
Interpreters for inverse Polish expressions are generally stack-based. The process of interpretation is generally as follows: operands enter the stack; when operators are encountered, operands are off the stack, evaluated, and the results are put into the stack; when repeated, the top of the stack is the value of the expression. Therefore, the evaluation of the inverse Polish expression is easy to implement using the stack structure, and can be evaluated quickly.
Language: cpp
Class Solution {public:int evalRPN (vector& tokens) {stack switint result, rnum, lnum;int size = tokens.size (); for (int I = 0; I < size; iTunes +) {if (tokens [I] = "*") {rnum = s.top (); s.pop (); lnum = s.top (); s.pop () Result = lnum * rnum; s.push (result);} else if (tokens [I] = "/") {rnum = s.top (); s.pop (); lnum = s.top (); s.pop (); result = lnum / rnum S.push (result);} else if (tokens [I] = "+") {rnum = s.top (); s.pop (); lnum = s.top (); s.pop (); result = lnum + rnum; s.push (result) } else if (tokens [I] = "-") {rnum = s.top (); s.pop (); lnum = s.top (); s.pop (); result = lnum-rnum; s.push (result) } else {s.push (atoi (tokens [I] .c _ str ());}} return s.top ();}}; at this point, the study of "how Java calculates the value of an inverse Polish expression" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.