• 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;
    }
    //稍微需要一点点点编码能力...
  • 相关阅读:
    php wamp 配置虚拟主机
    php xml 操作。
    第一个输入框中 输入 test,第二个输入框中自动出现 test.baibu.com
    正则表达式 手机号码的验证
    JQery 设置 退格键 不可用 jQery 中的keydown 事件
    YII 片段缓存如何实现。
    php set_time_limit() 函数
    VLAN
    进程
    802.11无线网络权威指南
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/6357487.html
Copyright © 2020-2023  润新知