hdu 1228 A + B

Problem Description
读入两个小于100的正整数A和B,计算A+B.
需要注意的是:A和B的每一位数字由对应的英文单词给出.


Input
测试输入包含若干测试用例,每个测试用例占一行,格式为"A + B =",相邻两字符串有一个空格间隔.当A和B同时为0时输入结束,相应的结果不要输出.


Output
对每个测试用例输出1行,即A+B的值.


Sample Input
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =


Sample Output
3
90
96


代码:

#include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip> 
using namespace std;
int cmp(string st,int a)
{
	if(st=="zero") a=a*10+0;
	else if(st=="one") a=a*10+1;
	else if(st=="two") a=a*10+2;
	else if(st=="three") a=a*10+3;
	else if(st=="four") a=a*10+4;
	else if(st=="five") a=a*10+5;
	else if(st=="six") a=a*10+6;
	else if(st=="seven") a=a*10+7;
	else if(st=="eight") a=a*10+8;
	else if(st=="nine") a=a*10+9;
	return a;
}
int main()
{
	string st;
	int n,m,k;
	while(cin>>st)
	{   n=0;m=0;k=0;
		n=cmp(st,n);
		while(cin>>st)
		{
			if(st!="+"&&k==0)
			  n=cmp(st,n);
	        else if(st=="+")
	          k=1;
            else if(k==1&&st!="=")
              m=cmp(st,m);
            else if(st=="=") break;
		}
	    if(n==0&&m==0) break;
	    else cout<<n+m<<endl;
	}
	return 0;
}


posted @ 2015-11-16 13:10  (慎独)  阅读(122)  评论(0编辑  收藏  举报