编写代码将给定的数字转换为单词(例如 1234 作为输入应该输出 1234)

Posted

技术标签:

【中文标题】编写代码将给定的数字转换为单词(例如 1234 作为输入应该输出 1234)【英文标题】:Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four) 【发布时间】:2012-08-30 08:52:05 【问题描述】:

编写 C/C++/Java 代码将给定的数字转换为单词。

例如:- 输入: 第1234章

输出: 一千二百三十四。

输入: 10

输出: 十

对于数字 0 到 10 是否需要完整的开关盒。

在从 14 到 19 的每个数字名称后添加“teen”(例如:14:四个“teen”。)。

然后为 20 到 99 范围内的数字添加“ty”和数字名称。

等等。

我认为必须有更好的方法来解决这个问题。

C 代码是首选。

【问题讨论】:

您需要在问题中提供更多详细信息:您要使用哪种语言来执行此操作?你都尝试了些什么?这是作业吗? 在微软的采访中被问到过。 阅读这篇文章以获得想法:accu.org/index.php/journals/1769 【参考方案1】:
#include<iostream>
using namespace std;
void expand(int);
int main()

    int num;
    cout<<"Enter a number : ";
    cin>>num;
    expand(num);

void expand(int value)

    const char * const ones[20] = "zero", "one", "two", "three","four","five","six","seven",
    "eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
    "eighteen","nineteen";
    const char * const tens[10] = "", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
    "eighty","ninety";

    if(value<0)
    
        cout<<"minus ";
        expand(-value);
    
    else if(value>=1000)
    
        expand(value/1000);
        cout<<" thousand";
        if(value % 1000)
        
            if(value % 1000 < 100)
            
                cout << " and";
            
            cout << " " ;
            expand(value % 1000);
        
    
    else if(value >= 100)
    
        expand(value / 100);
        cout<<" hundred";
        if(value % 100)
        
            cout << " and ";
            expand (value % 100);
        
    
    else if(value >= 20)
    
        cout << tens[value / 10];
        if(value % 10)
        
            cout << " ";
            expand(value % 10);
        
    
    else
    
        cout<<ones[value];
    
    return;

【讨论】:

@rgaut:作者忘记将"ten" 包含在他的ones[] 数组中。我更正了它,它现在似乎可以工作了。【参考方案2】:

考虑使用由小值索引的字符串表,而不是 switch 语句。

const char * const ones[20] = "zero", "one", "two", ..., "nineteen";
const char * const tens[10] = "", "ten", "twenty", ..., "ninety";

现在将问题分解成小块。编写一个可以输出一位数的函数。然后编写一个可以处理两位数的函数(可能会使用前面的函数)。根据需要继续构建功能。

创建具有预期输出的测试用例列表,并编写代码来调用您的函数并检查输出,这样,当您为更复杂的情况解决问题时,您可以确保更简单的情况继续工作。

【讨论】:

【参考方案3】:

