[HDU - 1237] simple calculator

This problem nothing to say. Simulation is bad, I think so:

Each read number three and two operators, if the first operator is "+" or "-" followed by a "*" or "/" then the first subsequent processing, then the value after processing and put it back in the figures have not used, the first operator is also returned.

After other cases, it is the first count value after processing the front and back not with the numbers, the same back into the second operator. (I can think of why)

Want to start with a stack but found that with the use of too much trouble to replace the double-ended queue up hhh are interested can go to learn about, quite simple.

Note: 1. Because the character is read into digital, digital pay attention to more than one , the sample run will know

   2. Topic require high accuracy needed Double , float will burst

 1 #include <bits/stdc++.h>
 2 #define maxn 200 + 10
 3 using namespace std;
 4 int main()
 5 {
 6     char s[maxn];
 7     while (gets(s) != NULL) {
 8         int len = strlen(s);
 9         if (len == 1 && s[0] == '0') break;
10         deque<char> q1; deque<double> q2;
11         for (int i = 0; i < len; i++)
12         {
13             int temp = 0;
14             if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/')
15                 q1.push_back(s[i]);
16             else if (s[i] >= '0' && s[i] <= '9')
17             {
18                 while (s[i] != ' ' && i < len)
19                     temp = temp * 10 + s[i++]-'0';
20             //printf("%d\n", temp);
21                 q2.push_back(temp);
22             }
23         }
24         double temp;
25         while (q2.size() > 2)
26         {
27             char oper1 = q1.front(); q1.pop_front();
28             char oper2 = q1.front(); q1.pop_front();
29             double num1 = q2.front(); q2.pop_front();
30             double num2 = q2.front(); q2.pop_front();
31             double num3 = q2.front(); q2.pop_front();
32             if ((oper1 == '+' || oper1 == '-') && (oper2 == '*' || oper2 == '/'))
33             {
34                 if (oper2 == '*')
35                 {
36                     temp = num2 * num3;
37                     //printf("%.2lf %.2lf %.2lf\n", num2, num3, temp);
38                     q2.push_front(temp);
39                     q2.push_front(num1);
40                     q1.push_front(oper1);
41 
42                 }
43                 else if (oper2 == '/')
44                 {
45                     temp = num2 / num3;
46                     //printf("%.2lf %.2lf %.2lf\n", num2, num3, temp);
47                     q2.push_front(temp);
48                     q2.push_front(num1);
49                     q1.push_front(oper1);
50 
51                 }
52             }
53             else
54             {
55                 if (oper1 == '+') temp = num1 + num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
56                 else if (oper1 == '-') temp = num1 - num2,q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
57                 else if (oper1 == '*') temp = num1 * num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
58                 else temp = num1 / num2, q2.push_front(num3), q2.push_front(temp), q1.push_front(oper2);
59             }
60 
61         }
62         double ans;
63         char oper = q1.front(); q1.pop_front();
64         double num1 = q2.front(); q2.pop_front();
65         double num2 = q2.front(); q2.pop_front();
66         //printf("%.2lf\n", num1);printf("%.2lf\n", num2);
67         if (oper == '+ ' ) Ans = num1 + num2;
68          else  if (oper == ' - ' ) ans = num1 - num2;
69          else  if (oper == ' * ' ) ans = num1 * num2;
70          Else years = num1 / num2;
71          printf ( " % .2lf \ n " , year);
72  
73      }
 74      return  0 ;
75 }
View Code

 

Guess you like

Origin www.cnblogs.com/Vikyanite/p/11370532.html