View Code of Problem 3855

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int main()
{
   int times;
   cin>>times;
   while(times--)
   {
       string str;
       cin>>str;
       int flag=0;
       int num=0,tem=0,sum=0;
       int qq=0;
       if(str[qq]>='0'&&str[qq]<='9')
       {
           for(qq=0;qq<str.length();qq++)
           {
               if(str[qq]>='0'&&str[qq]<='9')
                sum=sum*10+str[qq]-'0';
               else break;
           }
       }
       for(int i=qq;i<str.length();i++)
       {
           if(str[i]=='+')
            {
                flag=1;
            }
            else if(str[i]=='-')
           {
                 flag=-1;
           }
           tem=0;
           for(int j=i+1;j<str.length();j++)
           {
               if(str[j]>='0'&&str[j]<='9')
               {
                  tem=tem*10+str[j]-'0';
               }
               else {i=j-1;break;}
           }
          // cout<<tem<<endl;
          if(flag==1)
            sum+=tem;
          else sum-=tem;
          flag=0;
       }
       cout<<sum<<endl;
   }
   return 0;
}

Double click to view unformatted code.


Back to problem 3855