map应用:Inglish-Number Translator(map做法)

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:
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.
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

要想看懂这个代码先看这个吧
链接: Inglish-Number Translato-一般做法.

#include<stdio.h>
#include<string.h>
#include <iostream>
#include<map>
using namespace std;
map<string, int>m;
void f()//初始化函数f
{
	m["negative"] = -1;
	m["zero"] = 0;
	m["one"] = 1;
	m["two"] = 2;
	m["three"] = 3;
	m["four"] = 4;
	m["five"] = 5;
	m["six"] = 6;
	m["seven"] = 7;
	m["eight"] = 8;
	m["nine"] = 9;
	m["ten"] = 10;
	m["eleven"] = 11;
	m["twelve"] = 12;
	m["thirteen"] = 13;
	m["fourteen"] = 14;
	m["fifteen"] = 15;
	m["sixteen"] = 16;
	m["seventeen"] = 17;
	m["eighteen"] = 18;
	m["nineteen"] = 19;
	m["twenty"] = 20;
	m["thirty"] = 30;
	m["forty"] = 40;
	m["fifty"] = 50;
	m["sixty"] = 60;
	m["seventy"] = 70;
	m["eighty"] = 80;
	m["ninety"] = 90;
	m["hundred"] = 100;
	m["thousand"] = 1000;
	m["million"] = 1000000;
}
int main()
{
	int i,j,k,l;
	f();//调用f初始化m
	char a[10100];char b[100][100]; 
	while(gets(a))
	{
		memset(b,'\0',sizeof(b));
		l=strlen(a);int ss=0,sum=0,ff=1;
		if(l==0)
		break;
		for(i=0,j=0,k=0;i<l;i++)
		{
			if(a[i]==' ')
			j++,k=0;
			else
			b[j][k++]=a[i];
		}
		for(i=0;i<=j;i++)
		{
			if(m[b[i]]<100)
			{
				if(m[b[i]]==-1)
				ff=-1;
				else
				ss+=m[b[i]];
			}
			else
			{
				if(m[b[i]]==100)
				ss*=100;
				else
				sum+=ss*m[b[i]],ss=0;
			}
		} 
		printf("%d\n",(sum+ss)*ff);
	}
 } ```

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值