Problem A: Simple Calculator
Time Limit: 1 Sec Memory Limit: 32 MBSubmit: 2115 Solved: 867
[Submit][Status][Web Board][Creator:Imported]
Description
Comprising a read only +, -, *, / nonnegative integer calculation expression, the expression of the calculated value.
Input
Test input comprising a plurality of test cases, each test case per line, each line of no more than 200 characters, separated by a space between the integer and the operator. No illegal expression. When a line input end only 0:00, not output the corresponding results.
Output
An output line, i.e. the value of the expression for each test, two decimal place.
Sample Input
30 / 90 - 26 + 97 - 5 - 6 - 13 / 88 * 6 + 51 / 29 + 79 * 87 + 57 * 92
0
Sample Output
12178.21
specific code is:
1 #include <iostream> 2 #include <string> 3 #include <stack> 4 using namespace std; 5 6 double comp(double a, double b, char c){ // 进行具体的操作 7 double temp; 8 switch(c){ 9 case '-': 10 temp = a - b; 11 break; 12 case '+': 13 TEMP = A + B; 14 BREAK ; 15 Case ' / ' : 16 TEMP = A / B; . 17 BREAK ; 18 is Case ' * ' : . 19 TEMP = A * B; 20 is BREAK ; 21 is } 22 is return TEMP; 23 is } 24 25 BOOL Judge ( char C1, char C2) {// priority comparison between the symbol and the symbol stack top element to be pushed onto the stack 26 is BOOL in Flag =true; 27 if(c1=='-'&&c2=='/' || c1=='-'&& c2=='*'){ 28 flag = false; 29 }else if(c1=='+'&&c2=='/' || c1=='+'&& c2=='*'){ 30 flag = false; 31 } 32 return In Flag; 33 is } 34 is 35 int main () { 36 String S; 37 [ Stack < char > SC; 38 is Stack < Double > SB; 39 the while (getline (CIN, S)) { 40 IF (s.size () = = . 1 && S == " 0 " ) {// only input 0, exit the program 41 is BREAK ; 42 is } 43 is Double TEMP, A, B; 44 is int I = 0 , len = s.size (); 45 char C; 46 is do { 47 TEMP = 0 ; 48 for (; I <len; I ++ ) {// operand stack 49 IF (S [I] == ' ' ) { 50 sb.push (TEMP); 51 is BREAK ; 52 is } the else { 53 is TEMP TEMP * = 10 + S [I] - ' 0 ' ; 54 is } 55 IF (I == len . 1 ) {// Note that no space after the last operation and require special handling 56 sb.push (TEMP); 57 is } 58 } 59 IF (I =! len) { 60 I ++; // skip the space before the symbol 61 is the while (! () sc.empty) has priority {// stack insofar symbol a high-level operator, both the first process stack 62 is C = sc.top (); 63 is IF (Judge (C, S [I])) { 64 // symbols need top element from the stack 65 B = sb.top (); // second operand 66 sb.pop (); 67 a = sb.top (); // first operand 68 sb.pop (); 69 TEMP = CoMP (A, B, C); 70 sb.push (TEMP); 71 is sc.pop (); 72 } the else { 73 is BREAK ; 74 } 75 } 76 sc.push ( s [i]); // current operator stack 77 I = I + 2 ; // space after the skip symbol 78 } the else { 79 B = sb.top (); //The second operand 80 sb.pop (); 81 A = sb.top (); // first operand 82 sb.pop (); 83 TEMP = CoMP (A, B, sc.top ()) ; 84 IF (! sc.empty ()) {// must not forget that the symbol stack is not empty, the operation of the operand stack, operate to ensure that the last remaining two operators 85 sb.push (TEMP); 86 } 87 sc.pop (); 88 } 89 } the while (! sc.empty ()); 90 the printf ( " % .2f \ n- " , TEMP); 91 is } 92 return 0; 93 }
note:
1, the processing of the last operand, it must not forget the stack.
2, the operator priority comparison logic, to distinguish four cases
3, when all the elements have been completed stack, the stack is not empty at this time symbols, the operand stack elements must be> = 2 in. When carried out to ensure that the last operation, the operand stack element has only two, and the result of the operation do not need to push again.