Prefix-Based calculation time limit: +Ms | Memory Limit:65535KB 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.005.53
-
-
-
Tip: This program is constantly recursive ... Function name: Atof functions: Converts a string into a floating-point number usage: Double atof (const char *nptr); Program Example: #include <stdlib.h> #include <stdio.h>
int main (void) {float F; Char *str = "12345.67";
f = atof (str); printf ("string =%s float =%f/n", str, f); return 0; }
Transferred from: http://blog.csdn.net/xuefu2008/article/details/4646127
-
Program: # include <stdio.h># include <stdlib.h>char c[30];int i = 0;float Fun () {if (i! = 0)scanf ("%s", c);i = 1;if (c[0] = = ' + ')return fun () + fun ();else if (c[0] = = '-')Return fun ()-Fun ();else if (c[0] = = ' * ')return Fun () * Fun ();else if (c[0] = = '/')return fun ()/fun ();Elsereturn Atof (c);} int main (void) {while (~SCANF ("%s", c)){i = 0;printf ("%.2f\n", Fun ());}return 0;}
Copy to Google TranslateTranslation Results
Nyoj 128 prefix-type calculation