【问题描述】
输入一个非负整数表达式字符串,形式为 “非负整数”+“运算符”+“非负整数”+“运算符”+“非负整数”.... 。其中运算符只可能是 加号:+,或者减号:-。
【输入形式】
一个长度不超过100的表达式字符串,只含0-9数字字符以及不定个数的加号和减号,已知该表达式不会出现直接相邻的两个运算符,对应每个非负整数以及计算结果也不会溢出
【输出形式】
该表达式最终的求值结果
【样例输入】
123-123+123-123
【样例输出】
0
【样例输入】
0-496
【样例输出】
-496
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
string s;
char m[10000],t_s='+';
int temp=0,cnt=0;
cin>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]<='9'&&s[i]>='0') temp++,m[temp]=s[i];
else
{
int temp_num=0;
for(int j=1;j<=temp;j++)
{
temp_num+=(m[j]-'0')*pow(10,temp-j);
}
if(t_s=='+') cnt+=temp_num;
else cnt-=temp_num;
t_s=s[i];
temp=0;
}
if(i==s.length()-1)
{
int temp_num=0;
for(int j=1;j<=temp;j++)
{
temp_num+=(m[j]-'0')*pow(10,temp-j);
}
if(t_s=='+') cnt+=temp_num;
else cnt-=temp_num;
}
}
cout<<cnt;
}