题意
给出一个逗号表达式的值,要求求出该表达式的值
题解
对于逗号表达式来说,只有最后一个整数是有用的.
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
int ans,cnt = 0;
cin>>str;
int len = str.length(),i=0,f=1;
while(i < len) {
if(str[i] == '-') f = -1,i++;
else if(isdigit(str[i])) {
int temp = 0;
while(i < len && isdigit(str[i])) {
temp = temp*10 + str[i]-'0';
i++;
}
temp = temp * f;
ans = temp;
}
else f = 1,i++;
}
printf("%d\n",ans);
}