【问题标题】:Printing number into words将数字打印成单词
【发布时间】:2018-10-30 15:07:56
【问题描述】:

我编写了一个程序来将 0-9999 之间的数字显示为单词。该程序运行正常,但是当我输入 11-19 和 415,612,819 之类的数字(基本上所有末尾包含 0-19 的数字)时,我遇到了意外的输出。我写的程序是用 c++ 编写的,我目前是 c++ 的初学者。

//This program converts number into words
#include<iostream>
using namespace std;
main()
{
    int number,unit,ten,hundred,thousand;
    cout<<"Please enter any number between 0-9999: ";
    cin>>number;
    thousand=number/1000;
    number=number%1000;
    hundred=number/100;
    number=number%100;
    ten=number/10;
    number=number%10;
    unit=number;
    if(number>=11 && number <=19)
     {
        if(number==11) cout<<"eleven";
        if(number==12) cout<<"twelve";  
        if(number==13) cout<<"thirteen";
        if(number==14) cout<<"fourteen";
        if(number==15) cout<<"fifteen";
        if(number==16) cout<<"sixteen";
        if(number==17) cout<<"seventeen";
        if(number==18) cout<<"eighteen";
        if(number==19) cout<<"ninteen";
     }
    else
    {    
    if(thousand>=1 && thousand <=9)
    {
        if(thousand==1) cout<<"one thousand";
        if(thousand==2) cout<<"two thousand";
        if(thousand==3) cout<<"three thousand";
        if(thousand==4) cout<<"four thousand";
        if(thousand==5) cout<<"five thousand";
        if(thousand==6) cout<<"six thousand";
        if(thousand==7) cout<<"seven thousand";
        if(thousand==8) cout<<"eight thousand";
        if(thousand==9) cout<<"nine thousand";
    }
    if(hundred>=1 && hundred <=9)
    {
        if(hundred==1) cout<<" one hundred";
        if(hundred==2) cout<<" two hundred";
        if(hundred==3) cout<<" three hundred";
        if(hundred==4) cout<<" four hundred";
        if(hundred==5) cout<<" five hundred";
        if(hundred==6) cout<<" six hundred";
        if(hundred==7) cout<<" seven hundred";
        if(hundred==8) cout<<" eight hundred";
        if(hundred==9) cout<<" nine hundred";
    }
    if(ten>=1 && ten <=9)
    {
        if(ten==1) cout<<" ten";
        if(ten==2) cout<<" twenty";
        if(ten==3) cout<<" thirty";
        if(ten==4) cout<<" fourty";
        if(ten==5) cout<<" fifty";
        if(ten==6) cout<<" sixty";
        if(ten==7) cout<<" seventy";
        if(ten==8) cout<<" eighty";
        if(ten==9) cout<<" ninty";
    }
    if(unit>=1 & unit <=9)
    {
        if(unit==1) cout<<" one";
        if(unit==2) cout<<" two";
        if(unit==3) cout<<" three";
        if(unit==4) cout<<" four";
        if(unit==5) cout<<" five";
        if(unit==6) cout<<" six";
        if(unit==7) cout<<" seven";
        if(unit==8) cout<<" eight";
        if(unit==9) cout<<" nine";
    }
    }
}

输出:-

Please enter any number between 0-9999: 14
 ten four

输出:-

Please enter any number between 0-9999: 114
 one hundred ten four

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:How to debug small programs
  • number=number%10; 不会产生大于 9 的数字,因此 if(number&gt;=11 &amp;&amp; number &lt;=19) 永远不会为真。
  • 这看起来与过去几天几个不同帐户发布的代码非常相似。如果这些是你的同学,也许是时候聚会了?
  • 我建议你们一起坐在一台电脑前学习如何使用调试器。从长远来看,这比一遍又一遍地问同样的问题对你更好。
  • 听起来不错。

标签: c++ numbers word


【解决方案1】:

在这里您将number 设置为介于09 之间,到目前为止一切顺利:

number=number%10;

但是您正在检查它是否在1119 之间。这是不可能的:

if(number>=11 && number <=19)

因此,整个块将被跳过,而是最后两个块匹配,给你“十四个”。第一个块可能应该是

if((ten==1) && (number>0))
{
    if(number==1) cout<<"eleven";
    if(number==2) cout<<"twelve";  
    if(number==3) cout<<"thirteen";
    if(number==4) cout<<"fourteen";
    if(number==5) cout<<"fifteen";
    if(number==6) cout<<"sixteen";
    if(number==7) cout<<"seventeen";
    if(number==8) cout<<"eighteen";
    if(number==9) cout<<"ninteen";
 }

【讨论】:

  • 它确实解决了我打印 11-19 数字的问题,但是像 114 这样的数字仍然给出错误的输出,比如“114”被转换为“14”
  • @Xacer 逐步完成您的程序。使用调试器或您的想法。在每一行之后写下number 的值以及每个if 的作用。你会很容易地找到为什么会出现这个问题。您(和您的同学?)在这里提出的每一个问题都很容易解决,这是编程的基本技能。
【解决方案2】:

我重写了您的整个代码,使其简短易懂。另外,我还包含了一个重用代码的功能。希望对你有帮助。

#include<iostream>
#include<string>

std::string cast_to_str(int, int);

int main(){
    int num;
    std::cout<<"Enter the number: ";
    std::cin >>num;
    if(num / 1000)
        std::cout<<cast_to_str(num / 1000, 1)<<" thousand ";
    if((num = num % 1000) / 100)
        std::cout<<cast_to_str(num / 100, 1)<<" hundred ";
    if((num = num % 100) == 10)
        std::cout<<"ten"<<std::endl;
    else if(num > 10){
        if(num > 10 && num <= 19)
            std::cout<<cast_to_str(num / 10, 2)<<std::endl;
        else{
            std::cout<<cast_to_str(num/10, 3);
            if(num % 10)
                std::cout<<cast_to_str(num%10, 1)<<std::endl;
        }
    }
    else if(num)
        std::cout<<cast_to_str(num, 1)<<std::endl;
    else
        std::cout<<std::endl;
    return 0;
}

std::string cast_to_str(int num, int which){
    switch(num){
        case 1: return which == 1 ? "one"   : "eleven";
        case 2: return which == 1 ? "two"   : (which == 2 ? "twelve"    : "twenty ") ;
        case 3: return which == 1 ? "three" : (which == 2 ? "thirteen"  : "thirty ") ;
        case 4: return which == 1 ? "four"  : (which == 2 ? "fourteen"  : "fourty ") ;
        case 5: return which == 1 ? "five"  : (which == 2 ? "fifteen"   : "fifty ")  ;
        case 6: return which == 1 ? "six"   : (which == 2 ? "sixteen"   : "sixty ")  ;
        case 7: return which == 1 ? "seven" : (which == 2 ? "seventeen" : "seventy ");
        case 8: return which == 1 ? "eight" : (which == 2 ? "eighteen"  : "eighty ") ;
        case 9: return which == 1 ? "nine"  : (which == 2 ? "nineteen"  : "ninety ") ;
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    • 2011-04-15
    • 1970-01-01
    相关资源
    最近更新 更多