gpt4 book ai didi

c++ - 将数字打印成文字

转载 作者:行者123 更新时间:2023-11-28 01:27:43 27 4
gpt4 key购买 nike

我编写了一个程序,将 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

最佳答案

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

number=number%10;

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

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

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

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";
}

关于c++ - 将数字打印成文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53067362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com