将两个数字转换成单词

时间:2018-10-28 12:08:58

标签: c++ if-statement numbers word

我用c ++编写了一个程序,可以将数字转换成单词。 0-19的输出很好,但是20-99之间的数字输出错误。

//This program converts numbers from 0-99 into words
#include<iostream>
using namespace std;
int main()
{
    int number,unit,ten;
    cout<<"Please enter any number between 0-99: ";
    cin >>number;
    ten=number/10;
    unit=number%10;
    if(number<0 | number>99)
        cout<<"Number is out of range";
    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(unit==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";
        if(ten==10) cout<<"Ten";
        if(ten==20) cout<<"twenty";
        if(ten==30) cout<<"thirty";
        if(ten==40) cout<<"fourty";
        if(ten==50) cout<<"fifty";
        if(ten==60) cout<<"sixty";
        if(ten==70) cout<<"seventy";
        if(ten==80) cout<<"eighty";
        if(ten==90) cout<<"ninty";
    }               
}   

输出

Please enter any number between 0-99: 25
five

2 个答案:

答案 0 :(得分:1)

实际上,您的程序运行正常。对于输入25,它应该给出5,因为您正在执行unit = number%10,它会从您自己的代码中直接返回5if(unit==5) cout<<"five";

如果您想获得twenty five作为回报,则应更改代码。

与其直接匹配,不如将数字存储在map中并检查其值。另一种方法是将它们存储在两个不同的列表中,例如:d1 = ['zero','one' ...]d10 = ['ten', 'twenty', 'thirty', ...]。这样,您将不会重复任何操作。此外,您可以检查输入的长度,并知道它是一个十进制数还是两个十进制数,如果是两个十进制数,则可以使用number/10获取第一个,并使用number%10获取第二个并连接结果。 让我们以25为例,您将在2列表中搜索d10,并在5列表中搜索d1,最后,您的搜索结果为'二十'+ “五个”

n1 = number/10
n2 = number%10
for i in d10:
    if i == n1:
        for j in d1:
            if j == n2:
                print(i+' ' +j)
            else:
               break
     else:
         break

注意:代码在 python 3.x

答案 1 :(得分:0)

只有两个地方需要做一些小改动,您将获得想要的结果

首先,当您将十部分分开时,将其乘以10朝后,以在条件为10的倍数(即10,20,。)的情况下检查10的倍数。 然后只需将所有十个位置比较的位置更改为高于所有其他人的数字比较

我复制了您的代码并进行了更改, 我在下面评论了您需要更改的地方

    //This program converts numbers from 0-99 into words
    #include<iostream>
    using namespace std;
    int main()
    {   
        int number,unit,ten;
        cout<<"Please enter any number between 0-99: ";
        cin >>number;
        ten=number/10;
        unit=number%10;
        ten=ten*10;// multiply again it make it power of 10 so that you can check for if for multiple of 10
        if(number<0 | number>99)
            cout<<"Number is out of range";
        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(ten==10) cout<<"Ten";
            if(ten==20) cout<<"twenty";
            if(ten==30) cout<<"thirty";
            if(ten==40) cout<<"fourty";
            if(ten==50) cout<<"fifty";
            if(ten==60) cout<<"sixty";
            if(ten==70) cout<<"seventy";
            if(ten==80) cout<<"eighty";
            if(ten==90) cout<<"ninty";
            //all ten position comparisons has been shifted before all one's position comparisons
            if(unit==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";    }               
    }