题目描述:
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.
输入:
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.
输出:
对每个测试用例输出1行,即A+B的值.
样例输入:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
样例输出:
3
90
96
#include<iostream>
#include<string>
using namespace std;
int getNum(string str);
int main()
{
	while(true)
	{
		string str1;
		int a=0,b=0;
		int flag=1;
		while(cin>>str1)
		{
			if(str1=="+")
			{flag=0;continue;}
			if(str1=="=")
				break;
			if(flag)
			{
				a=a*10+getNum(str1);
			}
			else
			{
				b=b*10+getNum(str1);
			}
		}
		if(!(a||b))
			break;
		else
			cout<<a+b<<endl;
	}

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




 posted on 2014-04-29 18:52  mnmlist1  阅读(155)  评论(0编辑  收藏  举报