题目1101 :计算表达式 (运算符操作)方法比较简洁

题目1101:计算表达式

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:5469

解决:1703

题目描述:

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

输入:

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

输出:

输出结果

样例输入:
6/2+3+3*4
样例输出:
18
#include <iostream>
#include<stdio.h>
using namespace std;
//6/2+3+3*4
int main()
{
   char ch;
   int i,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(int j=1;j<=i;j++)
       {
           a[0]+=a[j];
       }
       printf("%d\n",a[0]);
 
   }
    return 0;
}
 
/**************************************************************
    Problem: 1101
    User: zhuoyuezai
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1520 kb
****************************************************************/

 

posted @ 2016-07-24 08:20  多思考&&多动手  阅读(384)  评论(0编辑  收藏  举报