Inglish-Number Translator

题目描述

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

输出

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

    本题的解题思路个人认为还是比较好想的。但还是有几个坑点,都是泪大哭

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[500];
char b[100][10];
int main()
{
    while(gets(a))
    {
        if (!strlen(a))   //  题目说的是输入以空行结束则要判断当空行时即使退出,不然wa。
            break;
        int i,j=0,k=0,flag=1,sum=0,ans=0;
        for (i=0;a[i]!='\0';i++)    // 我在这里的想法时把a[]中的单词分别装入b[][]中方便下面的比较。
        {
            if (a[i]!=32)
            b[j][k++]=a[i];
            else
            {
                b[j][k]='\0';
                j++;
                k=0;
            }
        }
        b[j][k]='\0';
        for (i=0;i<j+1;i++)
        {
            if (strcmp(b[i],"negative")==0)
                flag=-1;       //flag的设立方便了确定最后结果的正负。
            else if(strcmp(b[i],"zero")==0)
                sum=sum+0;
            else if(strcmp(b[i],"one")==0)
                sum=sum+1;
            else if(strcmp(b[i],"two")==0)
                sum=sum+2;
            else if(strcmp(b[i],"three")==0)
                sum=sum+3;
            else if(strcmp(b[i],"four")==0)
                sum=sum+4;
            else if(strcmp(b[i],"five")==0)
                sum=sum+5;
            else if(strcmp(b[i],"six")==0)
                sum=sum+6;
            else if(strcmp(b[i],"seven")==0)
                sum=sum+7;
            else if(strcmp(b[i],"eight")==0)
                sum=sum+8;
            else if(strcmp(b[i],"nine")==0)
                sum=sum+9;
            else if(strcmp(b[i],"ten")==0)
                sum=sum+10;
            else if(strcmp(b[i],"eleven")==0)
                sum=sum+11;
            else if(strcmp(b[i],"twelve")==0)
                sum=sum+12;
            else if(strcmp(b[i],"thirteen")==0)
                sum=sum+13;
            else if(strcmp(b[i],"fourteen")==0)
                sum=sum+14;
            else if(strcmp(b[i],"fifteen")==0)
                sum=sum+15;
            else if(strcmp(b[i],"sixteen")==0)
                sum=sum+16;
            else if(strcmp(b[i],"seventeen")==0)
                sum=sum+17;
            else if(strcmp(b[i],"eighteen")==0)
                sum=sum+18;
            else if(strcmp(b[i],"nineteen")==0)
                sum=sum+19;
            else if(strcmp(b[i],"twenty")==0)
                sum=sum+20;
            else if(strcmp(b[i],"thirty")==0)
                sum=sum+30;
            else if(strcmp(b[i],"forty")==0)
                sum=sum+40;
            else if(strcmp(b[i],"fifty")==0)
                sum=sum+50;
            else if(strcmp(b[i],"sixty")==0)
                sum=sum+60;
            else if(strcmp(b[i],"seventy")==0)
                sum=sum+70;
            else if(strcmp(b[i],"eighty")==0)
                sum=sum+80;
            else if(strcmp(b[i],"ninety")==0)
                sum=sum+90;
            else if(strcmp(b[i],"hundred")==0)
            {
                sum=sum*100;
            }
            else if(strcmp(b[i],"thousand")==0)   //以下部分也是本题的一个难点。因为sum只负责0-100的加和,则当出现千和更                                                    //大的数量级后就得先保存起来最后在加起来即可。  如果不明白可以手动的模                                                    //拟一下
            
            ans+=sum*1000; 
            sum=0; 
            
            else if(strcmp(b[i],"million")==0) 
           
            ans+=sum*1000000; 
            sum=0; 
           
        }
        cout<<(ans+sum)*flag<<endl;  //最后别忘了乘以flag
    }
    return 0;
}

加油吧,为了我的好几万的月薪。(自己写到这都老脸一红
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值