hdu 1228 A+B 字符串处理 超级大水题

中文意思不解释。

很水,我本来想用switch处理字符串,然后编译不通过。。。原来switch只能处理整数型的啊,我都忘了。

然后就有了很挫的一大串if代码了。。。

代码:

#include <iostream>
#include <string>
using namespace std;

int digit(string str) {
	if (str == "zero")
		return 0;
	if (str == "one")
		return 1;
	if (str == "two")
		return 2;
	if (str == "three")
		return 3;
	if (str == "four")
		return 4;
	if (str == "five")
		return 5;
	if (str == "six")
		return 6;
	if (str == "seven")
		return 7;
	if (str == "eight")
		return 8;
	if (str == "nine")
		return 9;
	return -1;
}

int getval() {
	string tmp;
	int s = 0;
	while (cin >> tmp && tmp != "+" && tmp != "=") {
		s = s * 10 + digit(tmp);
//		cout << tmp << endl;
	}
	return s;
}

int main() {
	int p1, p2;
	while (1) {
		p1 = getval();
		p2 = getval();
//		cout << p1 << " + " << p2 << " = "<< endl;
		if (p1 == 0 && p2 == 0)
			break;
		cout << p1 + p2 << endl;
	}//while
	return 0; 
}


原文地址:https://www.cnblogs.com/java20130723/p/3212231.html