View Code of Problem 3855

#include<stdio.h>
#include<math.h>
#include<string.h>
#define inf 0x7fffffff
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		char s[1000];
		scanf("%s",s);
		int sum=0;
		int b;//做运算的数
		int flag=1;//   +/-
		int i;
		for(i=0;i<strlen(s);)
		{
			if(s[i]=='+'||s[i]=='-')
			{
				if(s[i]=='+')
				{
					flag=1;
				}else{
						flag=-1;
					}
				i++;
				b=0;
				while(s[i]>='0'&&s[i]<='9')
				{
					b=b*10+s[i]-'0';
					i++;
				}
				sum+=b*flag;
			}
			else{//第一个数 
					b=0;
				while(s[i]>='0'&&s[i]<='9')
				{
					b=b*10+s[i]-'0';
					i++;
				}
				sum+=b;
			}	
			
		} printf("%d\n",sum);

		
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3855