1. 程式人生 > >題目1101:計算表示式

題目1101:計算表示式

題目描述:

對於一個不存在括號的表示式進行計算

輸入:

存在多種資料,每組資料一行,表示式不存在空格

輸出:

輸出結果

樣例輸入:
6/2+3+3*4
樣例輸出:
18
#include<cstdio>
using namespace std;
 
int main()
{
    char ch;
    int i,j,temp,a[200];
    while(scanf("%d",&temp)!=EOF)
    {
        i=1;
        a[0]=0;
        a[1]=temp;
        while(scanf("%c",&ch)!=EOF && ch!='\n')
        {
            scanf("%d",&temp);
            if(ch=='-')a[++i]=-temp;
            else if(ch=='+')a[++i]=temp;
            else if(ch=='*')a[i]*=temp;
            else if(ch=='/')a[i]/=temp;
        }
        for(j=1;j<=i;++j)
            a[0]+=a[j];
        printf("%d\n",a[0]);
    }
    return 0;
}
/**************************************************************
    Problem: 1101
    User: Carvin
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1020 kb
****************************************************************/