B - Inglish-Number Translator

B - Inglish-Number Translator
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

In this problem, you will be given one or more integers in English. Your task is to translate these numbers into their integer representation. The numbers can range from negative 999,999,999 to positive 999,999,999. The following is an exhaustive list of English words that your program must account for: 
negative, zero, one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand, million 

Input

The input consists of several instances. Notes on input: 
  1. Negative numbers will be preceded by the word negative. 
  2. The word "hundred" is not used when "thousand" could be. For example, 1500 is written "one thousand five hundred", not "fifteen hundred".

The input is terminated by an empty line.

Output

The answers are expected to be on separate lines with a newline after each.

Sample Input

six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

Sample Output

6
-729
1000101
814022

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
	char word[32][50]={"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve",
		"thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","thirty","forty","fifty",
		"sixty","seventy","eighty","ninety","hundred","thousand","million","negative"};
	char s[50];
	int num=0;
	int i,j;
	char c;
	while(scanf("%s",s)!=EOF)
	{
		for(j=0;j<32;j++)
			if(strcmp(s,word[j])==0)
				break;
			if(j<=20)
				num+=j;
			if(j>20&&j<=27)
				num+=((j-18)*10);
			if(j>27&&j<=30)
			{
				switch(j)
				{
				case 28:num=num%100*100+(num/100)*100;break;
				case 29:num=num%1000*1000+(num/1000)*1000;break;
				case 30:num=num%1000000*1000000+(num/1000000)*1000000;break;
				}
			}
			if(j==31)
				cout<<'-';

			c=getchar();
			if(c=='\n')
			{
				cout<<num<<endl;
				num=0;
			}

	}
	return 0;
}



你可能感兴趣的:(B - Inglish-Number Translator)