View Code of Problem 3855

#include<bits/stdc++.h>
using namespace std;
 
int main(){
	int T;
	cin>>T;
	while(T--){
		string s;
		cin>>s;
		int i;
		int sum=0;
		int flag=1;
		int b;
		for(i=0;i<s.length();i++){
			if(s[i]>='0'&&s[i]<='9'){
				b=0;
				while(s[i]>='0'&&s[i]<='9'){
					b=b*10+(s[i]-'0');
					i++;
				}
				sum+=flag*b;
			}
			if(s[i]=='+')
				flag=1;
			else if(s[i]=='-')
				flag=-1;	
		}
		cout<<sum<<endl;
	}
}

Double click to view unformatted code.


Back to problem 3855