1026: a+b问题

使用scanf和cin可以保证遇到空格或者换行符就读入下一个变量

#include <bits/stdc++.h>
using namespace std;

unordered_map<string, int> mp; 
int Process(){
	string s1,s2;
	int num;
	cin>>s1 >> s2;  //使用scanf和cin可以保证遇到空格或者换行符就读入下一个变量
	if(s2[0] == '+' || s2[0] == '=')
		num = mp[s1];
	else{
		num =mp[s1]*10+mp[s2];
		cin>>s1;  //要记得还没读入的符号 
	} 
	return num;
	
}

int main(){
	int A,B;
	mp["zero"] = 0; mp["one"] = 1; mp["two"] = 2; mp["three"] = 3; mp["four"] = 4; mp["five"] = 5; mp["six"] = 6;
	mp["seven"]= 7; mp["eight"] = 8; mp["nine"] = 9; 
	
	while(1){
		A = Process();
		B = Process();
		if( A + B ==0) break;
		printf("%d\n",A+B);
	}
	return 0;	
}
posted @ 2026-02-24 23:49  peter_shen  阅读(9)  评论(0)    收藏  举报