题目链接:点击打开链接
1913: 小火山的计算能力
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 454 Solved: 113
Submit Status Web Board
Description
别人说小火山的计算能力不行,小火山很生气,于是他想证明自己,现在有一个表达式,他想计算出来。
Input
首先是一个t(1<=20)表示测试组数。然后一个表达式,表达式长度不超过200,只有加法和减法,并且保证第一个字符不会是运算符号,最终结果小于2^63-1。
Output
输出运算结果。
Sample Input
2
1+1
2+1-1
Sample Output
2
2
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
char str[222];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
long long i=0,a=0;
while(str[i]>='0'&&str[i]<='9') // 得到计算式子的第一个数
{
a=a*10+str[i]-'0';
i++;
}
char fuhao;
long long sum=a;
while(i<strlen(str))
{
fuhao=str[i]; // 获取计算符号
i++;
long long b=0;
while(str[i]>='0'&&str[i]<='9') // 依次得到计算式子的第 2 ~ n 个数
{
b=b*10+str[i]-'0';
i++;
}
if(fuhao=='+') sum+=b;
if(fuhao=='-') sum-=b;
}
printf("%lld\n",sum);
}
return 0;
}