View Code of Problem 3855

#include<stdio.h>
#include<string.h>
int main(void){
	int t;
	scanf("%d",&t);
	while(t--){
		char e[1000];
		int sum=0,flag,b;
		scanf("%s",e);
		for(int i=0;i<strlen(e);){
			flag=1;
			if(e[i]=='-'||e[i]=='+'){
				if(e[i]=='-') flag=-1;
				i++;
				b=0;
				while(e[i]>='0'&&e[i]<='9'){
					b=b*10+e[i]-'0';
					i++;
				}
				sum+=b*flag;
			}else{
				b=0;
				while(e[i]>='0'&&e[i]<='9'){
					b=b*10+e[i]-'0';
					i++;
				}
				sum+=b;
			}
		}
		printf("%d\n",sum);
	}
}

Double click to view unformatted code.


Back to problem 3855