poj 2121 Inglish-Number Translator

/*
English-Number Translator
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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

输入
The input consists of several instances. Notes on input: 
Negative numbers will be preceded by the word negative. 
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.
输出
The answers are expected to be on separate lines with a newline after each.
样例输入
six
negative seven hundred twenty nine
one million one hundred one
eight hundred fourteen thousand twenty two

样例输出
6
-729
1000101
814022
*/
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;

/*
长度为零时为空行 直接跳出 
*/
int get_num(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;
	if(str == "ten") return 10;
	if(str == "eleven") return 11;
	if(str == "twelve") return 12;
	if(str == "thirteen") return 13;
	if(str == "fourteen") return 14;
	if(str == "fifteen") return 15;
	if(str == "sixteen") return 16;
	if(str == "seventeen") return 17;
	if(str == "eighteen") return 18;
	if(str == "nineteen") return 19;
	if(str == "twenty") return 20;
	if(str == "thirty") return 30;
	if(str == "forty") return 40;
	if(str == "fifty") return 50;
	if(str == "sixty") return 60;
	if(str == "seventy") return 70;
	if(str == "eighty") return 80;
	if(str == "ninety") return 90;
	return 0;
}

int main()
{
	string str_temp;
	string str_substr;//子串 
	while (getline(cin, str_temp))
	{
		if(str_temp.length() == 0) break;//长度为零时为空行 直接跳出 
		int i_pcur = 0;//本次子串开始的位置 
		int i_sum = 0;//当前和 
		int i_toadd = 0;//当前应该加的数 
		bool is_negative = 0;
		for (int i = 0; i < str_temp.length(); ++i)
		{
			if (str_temp[i] == ' ' || i == str_temp.length() - 1)
			{
				if (i == str_temp.length() - 1) ++i;
				str_substr = str_temp.substr(i_pcur, i - i_pcur);
				//cout << str_substr << endl;
				i_pcur = i + 1;
				if (str_substr == "negative") is_negative = 1;
				else if(str_substr == "million")
				{
					i_sum = i_sum + i_toadd * 1000000;
					i_toadd = 0;
				}
				else if(str_substr == "thousand")
				{
					i_sum = i_sum + i_toadd * 1000;
					i_toadd = 0;
				}
				else if(str_substr == "hundred")//需要特别判断 注意样例eight hundred fourteen thousand twenty two 
				{
					i_toadd *= 100;
				}
				else
				{
					i_toadd += get_num(str_substr);
				}
			}
		}
		i_sum += i_toadd; 
		if (is_negative && i_sum != 0) cout << "-";//存不存在负零? 未测试 
		cout << i_sum << endl;
		
	}
	return 0;
}

你可能感兴趣的:(poj)