仅加减的字符串求值

#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{	
	string s;
	while (cin>>s) {
		int t=0;
		vector<int> ans;
		char fu='#';
		for (int i = 0; i <= s.size();i++) {
			if (s[i] >= '0' && s[i] <= '9') {
				t = t * 10 + s[i] - '0';
				//cout << t << ':';
			}
			else {
				if (ans.empty())ans.push_back(t);
				else {
					if (fu == '+')ans[0] = ans[0] + t;
					else ans[0] = ans[0] - t;
				}
				fu = s[i];
				t = 0;
			}
		}
		cout << ans[0] << endl;
	}
	return 0;
}
posted @ 2021-11-18 20:12  Zupernova  阅读(23)  评论(0编辑  收藏  举报