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);
	}
 } ```

猜你喜欢

转载自blog.csdn.net/m0_46381590/article/details/112941710
Map
今日推荐