hud1237 简单计算器

C - 简单计算器

读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。 

Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。 
Output对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。 
Sample Input

1 + 2
4 + 2 * 5 - 7 / 11
0

Sample Output

3.00
13.36

输入对我来说是个难点 - -!,get char 判断只输入一个数的情况,把算出来的数放进数组,加减直接放在后一位,乘除在前一位的基础上进行运算,且存放在前一位的位置,这样就可以做到先乘除后加减。
 1 #include<iostream>
 2 using namespace std;
 3 #include<string.h>
 4 #include<stdio.h>
 5 #include<stack>
 6 #include<string.h>
 7 #include<string>
 8 #include<algorithm>
 9 #include<queue>
10 int main()
11 {
12     double a[210],s,c;
13     char b,d;
14     while(scanf("%lf",&s))
15     {
16         char TM;
17         TM=getchar();
18         if(TM=='\n')
19         {
20             if(!s)
21                 break;
22             else
23             {
24                 printf("%.2lf\n",s);
25                 continue;
26             }
27         }
28         a[0]=s;
29         int i=0;
30             while(scanf("%c %lf%c",&b,&c,&d))
31             {
32                 if(b=='+')
33                 {
34                     a[++i]=c;
35                 }
36                 else if(b=='-')
37                 {
38                     a[++i]=-1*c;
39                 }
40                 else if(b=='*')
41                 {
42                     a[i]*=c;
43                 }
44                 else if(b=='/')
45                 {
46                     a[i]/=c;
47                 }
48                 if(d!=' ')
49                     break;
50             }
51             double sum=0;
52             for(int k=0;k<=i;k++)
53                 sum+=a[k];
54             printf("%.2lf\n",sum);
55 
56     }
57     return 0;
58 }

 


posted @ 2017-07-20 11:19  小小超plus  阅读(293)  评论(0编辑  收藏  举报