View Code of Problem 3855

#include<iostream>
#include<string>

using namespace std;

int main()
{
   int t,temp;
   cin >> t;
   string str;
   while(t--){
        cin >> str;
        int flag = 1;
        int sum = 0;
        for(int i = 0; i < str.size(); i++){
             temp = 0;
            if(str[i] == '-'){
                flag = -1;
            }
            else if(str[i] == '+'){
                flag = 1;
            }
            else {
                while(str[i] >= '0' &&str[i] <= '9'){
                    temp = temp * 10 + str[i] - '0';
                    i++;
                }
                sum += temp * flag;
                i--;  // 不减的话会少判断符号
            }
        }
        cout << sum <<endl;
   }
   return 0;
}

Double click to view unformatted code.


Back to problem 3855