View Code of Problem 3855

#include <stdio.h>
#include <string.h>
int main()
{
	char  str[100000];
	int t;
	scanf("%d",&t);
	//getchar();
	while(t--)
	{
		scanf("%s",str);
		int result=0,len=strlen(str);
		for(int i=0;i<len;)
		{
			if(str[i]>='0'&&str[i]<='9')//数字
			{
				int b=0;
				while((str[i]>='0'&&str[i]<='9')&&(i<len))
				{
					b=b*10+str[i]-'0';
					++i;
				}
				result=result+b;
			}
			else//非数字,即为+or-
			{
				int s;
				if(str[i]=='-')
					s=-1;
				else//str[i]=='+'
					s=1;
					
				++i;
				int b=0;
				while((str[i]>='0'&&str[i]<='9')&&(i<len))
                                {
                                        b=b*10+str[i]-'0';
                                        ++i;
                                }
				result=result+b*s;			
			}
			
		}
		printf("%d\n",result);
	}

	
}

Double click to view unformatted code.


Back to problem 3855