h0105 (10 分) c/c++

h0105 (10 分) c/c++_第1张图片

 

c++代码

#include 
#include 
using namespace std;

int main()
{
	string s;
	int cnt = 0;
	while (getline(cin, s))
	{
		int indexop = 0, indexop1 = 0;
		for (int i = 0; i < s.size(); i++) {
			if (s[i] == '+' || s[i] == '-')	indexop = i; // +或-的位置
			if (s[i] == '=') indexop1 = i; //=的位置
		}
		string sa, sb, sc;
		sa = s.substr(0, indexop);
		sb = s.substr(indexop + 1, indexop1 - indexop - 1);
		sc = s.substr(indexop1 + 1);
		//cout << sa << " " << sb << " " << sc << endl; //测试截取的是否正确
		int a = stoi(sa), b = stoi(sb);//找出a和b的整型数值
		if (s[indexop] == '-') b = -b;
		if (sc == "?") continue;
		else {
			int c = stoi(sc);
			if (a + b == c)
				cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}

 c语言

#include 
#include 
int main()
{
	int i, j, cnt = 0, a, b, s;
	char c1, ch[5];
	while (scanf("%d%c%d=%s", &a, &c1, &b, ch) != EOF)
	{
		if (strcmp(ch, "?") == 0) continue;
		s = 0;
		for (i = 0; ch[i]; i++) {
			s = s * 10 + (ch[i] - '0');
		}
		if (c1 == '-') b = -b;
		if (a + b == s)
			cnt++;
	}
	printf("%d\n", cnt);
	return 0;
}

你可能感兴趣的:(补题,蓝桥杯,职场和发展)