Original title address: http://acm.nyist.net/JudgeOnline/problem.php?pid=128
Prefix-Type calculation
Time limit: Ms | Memory Limit: 65535 KB
Difficulty: 3
Describe
First explain what is infix:
such as 3+4, the most common expression of our formula is infix.
And the infix type in the order of operation with parentheses is: ((3+4))
Then put the operator in front of the parentheses is + (2 * (+ (3 4) 5))
Remove the brackets: + 2 * + 3 4 5
The last expression is the prefix representation of the formula.
Give you a prefix expression, please calculate the value of the prefix.
Like what:
+ 2 * + 3 4 5 The value is 37
Input
There are multiple sets of test data, one row for each set of test data, between any two operators, and between any two operands, and there is a space between the operand and the operator. The two operands you enter may be decimals, the data guarantees that the number of inputs is positive, and both are less than 10 and the number of operands is less than 500.
A flag that ends with EOF as input.
Output
For each set of data, output the value of the prefix expression. The output retains two decimal places.
Sample input
+ 2 * + 3 4 5
+ 5.1/3 7
Sample output
37.00
5.53
The following is the God code seen in the discussion area
1#include <stdio.h>2#include <stdlib.h>3 DoubleZ ()4 {5 Chara[Ten];6 if(!~SCANF ("%s", a))7Exit0);8 Switch(*a) {9 Case'+':returnZ () +z ();Ten Case'-':returnZ ()-z (); One Case'*':returnZ () *z (); A Case'/':returnZ ()/z (); - default:returnAtof (a); - } the } - Main () - { - while(1) +printf"%.2f\n", z ()); -}
Nyoj 128 prefix-type calculation