计算表达式

题目1101:计算表达式

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:1623

解决:434

题目描述:

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

输入:

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

输出:

输出结果

样例输入:
6/2+3+3*4
样例输出:
18
 
     
 
      
#include
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: 3011216016
     Language: C++
     Result: Accepted
     Time:0 ms
     Memory:1020 kb
****************************************************************/

你可能感兴趣的:(ACM,CPP)