牛客 计算表达式【非栈的方式】

对于一个不存在括号的表达式进行计算

输入描述:

存在多种数据,每组数据一行,表达式不存在空格

输出描述:

输出结果

示例1

输入

复制

6/2+3+3*4

输出

复制

18

#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
using namespace std;
int main(){
    double t;
    double a[1001];
    while(scanf("%lf",&t)!=EOF)
    {
        double sum=0;
        char ch;
        int i=0;
        a[0]=t;
        while(scanf("%c",&ch)!=EOF&&ch!='\n')
        {
            double t2;
            scanf("%lf",&t2);
            if(ch=='+')
                a[++i]=t2;
            else if(ch=='-')
                a[++i]=-t2;
            else if(ch=='*')
                a[i]*=t2;
            else if(ch=='/')
                a[i]/=t2;
        }
        for(int j=0;j<=i;j++)
            sum+=a[j];
        printf("%.0lf",sum);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值