zoukankan      html  css  js  c++  java
  • 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=='
    '){
                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=='
    '){
                    for(int i=0;i<k;i++){
    //                    printf("%.2f
    ",cun[i]);
                        sum+=cun[i];
                    }
                    printf("%.2f
    ",sum);
                    break;
                }
                fh=getchar();
                e=getchar();
    
            }
        }
        return  0;
    }
    //稍微需要一点点点编码能力...
  • 相关阅读:
    evernote100个做笔记的好方法
    平衡二叉树的调整模版
    晨间日记的奇迹
    hdu 2952 Counting Sheep
    hdu 1535 Invitation Cards
    poj 3259 Wormholes(spfa)
    poj 2263 Heavy Cargo(floyd)
    poj 3268 Silver Cow Party(SPFA)
    hdu 1690 Bus System
    hdu 3631 Shortest Path(Floyd)
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6357487.html
Copyright © 2011-2022 走看看