wenbao与模拟表达式计算

http://lx.lanqiao.cn/problem.page?gpid=T419

 

水水水,也就写了两三个小时

 

 

 

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 char str[109], b[109];
 5 int a[109], sum = 0, aa = 0, bb = 0, x, y;
 6 void xx(){ //处理乘除
 7     while(bb && (b[bb-1] == '*' || b[bb-1] == '/')){
 8         if(b[bb-1] == '*') a[aa-2] = a[aa-2]*a[aa-1];
 9         else a[aa-2] = a[aa-2]/a[aa-1];
10         aa--, bb--;
11     }
12 }
13 int main(){
14     scanf("%s", str);
15     int len = strlen(str);
16     bool flag = false;
17     for(int i = 0; i <= len; ++i){
18         if(i == len){
19             if(flag) a[aa++] = sum, xx();
20             break;
21         }
22         if(str[i] >= '0' && str[i] <= '9'){
23             flag = true, sum = sum*10+str[i]-'0';
24         }else{
25             if(flag) a[aa++] = sum, xx(), sum = 0;
26             if(str[i] == ')'){ //处理括号
27                 int j;
28                 for(j = 1; ; ++j) if(b[bb-j] == '(') break;
29                 for(int k = bb-j+1, t = 1; k < bb; ++k, ++t){
30                     if(b[k] == '+') a[aa-j] += a[aa-j+t];
31                     else a[aa-j] -= a[aa-j+t];
32                 }
33                 aa = aa-j+1, bb = bb-j, xx();
34             }else{
35                 b[bb++] = str[i];
36             }
37             flag = false;
38         }
39     }
40     for(int i = 0, k = 1; i < bb; ++i, k++){
41         if(b[i] == '+') a[0] += a[k];
42         else a[0] -= a[k];
43     }
44     printf("%d\n", a[0]);
45     return 0;
46 }

 

 ————————————————————————————————————————————————————————————————————————————————————————————————————————

 

类似的,只不过是求浮点数的,稍微改一改就过了,,,水水水

http://acm.nyist.net/JudgeOnline/problem.php?pid=35

 

 

 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 using namespace std;
 5 char str[1009], b[1009];
 6 double a[1009], sum;
 7 int aa, bb, num;
 8 void xx(){ //处理乘除
 9     while(bb && (b[bb-1] == '*' || b[bb-1] == '/')){
10         if(b[bb-1] == '*') a[aa-2] = a[aa-2]*1.0*a[aa-1];
11         else a[aa-2] = a[aa-2]*1.0/a[aa-1];
12         aa--, bb--;
13     }
14 }
15 int main(){
16     int t;
17     scanf("%d", &t);
18     while(t--){
19         aa = bb = num = 0;
20         sum = 0;
21         scanf("%s", str);
22         int len = strlen(str);
23         bool flag = false, flag2 = false;
24         for(int i = 0; i < len; ++i){
25             if(i == len-1){
26                 if(flag) a[aa++] = sum, xx();
27                 break;
28             }
29             if(str[i] == '*' || str[i] == '/' || str[i] == '+' || str[i] == '-' || str[i] == '(' || str[i] == ')'){
30                 if(flag) a[aa++] = sum, xx(), sum = 0, num = 0;
31                 if(str[i] == ')'){ //处理括号
32                     int j;
33                     for(j = 1; ; ++j) if(b[bb-j] == '(') break;
34                     for(int k = bb-j+1, t = 1; k < bb; ++k, ++t){
35                         if(b[k] == '+') a[aa-j] += a[aa-j+t];
36                         else a[aa-j] -= a[aa-j+t];
37                     }
38                     aa = aa-j+1, bb = bb-j, xx();
39                 }else{
40                     b[bb++] = str[i];
41                 }
42                 flag2 = flag = false;
43             }else{
44                 flag = true;
45                 if(str[i] == '.'){
46                     flag2 = true; continue;
47                 }
48                 if(flag2){
49                     double x = (str[i]-'0')*1.0;
50                     num++;
51                     int num2 = num;
52                     while(num2--){
53                         x/=10.0;
54                     }
55                     sum += x;
56                 }else{
57                     sum = sum*10.0+(str[i]-'0')*1.0;
58                 }
59             }
60         }
61         for(int i = 0, k = 1; i < bb; ++i, k++){
62             if(b[i] == '+') a[0] += a[k];
63             else a[0] -= a[k];
64         }
65         printf("%.2lf\n", a[0]);
66     }
67     return 0;
68 }

 

 

 

只有不断学习才能进步!

 

posted @ 2018-04-14 13:52  wenbao  阅读(103)  评论(0编辑  收藏  举报