C++ 编写代码将给定的数字转换为单词(例如,输入 1234 应输出 1234)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12284768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write code to convert given number into words (eg 1234 as input should output one thousand two hundred and thirty four)
提问by Hariraman Radhakrishnan
Write C/C++/Java code to convert given number into words.
编写 C/C++/Java 代码将给定的数字转换为单词。
eg:- Input: 1234
例如:-输入:1234
Output: One thousand two hundred thirty-four.
输出:一千二百三十四。
Input: 10
输入:10
Output: Ten
输出:十
Does it require a complete switch case for digits 0 to 10.
数字 0 到 10 是否需要完整的开关盒。
Adding "teen" after every number name (eg: 14: four "teen".) from 14 to 19.
在从 14 到 19 的每个数字名称后添加“teen”(例如:14:四个“teen”。)。
And than adding "ty" and the digits name for a number in the range 20 to 99.
然后为 20 到 99 范围内的数字添加“ty”和数字名称。
And so on.
等等。
I think there must be some far better approach for solving this.
我认为必须有一些更好的方法来解决这个问题。
C code is preferred.
C 代码是首选。
回答by pr6989
#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;
}
回答by Adrian McCarthy
Instead of switch statements, consider using tables of strings indexed by a small value.
考虑使用由小值索引的字符串表,而不是 switch 语句。
const char * const ones[20] = {"zero", "one", "two", ..., "nineteen"};
const char * const tens[10] = {"", "ten", "twenty", ..., "ninety"};
Now break the problem into small pieces. Write a function that can output a single-digit number. Then write a function that can handle a two-digit number (which will probably use the previous function). Continue building up the functions as necessary.
现在把问题分解成小块。编写一个可以输出一位数的函数。然后编写一个可以处理两位数的函数(可能会用到前面的函数)。根据需要继续构建功能。
Create a list of test cases with expected output, and write code to call your functions and check the output, so that, as you fix problems for the more complicated cases, you can be sure that the simpler cases continue to work.
创建具有预期输出的测试用例列表,并编写代码来调用您的函数并检查输出,以便在为更复杂的用例修复问题时,可以确保更简单的用例继续工作。
回答by trodevel
if you are interested in a ready solution then you may look at HumanizerCpp library (https://github.com/trodevel/HumanizerCpp) - it is a port of C# Humanizer library and it does exactly what you want.
如果您对现成的解决方案感兴趣,那么您可以查看 HumanizerCpp 库(https://github.com/trodevel/HumanizerCpp)——它是 C# Humanizer 库的一个端口,它完全符合您的要求。
It can even convert to ordinals and currently supports 3 languages: English, German and Russian.
它甚至可以转换为序数,目前支持 3 种语言:英语、德语和俄语。
Example:
例子:
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;
Output:
输出:
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
одна тысяча сто двенадцатый
In any case you may take a look at the source code and reuse in your project or try to understand the logic. It is written in pure C++ without external libraries.
在任何情况下,您都可以查看源代码并在您的项目中重用或尝试理解其中的逻辑。它是用纯 C++ 编写的,没有外部库。
Regards, Serge
问候, 塞尔吉
回答by IGI30
/* 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;
}
回答by RedDragon
Works for any number from 0to 999999999.
适用于0到999999999 之间的任何数字。
This program gets a number from the user, divides it into three parts and stores them separately in an array. The three numbers are passed through a function that convert them into words. Then it adds "million" to the first part and "thousand" to the second part.
该程序从用户那里获取一个数字,将其分成三部分并将它们分别存储在一个数组中。这三个数字通过一个将它们转换为单词的函数传递。然后它在第一部分加上“百万”,在第二部分加上“千”。
#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];
}
}
}
回答by kowsalyajaganathan
#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();
}
回答by kowsalyajaganathan
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++;
}
}
}
回答by Chahit Kumar
/*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;
}
}
}
}
}