hdu 1237 简单计算器

用一个pre储存之前运算过的数,用prec处理temp之前的符号,用temp存储现在正在运算的数,注意一下细节就好。

http://acm.hdu.edu.cn/showproblem.php?pid=1237

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int a[205];
char b[205];

int main(void){
    //freopen("a.in","r",stdin);
    int i,lena;
    char tempc,prec;
    double pre,temp;
    while(~scanf("%d",a)){
        tempc = getchar();
        if(tempc == '\n'&&a[0]==0)break;
        for(i=1;tempc!='\n';i++){
            scanf("%c %d",b+i,a+i);
            tempc = getchar();
        }
        lena = i;
        pre = 0;
        prec = '+';
        temp = a[0];
        for(i=1;i<lena;i++){
            if(b[i]=='-'){
                if(prec == '+') pre = pre + temp;
                else pre = pre - temp;
                prec = '-'; temp = a[i];
            }
            else if(b[i]=='+'){
                if(prec == '+') pre = pre + temp;
                else pre = pre - temp;
                prec = '+'; temp = a[i];
            }
            else if(b[i]=='*'){
                temp = temp * a[i];
            }
            else {
                temp = temp / a[i];
            }
        }
        if(prec == '+')pre = pre + temp;
        else pre = pre - temp;
        printf("%.2lf\n",pre);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值