RPNEVAL - Evaluate simple RPN expression

Evaluate an expression in Reverse Polish Notation (otherwise known as Postfix notation). Assume there are 4 numerical operators: + - * /, and numbers can be floating point values. Each token in the expression, be it a number of operator, is separated from its neighbours by one or more spaces, to make the expression easy to parse. The program should read a series of input strings from stdin and output either the evaluated number to 4 decimal places (use the %.4f print format), or the single word "ERROR". If the expression is not parseable, does not evaluate correctly, or leaves extra data on the stack, the output should be "ERROR". An example INPUT/OUTPUT sequence is given below for reference.

Input

A series of RPN expressions, one per line on stdin.

Output

One line for each output element - either the numerical output, or the string "ERROR"

Example

Input:
1 2 3 /
2 3 /
3 4 * /
1 2 4 + - 5 * 7 /

Output:
ERROR
0.6667
ERROR
-3.5714

Added by:Nitin Chandrachoodan
Date:2013-01-26
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:EE4371 course at IIT Madras

hide comments
2023-12-14 19:01:47 D¿oana
Yes, you have to read the input until EOF.
2021-11-06 13:10:27
I think it is read until EOF
2020-06-09 22:41:53
same issue
dk when to terminate
giving tle
2020-05-16 10:29:44
Hey can anyone help me with when i should stop taking input.
2020-04-03 18:04:05
When should we need to terminate the process of getting input. No limit or no of test case as input is also not provided what we should do? Can any one plz suggest a way
2020-03-27 21:36:37
al final cuantos casos de prueba se hacen para este problema
2019-07-28 03:36:17
excuse me but how many tests are there ?
2019-06-26 02:06:00
Use stack and parse each character in the input.
2018-12-23 16:08:50
my answer is correct for given inputs but getting wrong answer on spoj
please help


Last edit: 2018-12-23 16:09:27
2018-12-15 08:14:35
Consider using test cases. Though you can easily input using `while(std::getline(std::cin, input_exp)) {}`
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.