hdoj 1237 模拟

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

  

#include "cstdio"
#include "algorithm"
#include "cstring"
double  cun[100];
int main()
{
    double  a,sum;
    char fh,e;//e为空格,fh为运算符号。
    int k;
    while (1){
        sum=0;
        k=0;
        scanf("%lf",&a);
        e=getchar();
        if(a==0&&e=='\n'){
            break;
        }
        cun[k++]=a;
        fh=getchar();
        e=getchar();
        while(scanf("%lf",&a)==1){
            if(fh=='*'){//符号为乘除时直接运算
                cun[k-1]*=a;
            }
            else if(fh=='/'){
                cun[k-1]/=a;
            }
            else if(fh=='+'){//符号为加或者减时将数字存下
                cun[k++]=a;
            }
            else if(fh=='-'){
                cun[k++]=-a;
            }
            e=getchar();
            if(e=='\n'){
                for(int i=0;i<k;i++){
//                    printf("%.2f\n",cun[i]);
                    sum+=cun[i];
                }
                printf("%.2f\n",sum);
                break;
            }
            fh=getchar();
            e=getchar();

        }
    }
    return  0;
}
//稍微需要一点点点编码能力...
posted @ 2017-01-29 14:39  thges  阅读(125)  评论(0编辑  收藏  举报