Problem C: Alice's Calculating Method (2011-ish)


Description

You're lounging at your friend's house when her grade-three daughter Alice arrives home from school.

“ You know, the problem with arithmetic and algebra is parentheses! They are a mess. We need to get rid of them. ”

“Say what? ”

“Like, if I ask you what is ‘3 + 5 * 7’, is it ‘56’ or is it ‘38’?”

“Well, see, that is where precedence decides. It should be ‘38’ because…”

“Our teacher says to use parentheses so that it is clear. So like ‘(3 + 5) * 7’ or ‘3 + (5 * 7)’.”

“That seems reasonable enough.”

“Bleah. It is horrible! I can't stand parentheses!”

“Why on earth would you dislike…”

“ But listen. I have a way of getting rid of them! Put the operators — you know, the ‘+’, ‘-’, and such — in front of the numbers, not in between. That is where all the trouble is from. So I'll say ‘* 2 3’ instead of in the middle as in ‘2 * 3’. And ‘3 + (5 * 7)’ would be ‘+ 3 * 5 7’. See, no parentheses needed.”

“By the way, the ‘in between’ is called ‘infix’. Your way is called ‘prefix’. Even more common though is… Oh, never mind. Yes, that is a very nice idea.”

“My calculator doesn't do the prefixy one, though. Only the stupid infixy one. So…would you program up the prefixy one for me?”

“Calculators aren't very expensive, you know!”


Input

The input will consist of multiple lines of prefix arithmetic equations, one per line. Each formula will be 80 characters or fewer. The allowed operators are

  1. +’ for addition (e.g., ‘+ 2 3’ evaluates to ‘5’);
  2. -’ for subtraction (e.g., ‘- 2 3’ evaluates to ‘-1’);
  3. *’ for multiplication (e.g., ‘* 2 3’ evaluates to ‘6’);
  4. /’ for division (e.g., ‘/ 2 8’ evaluates to ‘0.25’);
  5. ^’ for exponentiation (e.g., ‘^ 2 3’ evaluates to ‘8’); and
  6. P’ for precision, which sets the precision — digits after the decimal point — to use for presenting the answer. (e.g., ‘P 3’).

Sample Input

+ 2 3
+ * 2 3 4
/ + 1 2 2
^ 2 3
P 5 / 2.5 3
P 5 / 2.5 .5
/ ^ P + 1 2 2 5 3
/ 3 - 1 1
P / 1 2

* 3
+ 2 3 4

Output

For each input prefix formula, output the number (the answer) that is the evaluation of that formula. You are guaranteed that the formulas will be syntactically correct: just numbers and the operators listed above. You are also guaranteed that the calculation — and all intermediate calculations — all fit in a double.

You may encounter a “bad” value during calculation. You should handle two cases:

If you encounter such a case, print “bad value”.

Which each formula is syntactically correct, it may not be well formed; it may have too many or two few numbers for a given operator. (An empty line would be malformed.) If you encounter such a case (and you encountered no bad values), print “malformed”.

You should print the answer at the precision P. Except, if the answer is integer, print it with precision 0 (no decimal point). The default precision is 2. Round as format or printf does; e.g., “P 3 / 2 3” is “0.667”. The (last) P evaluated from the formula resets the precision, if any. Print fractions between -1 and 1 with a leading zero; e.g., “0.33”.


Sample Output

5
10
1.50
8
0.83333
5
10.667
bad value
bad value
malformed
malformed
malformed