如果您对现成的解决方案感兴趣,那么您可以查看 HumanizerCpp 库 (https://github.com/trodevel/HumanizerCpp) - 它是 C# Humanizer 库的一个端口,它完全符合您的要求。

它甚至可以转换为序数,目前支持 3 种语言:英语、德语和俄语。

例子:

const INumberToWordsConverter * e = Configurator::GetNumberToWordsConverter( "en" );

std::cout << e->Convert( 123 ) << std::endl;
std::cout << e->Convert( 1234 ) << std::endl;
std::cout << e->Convert( 12345 ) << std::endl;
std::cout << e->Convert( 123456 ) << std::endl;

std::cout << std::endl;
std::cout << e->ConvertToOrdinal( 1001 ) << std::endl;
std::cout << e->ConvertToOrdinal( 1021 ) << std::endl;


const INumberToWordsConverter * g = Configurator::GetNumberToWordsConverter( "de" );

std::cout << std::endl;
std::cout << g->Convert( 123456 ) << std::endl;

const INumberToWordsConverter * r = Configurator::GetNumberToWordsConverter( "ru" );

std::cout << r->ConvertToOrdinal( 1112 ) << std::endl;

输出:

one hundred and twenty-three
one thousand two hundred and thirty-four
twelve thousand three hundred and forty-five
one hundred and twenty-three thousand four hundred and fifty-six

thousand and first
thousand and twenty-first

einhundertdreiundzwanzigtausendvierhundertsechsundfünfzig
одна тысяча сто двенадцатый

在任何情况下,您都可以查看源代码并在项目中重用或尝试理解逻辑。它是用纯 C++ 编写的,没有外部库。

问候, 塞尔吉

【讨论】:

【参考方案4】:
/* This Program will convert Numbers from -999,999,999 to 999,999,999 into words */

#include <vector>
#include <iostream>
#include <stdexcept>
#include <string>

using namespace std;

const std::vector<std::string> first14 =  "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen" ;
const std::vector<std::string> prefixes =  "twen", "thir", "for", "fif", "six", "seven", "eigh", "nine" ;

std::string inttostr(const int number)

    if (number < 0)
    
        return "minus " + inttostr(-number);
    
    if (number <= 14)
        return first14.at(number);
    if (number < 20)
        return prefixes.at(number - 12) + "teen";
    if (number < 100) 
        unsigned int remainder = number - (static_cast<int>(number / 10) * 10);
        return prefixes.at(number / 10 - 2) + (0 != remainder ? "ty " + inttostr(remainder) : "ty");
    
    if (number < 1000) 
        unsigned int remainder = number - (static_cast<int>(number / 100) * 100);
        return first14.at(number / 100) + (0 != remainder ? " hundred " + inttostr(remainder) : " hundred");
    
    if (number < 1000000) 
        unsigned int thousands = static_cast<int>(number / 1000);
        unsigned int remainder = number - (thousands * 1000);
        return inttostr(thousands) + (0 != remainder ? " thousand " + inttostr(remainder) : " thousand");
    
    if (number < 1000000000) 
        unsigned int millions = static_cast<int>(number / 1000000);
        unsigned int remainder = number - (millions * 1000000);
        return inttostr(millions) + (0 != remainder ? " million " + inttostr(remainder) : " million");
    
    throw std::out_of_range("inttostr() value too large");


int main()

    int num;
    cout << "Enter a number to convert it into letters : ";
    cin >> num;
    cout << endl << num << " = " << inttostr(num) << endl;
    system("pause");
    return 0;

【讨论】:

您好 IGI30,欢迎来到 Stack Overflow。不鼓励仅代码问题。请提供更多信息。【参考方案5】:

适用于从 0999999999 的任何数字。

这个程序从用户那里得到一个数字,把它分成三部分,分别存储在一个数组中。这三个数字通过将它们转换为单词的函数传递。 然后它在第一部分加上“百万”,在第二部分加上“千”。

#include <iostream>
using namespace std;
int buffer = 0, partFunc[3] = 0, 0, 0, part[3] = 0, 0, 0, a, b, c, d;
long input, nFake = 0;
const char ones[][20] = "",       "one",       "two",      "three",
                         "four",    "five",      "six",      "seven",
                         "eight",   "nine",      "ten",      "eleven",
                         "twelve",  "thirteen",  "fourteen", "fifteen",
                         "sixteen", "seventeen", "eighteen", "nineteen";
const char tens[][20] = "",     "ten",   "twenty",  "thirty", "forty",
                         "fifty", "sixty", "seventy", "eighty", "ninety";
void convert(int funcVar);
int main() 
  cout << "Enter the number:";
  cin >> input;
  nFake = input;
  buffer = 0;
  while (nFake) 
    part[buffer] = nFake % 1000;
    nFake /= 1000;
    buffer++;
  
  if (buffer == 0) 
    cout << "Zero.";
   else if (buffer == 1) 
    convert(part[0]);
   else if (buffer == 2) 
    convert(part[1]);
    cout << " thousand,";
    convert(part[0]);
   else 
    convert(part[2]);
    cout << " million,";

    if (part[1]) 
      convert(part[1]);
      cout << " thousand,";
     else 
      cout << "";
    
    convert(part[0]);
  
  system("pause");
  return (0);


void convert(int funcVar) 
  buffer = 0;
  if (funcVar >= 100) 
    a = funcVar / 100;
    b = funcVar % 100;
    if (b)
      cout << " " << ones[a] << " hundred and";
    else
      cout << " " << ones[a] << " hundred ";
    if (b < 20)
      cout << " " << ones[b];
    else 
      c = b / 10;
      cout << " " << tens[c];
      d = b % 10;
      cout << " " << ones[d];
    
   else 
    b = funcVar;
    if (b < 20)
      cout << ones[b];
    else 
      c = b / 10;
      cout << tens[c];
      d = b % 10;
      cout << " " << ones[d];
    
  

【讨论】:

【参考方案6】:
#include<stdio.h>
#include<conio.h>
void main()

int len=0,revnum,i,dup=0,j=0,k=0;

 long int gvalue;

 char  ones[]  [10]="one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","";

 char twos[][10]="Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety","";

clrscr();


 printf("\n Enter value"); 

 scanf("%ld",&gvalue);
 if(gvalue==10)

   printf("Ten");

 else if(gvalue==100)

   printf("Hundred");

 else if(gvalue==1000)

   printf("Thousand");

 dup=gvalue;

 for(i=0;dup>0;i++)
  
   revnum=revnum*10+dup%10;

   len++;

   dup=dup/10;

  

 while(j<len)

 

 if(gvalue<10)

  

   printf("%s ",ones[gvalue-1]);

   

 else if(gvalue>10&&gvalue<=19)

  

   printf("%s ",ones[gvalue-2]);

   break;

  

 else if(gvalue>19&&gvalue<100)

  

   k=gvalue/10;

   gvalue=gvalue%10;

    printf("%s ",twos[k-1]);

   

  else if(gvalue>100&&gvalue<1000)

   

    k=gvalue/100;

    gvalue=gvalue%100;

    printf("%s Hundred ",ones[k-1]);

     

   else if(gvalue>=1000&&gvlaue<9999)

   

    k=gvalue/1000;

    gvalue=gvalue%1000;

    printf("%s Thousand ",ones[k-1]);

    

   else if(gvalue>=11000&&gvalue<=19000)

    

    k=gvalue/1000;

    gvalue=gvalue%1000;

    printf("%s Thousand ",twos[k-2]);

    

   else if(gvalue>=12000&&gvalue<100000)

   

    k=gvalue/10000;

    gvalue=gvalue%10000;

    printf("%s ",ones[gvalue-1]);

   

else



printf("");



 j++;

 getch();


【讨论】:

char ones[] [10]: 可能是 20 而不是 10?【参考方案7】:
import java.lang.*;
import java.io.*;
public class rupee

public static void main(String[] args)throws  IOException


int len=0,revnum=0,i,dup=0,j=0,k=0;
int gvalue;
 String[] ones="one","Two","Three","Four","Five","Six","Seven","Eight","Nine","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen","";
String[] twos="Ten","Twenty","Thirty","Fourty","fifty","Sixty","Seventy","eighty","Ninety","";
System.out.println("\n Enter value");
InputStreamReader b=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(b);
gvalue=Integer.parseInt(br.readLine());
if(gvalue==10)

   System.out.println("Ten");

else if(gvalue==100)

  System.out.println("Hundred");

else if(gvalue==1000)
  System.out.println("Thousand");

dup=gvalue;
for(i=0;dup>0;i++)

  revnum=revnum*10+dup%10;
  len++;
  dup=dup/10;

while(j<len)

 if(gvalue<10)
 
  System.out.println(ones[gvalue-1]);
 
else if(gvalue>10&&gvalue<=19)
 
  System.out.println(ones[gvalue-2]);
  break;
 
else if(gvalue>19&&gvalue<100)
 
   k=gvalue/10;
   gvalue=gvalue%10;
   System.out.println(twos[k-1]);
 
else if(gvalue>100&&gvalue<1000)
 
   k=gvalue/100;
   gvalue=gvalue%100;
   System.out.println(ones[k-1] +"Hundred");
 
else if(gvalue>=1000&&gvalue<9999)
 
   k=gvalue/1000;
   gvalue=gvalue%1000;
   System.out.println(ones[k-1]+"Thousand");

else if(gvalue>=11000&&gvalue<=19000)
 
  k=gvalue/1000;
  gvalue=gvalue%1000;
  System.out.println(twos[k-2]+"Thousand");
       
else if(gvalue>=12000&&gvalue<100000)

 k=gvalue/10000;
     gvalue=gvalue%10000;
 System.out.println(ones[gvalue-1]);

else

 System.out.println("");
    
j++;



【讨论】:

@kowsalyajaganathan 兄弟语言错误。【参考方案8】:
/*Maximum value that can be entered is 2,147,483,647
 * Program to convert entered number into string
 * */
import java.util.Scanner;

public class NumberToWords 


public static void main(String[] args) 

    double num;//for taking input number
    Scanner obj=new Scanner(System.in);
    do
    
        System.out.println("\n\nEnter the Number (Maximum value that can be entered is 2,147,483,647)");
        num=obj.nextDouble();
        if(num<=2147483647)//checking if entered number exceeds maximum integer value
        
            int number=(int)num;//type casting double number to integer number
            splitNumber(number);//calling splitNumber-it will split complete number in pairs of 3 digits
        
        else
            System.out.println("Enter smaller value");//asking user to enter a smaller value compared to 2,147,483,647
    while(num>2147483647);

//function to split complete number into pair of 3 digits each
public static void splitNumber(int number)
   //splitNumber array-contains the numbers in pair of 3 digits
    int splitNumber[]=new int[4],temp=number,i=0,index;
    //splitting number into pair of 3
    if(temp==0)
        System.out.println("zero");
    while(temp!=0)
    
        splitNumber[i++]=temp%1000;
        temp/=1000;
    
    //passing each pair of 3 digits to another function
    for(int j=i-1;j>-1;j--)
       //toWords function will split pair of 3 digits to separate digits
        if(splitNumber[j]!=0)
            toWords(splitNumber[j]);
        if(j==3)//if the number contained more than 9 digits
            System.out.print("billion,");
        else if(j==2)//if the number contained more than 6 digits & less than 10 digits
            System.out.print("million,");
        else if(j==1)
            System.out.print("thousand,");//if the number contained more than 3 digits & less than 7 digits
            
                   

//function that splits number into individual digits
public static void toWords(int number)
    //splitSmallNumber array contains individual digits of number passed to this function
   int splitSmallNumber[]=new int[3],i=0,j;
    int temp=number;//making temporary copy of the number
    //logic to split number into its constituent digits

    while(temp!=0)
    
        splitSmallNumber[i++]=temp%10;
        temp/=10;
    
    //printing words for each digit
    for(j=i-1;j>-1;j--)
      //   if the digit is greater than zero
        if(splitSmallNumber[j]>=0)
            //if the digit is at 3rd place or if digit is at (1st place with digit at 2nd place not equal to zero)
           if(j==2||(j==0 && (splitSmallNumber[1]!=1)))
            
                switch(splitSmallNumber[j])
                

                    case 1:System.out.print("one ");break;
                    case 2:System.out.print("two ");break;
                    case 3:System.out.print("three ");break;
                    case 4:System.out.print("four ");break;
                    case 5:System.out.print("five ");break;
                    case 6:System.out.print("six ");break;
                    case 7:System.out.print("seven ");break;
                    case 8:System.out.print("eight ");break;
                    case 9:System.out.print("nine ");break;

            

        
        //if digit is at 2nd place
        if(j==1)
               //if digit at 2nd place is 0 or 1
                if(((splitSmallNumber[j]==0)||(splitSmallNumber[j]==1))&& splitSmallNumber[2]!=0 )
                System.out.print("hundred ");
            switch(splitSmallNumber[1])
               case 1://if digit at 2nd place is 1 example-213
                        switch(splitSmallNumber[0])
                        
                        case 1:System.out.print("eleven ");break;
                        case 2:System.out.print("twelve ");break;
                        case 3:System.out.print("thirteen ");break;
                        case 4:System.out.print("fourteen ");break;
                        case 5:System.out.print("fifteen ");break;
                        case 6:System.out.print("sixteen ");break;
                        case 7:System.out.print("seventeen ");break;
                        case 8:System.out.print("eighteen ");break;
                        case 9:System.out.print("nineteen ");break;
                        case 0:System.out.print("ten ");break;
                        break;
                        //if digit at 2nd place is not 1
                    case 2:System.out.print("twenty ");break;
                    case 3:System.out.print("thirty ");break;
                    case 4:System.out.print("forty ");break;
                    case 5:System.out.print("fifty ");break;
                    case 6:System.out.print("sixty ");break;
                    case 7:System.out.print("seventy ");break;
                    case 8:System.out.print("eighty ");break;
                    case 9:System.out.print("ninety ");break;
                    //case 0:   System.out.println("hundred ");break;

                                       
                   
    
  


【讨论】:

以上是关于编写代码将给定的数字转换为单词(例如 1234 作为输入应该输出 1234)的主要内容,如果未能解决你的问题,请参考以下文章

编写一个函数,将一个数字字符串转换为一个整数

我可以将给定的单词(String)表示为数字吗?

将给定数字转换为幸运数字所需的最小移动次数[关闭]

读取数字并转换为单词

如何在 Java 中将 String 转换为 int?

如何在 Java 中将 String 转换为 int?