[Huawei OJ] anti-Polish

Source: Internet
Author: User
Description:

In the inverse Polish notation, operators are placed behind the operands. For example, when "three plus four" is expressed, "3 4 +" is written instead of "3 + 4 ". If there are multiple operators, the operator is placed behind the second operand, so the "3-4 + 5" of the General infix method is in the inverse wave.

Write 3 4-5 + in the logging method. "3 minus 4, plus 5. One advantage of using the inverse Polish notation is that parentheses are not needed.


Compile the result of Java function compute inverse polish expression.


Enter a reverse polish expression,

1. the operand type is a positive double number, which complies with Java's double constant specification. Different operation numbers are separated by spaces or operators;

2. Four arithmetic operations are supported, that is, the operators include +,-, *, And ,-,*,/.

Output as computing result

If the input is "1 2 + 3 *", the output is "9.0"

Exam points:

1. A simple expression (50 points) can be correctly processed. For example, the result of 1.0 + 2 is 3.0.

2. Correct Processing of different operators (30 points). For example, the result of 1 2 + 3 * calculation is 9.0.
3. correctly identifies the syntax error (10 points) in the input inverse polish expression. If the input is 1 +, It is thrown.

Rpnsyntaxerrorexception exception

4. correct identification of the Division by zero (10 points) in the input inverse polish expression, if the input is 1 2 2 -/

Throwing devidebyzeroexception

 
Knowledge point:
Source: Internal sorting
Exercise phase: Advanced
Running time limit: 10sec
Memory limit: 128 Mbyte
Input:

Input formula. Numbers are separated by spaces.

 
Output:

Output computing result

 
Sample input:
1 2 + 3 *
Sample output:
9.0
Answer:



This article from the "mess" blog, please be sure to keep this source http://541777359.blog.51cto.com/5408989/1533935

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.