1696: Inverse Polish expression
-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
-
Describe
-
The
-
inverse Polish expression is an arithmetic expression that puts the operator in front, such as the inverse Polish notation for the normal expression 2 + 3 for + 2 3. The advantage of inverse Polish expressions is that there is no need for precedence between operators, and there is no need to change the order of operations with parentheses, for example (2 + 3) * 4 for inverse Polish notation for * + 2 3 4. The subject solves the value of the inverse Polish expression, where the operator includes +-*/four.
-
-
Input
-
-
input is a row where the operator and the operand are separated by a space and the operand is a floating-point number.
-
-
Output
-
-
output is a row, the value of an expression.
The value of the expression can be output directly with printf ("%f\n", V).
-
-
Sample input
-
-
* + 11.0 12.0 + 24.0 35.0
-
-
Sample output
-
-
1357.000000
-
-
Tips
-
-
You can use Atof (str) to convert a string to a floating-point number of type Double. The atof is defined in the MATH.H.
This problem can be solved using the method of recursive invocation of functions.
-
#include <cstdio>#include<iostream>#include<windows.h>Doublecalculation () {Chara[Ten]; scanf ("%s", a); Switch(a[0]) { Case '+': returnCalculation () +calculation (); Case '-': returnCalculation ()-calculation (); Case '*': returnCalculation () *calculation (); Case '/': returnCalculation ()/calculation (); default: returnAtof (a); }}intMain () {Doubleans; Ans=calculation (); printf ("%f\n", ans); return 0;} If the heart is sunny, no words sorrow
Inverse Polish expression (stack, recursive)