Prefix computing time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
-
Description
-
First, describe what is an infix:
Such as 2 + (3 + 4) * 5. The most common formula is the infix.
Adding brackets to the infix type in the order of operation is: (2 + (3 + 4) * 5 ))
Then, the operator is written before the brackets (+ (2 * (+ (3 4) 5 ))
Remove the brackets: + 2 * + 3 4 5
The formula is the prefix of the expression.
Here is a prefix expression. Calculate the value of this prefix.
For example:
+ 2 * + 3 4 5 is 37
-
Input
-
There are multiple groups of test data. Each group of test data occupies one row. There is a space between any two operators and between any two operands. The two input operands may be decimal places. The data ensures that all input operands are positive and smaller than 10, and the number of operands cannot exceed 500.
End sign with EOF as input.
-
Output
-
Output the value of this prefix expression for each group of data. The output result is retained with two decimal places.
-
Sample Input
-
+ 2 * + 3 4 5+ 5.1 / 3 7
-
Sample output
-
37.005.53
Prefix calculation: traversing from the back to the front, When a number is pushed into the stack, and when a symbol is encountered, two numbers are popped up for calculation.
(The first operand is the number of outgoing stacks) and then pushed into the stack.
1 #include<stdio.h> 2 #include<string.h> 3 #include<stack> 4 using namespace std; 5 double calPerfix(char *input) 6 { 7 stack<double> result; 8 int len = strlen(input) - 1; 9 int n,j,i; 10 while (len >= 0) 11 { 12 j = len; 13 double sum = 0.0,dot = 0.0; 14 if (len == 0){ 15 j = 0; 16 n = j; 17 } 18 else { 19 while (input[j] != ‘ ‘) 20 j--; 21 n = j+1; 22 } 23 if (input[n] >= ‘0‘ && input[n] <= ‘9‘){ 24 for (i=n; i <= len; i++) 25 { 26 if(input[i] >= ‘0‘ && input[i] <= ‘9‘) 27 { 28 double num = input[i] - ‘0‘; 29 sum = sum * 10 + num; 30 } 31 else if (input[i] == ‘.‘) 32 { 33 int l = 10; 34 dot = 0.0; 35 for (int k=i+1; k <= len; k++ ) 36 { 37 if(input[k] >= ‘0‘ && input[k] <= ‘9‘) 38 { 39 double num = input[k] - ‘0‘; 40 dot += num / l; 41 l *= 10; 42 i++; 43 } 44 } 45 } 46 } 47 sum += dot; 48 result.push(sum); 49 } 50 else { 51 double one; 52 switch (input[n]) 53 { 54 case ‘+‘: 55 sum = result.top(); 56 result.pop(); 57 one = result.top(); 58 result.pop(); 59 sum += one; 60 result.push(sum); 61 break; 62 case ‘-‘: 63 sum = result.top(); 64 result.pop(); 65 one = result.top(); 66 result.pop(); 67 sum -= one; 68 result.push(sum); 69 break; 70 case ‘*‘: 71 sum = result.top(); 72 result.pop(); 73 one = result.top(); 74 result.pop(); 75 sum *= one; 76 result.push(sum); 77 break; 78 case ‘/‘: 79 sum = result.top(); 80 result.pop(); 81 one = result.top(); 82 result.pop(); 83 sum /= one; 84 result.push(sum); 85 break; 86 default: 87 break; 88 } 89 } 90 len = j - 1; 91 } 92 return result.top(); 93 } 94 int main() 95 { 96 char input[10000]; 97 while(gets(input)){ 98 printf("%.2f\n",calPerfix(input)); 99 fflush(stdin);100 }101 return 0;102 }
Nyoj 128 prefix Calculation