Friday, April 24, 2015

UVa 11878 - Homework Checker

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

int main() {
	int correct_awnsers = 0;
	string line;
	getline(cin, line);
	while (line.size() > 0) {
		int n = line.length();
		int equal_pos = line.find("=");
		string left = line.substr(0, equal_pos);
		int plus_pos = line.find("+");
		int sign_pos = plus_pos == -1 ? line.find("-") : plus_pos;

		string a_st = left.substr(0, sign_pos);
		int a = atoi(a_st.c_str());
		string b_st = left.substr(sign_pos + 1, equal_pos - sign_pos - 1);
		int b = atoi(b_st.c_str());
		string c_st = line.substr(equal_pos + 1);
		if (c_st[0] != '?') {
			int c = atoi(c_st.c_str());

			int correct_c = plus_pos == -1 ? a - b : a + b;
			if (c == correct_c)
				correct_awnsers++;
		}
		getline(cin, line);
	}
	cout << correct_awnsers << endl;
	return 0;
}

No comments:

Post a Comment