码迷,mamicode.com
首页 > 其他好文 > 详细

ZZULIoj 1913: 小火山的计算能力

时间:2016-08-10 20:54:52      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Description

别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来。

Input

首先是一个t(1<=20)表示测试组数。然后一个表达式,表达式长度不超过200,只有加法和减法,并且保证第一个字符不会是运算符号,最终结果小于2^63-1。

Output

输出运算结果。

Sample Input

2
1+1
2+1-1

Sample Output

2
2

 1 #include<cstdio>
 2 #include<string.h>
 3 int main()
 4 {
 5     int t;
 6     
 7     scanf("%d",&t);
 8     
 9     while(t--)
10     {
11         long long i=0,sum=0,j;
12         
13         char c,str[10010];
14         
15         scanf("%s",&str);
16         
17         int len=strlen(str);
18         
19         while(str[i]>=0 && str[i]<=9)
20         {
21             sum=sum*10+str[i]-0;
22             i++;
23         }
24         
25         for(j = i ; j < len ; )
26         {
27             if(str[j] == +)
28             {
29                 c=+;
30             }
31             else
32             {
33                 c=-;
34             }
35             j++;
36             
37             long long a=0;
38             
39             while(str[j]>=0 && str[j]<=9)
40             {
41                 a=a*10+str[j]-0;
42                 j++;
43             }
44             
45             if(c == +)
46             {
47                 sum+=a;
48             }
49             
50             else
51             {
52                 sum-=a;
53             }
54         }
55         printf("%lld\n",sum);
56     }
57 }

 

ZZULIoj 1913: 小火山的计算能力

标签:

原文地址:http://www.cnblogs.com/yexiaozi/p/5758289.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!