机试题2019夏令营-4--表达式求值

机试题2019夏令营-4--表达式求值_第1张图片

#include
#include
int main()
{
    int n;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        double a[100],sum=0;
        char c;
        int i=0;
        scanf("%lf",&a[0]);
        c=getchar();
        while(c!='\n')
        {
            double temp;
            scanf("%lf",&temp);
            switch(c)
            {
                case '+':a[++i]=temp;break;
                case '-':a[++i]=-temp;break;
                case '*':a[i]*=temp;break;
                case '/':a[i]/=temp;break;
            }
            c=getchar();
        }
        for(int j=0;j<=i;j++)
        {
            sum=sum+a[j];
        }
        if(sum-int(sum)<=1e-6)
        {
            printf("%.0f\n",sum);
        }
        else
        {
            printf("%.1f\n",sum);
        }
    }
    return 0;
}

机试题2019夏令营-4--表达式求值_第2张图片

你可能感兴趣的:(机试题2019夏令营-4--表达式求值)