将数字转换成单词

时间:2018-10-29 10:22:17

标签: c++ numbers word

我是一个初学者,我用c ++编写了一个程序,可以将0-99999之间的数字转换成单词。对于最大为100的数字,它工作正常,但之后输出错误。我知道有一些严重的逻辑错误,但我无法弄清楚。

//This program converts number into words between 0-99999
#include<iostream>
using namespace std;
main()
{
    long int number,unit,ten,hundred,thousand,ten_thousand;
    cout<<"Please enter any number between 0-99999: ";
    cin>>number;
    ten_thousand=number/10000;
    thousand=number/1000;
    hundred=number/100;
    ten=number/10;
    unit=number%10;

    if(number<0 || number>99999)
    {
        cout<<"Number is out of range"; return 0;
    }
    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(thousand>=1 && thousand <=9)
        {
        if(thousand==1) cout<<"one thousand";
        if(thousand==2) cout<<"one thousand";
        if(thousand==3) cout<<"one thousand";
        if(thousand==4) cout<<"one thousand";
        if(thousand==5) cout<<"one thousand";
        if(thousand==6) cout<<"one thousand";
        if(thousand==7) cout<<"one thousand";
        if(thousand==8) cout<<"one thousand";
        if(thousand==9) cout<<"one thousand";
        }
    if(ten_thousand >=1 && ten_thousand <=9)
        {
        if(ten_thousand==1) cout<<"one thousand";
        if(ten_thousand==2) cout<<"two thousand";
        if(ten_thousand==3) cout<<"three thousand";
        if(ten_thousand==4) cout<<"four thousand";
        if(ten_thousand==5) cout<<"five thousand";
        if(ten_thousand==6) cout<<"six thousand";
        if(ten_thousand==7) cout<<"seven thousand";
        if(ten_thousand==8) cout<<"eight thousand";
        if(ten_thousand==9) cout<<"nine thousand";
        }
    if(ten == 1)
    {
        if(number==10) cout<<"ten"; 
        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(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==0 && ten ==0) cout<<" zero";
        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";
    }
}

输出1 :-(正确)

Please enter any number between 0-99999: 85
eighty five

输出2 :-(错误)

Please enter any number between 0-99999: 254
two hundred four

输出3 :-(错误)

Please enter any number between 0-99999: 98541
nine thousand one

4 个答案:

答案 0 :(得分:3)

当您为thousands选择单位时,您之前不会删除该单位。您忘记了模数:

thousand=(number % 10000)/1000;

除了第一个和最后一个单元外,所有单元都一样。这就是为什么您只显示第一个非零数字,然后显示最后一个数字。

答案 1 :(得分:1)

假设您要使用 945 运行代码。

第一个计算是

  

945/100 = 9

如果执行 number / 10 ,则会导致:

  

945/10 = 94

这就是为什么您的代码不起作用的原因。每次划分“数字”时,都必须对其进行缩放。

number = 945
hund = 945/100 = 9
number = number - hund*100 = 45
ten = number/10 = 45/10 ( 4 )
number = number - ten*10 = 5
units = number/1 = 5

答案 2 :(得分:1)

您的提取逻辑不正确,因为任何好的调试器都会向您显示。

考虑某种形式的

unit = number % 10;
number /= 10; // remove the least significant digit by integer division
ten = number % 10;
number /= 10;
hundred = number % 10;
number /= 10;

,依此类推。这种方法很好,因为可以稍后使用unittenhundred,&c将其转换为循环。最终成为数组的元素。

也不要忘记提取任何负号。

答案 3 :(得分:0)

尝试一下:

ten_thousand = number/10000;
number = number%10000;

thousand = number/1000;
number = number%1000;

hundred = number/100;
number = number%100;

ten = number/10;
number = number%10;

unit = number;