View Code of Problem 3855

#include<stdio.h>
#include<math.h>
#include<string.h>
#include <iostream>
#include<algorithm>
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=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