编写代码以将给定的数字转换为单词。例如,如果输入“ 1234”,则输出应为“ 123.44”。
以下是相同的实现。该代码支持最多4位数字,即0到9999之间的数字。想法是创建一个数组,用于存储输出字符串的各个部分。一个数组用于一位数字,一个数组用于10到19的数字,一个数组用于20、30、40、50等。,另一组用于10的幂。
给定的数字分为两部分:前两位数字和后两位数字,并且这两部分分别打印。
C
/* C program to print a given number in words. The program
handles numbers from 0 to 9999 */
#include
#include
#include
/* A function that prints given number in words */
void convert_to_words(char* num)
{
int len = strlen(
num); // Get number of digits in given number
/* Base cases */
if (len == 0) {
fprintf(stderr, "empty string\n");
return;
}
if (len > 4) {
fprintf(stderr,
"Length more than 4 is not supported\n");
return;
}
/* The first string is not used, it is to make
array indexing simple */
char* single_digits[]
= { "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine" };
/* The first string is not used, it is to make
array indexing simple */
char* two_digits[]
= { "", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen" };
/* The first two string are not used, they are to make
array indexing simple*/
char* tens_multiple[] = { "", "", "twenty",
"thirty", "forty", "fifty",
"sixty", "seventy", "eighty",
"ninety" };
char* tens_power[] = { "hundred", "thousand" };
/* Used for debugging purpose only */
printf("\n%s: ", num);
/* For single digit number */
if (len == 1) {
printf("%s\n", single_digits[*num - '0']);
return;
}
/* Iterate while num is not '\0' */
while (*num != '\0') {
/* Code path for first 2 digits */
if (len >= 3) {
if (*num - '0' != 0) {
printf("%s ", single_digits[*num - '0']);
printf("%s ",
tens_power[len - 3]); // here len can
// be 3 or 4
}
--len;
}
/* Code path for last 2 digits */
else {
/* Need to explicitly handle 10-19. Sum of the
two digits is used as index of "two_digits"
array of strings */
if (*num == '1') {
int sum = *num - '0' + *(num + 1) - '0';
printf("%s\n", two_digits[sum]);
return;
}
/* Need to explicitely handle 20 */
else if (*num == '2' && *(num + 1) == '0') {
printf("twenty\n");
return;
}
/* Rest of the two digit numbers i.e., 21 to 99
*/
else {
int i = *num - '0';
printf("%s ", i ? tens_multiple[i] : "");
++num;
if (*num != '0')
printf("%s ",
single_digits[*num - '0']);
}
}
++num;
}
}
/* Driver program to test above function */
int main(void)
{
convert_to_words("9923");
convert_to_words("523");
convert_to_words("89");
convert_to_words("8989");
return 0;
}
Java
// Java program to print a given number in words. The
// program handles numbers from 0 to 9999
class GFG {
// A function that prints
// given number in words
static void convert_to_words(char[] num)
{
// Get number of digits
// in given number
int len = num.length;
// Base cases
if (len == 0) {
System.out.println("empty string");
return;
}
if (len > 4) {
System.out.println(
"Length more than 4 is not supported");
return;
}
/* The first string is not used, it is to make
array indexing simple */
String[] single_digits = new String[] {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
/* The first string is not used, it is to make
array indexing simple */
String[] two_digits = new String[] {
"", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
/* The first two string are not used, they are to
* make array indexing simple*/
String[] tens_multiple = new String[] {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
String[] tens_power
= new String[] { "hundred", "thousand" };
/* Used for debugging purpose only */
System.out.print(String.valueOf(num) + ": ");
/* For single digit number */
if (len == 1) {
System.out.println(single_digits[num[0] - '0']);
return;
}
/* Iterate while num
is not '\0' */
int x = 0;
while (x < num.length) {
/* Code path for first 2 digits */
if (len >= 3) {
if (num[x] - '0' != 0) {
System.out.print(
single_digits[num[x] - '0'] + " ");
System.out.print(tens_power[len - 3]
+ " ");
// here len can be 3 or 4
}
--len;
}
/* Code path for last 2 digits */
else {
/* Need to explicitly handle
10-19. Sum of the two digits
is used as index of "two_digits"
array of strings */
if (num[x] - '0' == 1) {
int sum
= num[x] - '0' + num[x + 1] - '0';
System.out.println(two_digits[sum]);
return;
}
/* Need to explicitely handle 20 */
else if (num[x] - '0' == 2
&& num[x + 1] - '0' == 0) {
System.out.println("twenty");
return;
}
/* Rest of the two digit
numbers i.e., 21 to 99 */
else {
int i = (num[x] - '0');
if (i > 0)
System.out.print(tens_multiple[i]
+ " ");
else
System.out.print("");
++x;
if (num[x] - '0' != 0)
System.out.println(
single_digits[num[x] - '0']);
}
}
++x;
}
}
// Driver Code
public static void main(String[] args)
{
convert_to_words("9923".toCharArray());
convert_to_words("523".toCharArray());
convert_to_words("89".toCharArray());
convert_to_words("8989".toCharArray());
}
}
// This code is contributed
// by Mithun Kumar
Python3
# Python program to print a given number in
# words. The program handles numbers
# from 0 to 9999
# A function that prints
# given number in words
def convert_to_words(num):
# Get number of digits
# in given number
l = len(num)
# Base cases
if (l == 0):
print("empty string")
return
if (l > 4):
print("Length more than 4 is not supported")
return
# The first string is not used,
# it is to make array indexing simple
single_digits = ["zero", "one", "two", "three",
"four", "five", "six", "seven",
"eight", "nine"]
# The first string is not used,
# it is to make array indexing simple
two_digits = ["", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen",
"nineteen"]
# The first two string are not used,
# they are to make array indexing simple
tens_multiple = ["", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty",
"ninety"]
tens_power = ["hundred", "thousand"]
# Used for debugging purpose only
print(num, ":", end=" ")
# For single digit number
if (l == 1):
print(single_digits[ord(num[0]) - '0'])
return
# Iterate while num is not '\0'
x = 0
while (x < len(num)):
# Code path for first 2 digits
if (l >= 3):
if (ord(num[x]) - 48 != 0):
print(single_digits[ord(num[x]) - 48],
end=" ")
print(tens_power[l - 3], end=" ")
# here len can be 3 or 4
l -= 1
# Code path for last 2 digits
else:
# Need to explicitly handle
# 10-19. Sum of the two digits
# is used as index of "two_digits"
# array of strings
if (ord(num[x]) - 48 == 1):
sum = (ord(num[x]) - 48 +
ord(num[x+1]) - 48)
print(two_digits[sum])
return
# Need to explicitely handle 20
elif (ord(num[x]) - 48 == 2 and
ord(num[x + 1]) - 48 == 0):
print("twenty")
return
# Rest of the two digit
# numbers i.e., 21 to 99
else:
i = ord(num[x]) - 48
if(i > 0):
print(tens_multiple[i], end=" ")
else:
print("", end="")
x += 1
if(ord(num[x]) - 48 != 0):
print(single_digits[ord(num[x]) - 48])
x += 1
# Driver Code
convert_to_words("9923")
convert_to_words("523")
convert_to_words("89")
convert_to_words("8989")
# This code is contributed
# by Mithun Kumar
C#
// C# program to print a given
// number in words. The program
// handles numbers from 0 to 9999
using System;
class GFG {
// A function that prints
// given number in words
static void convert_to_words(char[] num)
{
// Get number of digits
// in given number
int len = num.Length;
// Base cases
if (len == 0) {
Console.WriteLine("empty string");
return;
}
if (len > 4) {
Console.WriteLine("Length more than "
+ "4 is not supported");
return;
}
/* The first string is not used,
it is to make array indexing simple */
string[] single_digits = new string[] {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"
};
/* The first string is not used,
it is to make array indexing simple */
string[] two_digits = new string[] {
"", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen"
};
/* The first two string are not used,
they are to make array indexing simple*/
string[] tens_multiple = new string[] {
"", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"
};
string[] tens_power
= new string[] { "hundred", "thousand" };
/* Used for debugging purpose only */
Console.Write((new string(num)) + ": ");
/* For single digit number */
if (len == 1) {
Console.WriteLine(single_digits[num[0] - '0']);
return;
}
/* Iterate while num
is not '\0' */
int x = 0;
while (x < num.Length) {
/* Code path for first 2 digits */
if (len >= 3) {
if (num[x] - '0' != 0) {
Console.Write(
single_digits[num[x] - '0'] + " ");
Console.Write(tens_power[len - 3]
+ " ");
// here len can be 3 or 4
}
--len;
}
/* Code path for last 2 digits */
else {
/* Need to explicitly handle
10-19. Sum of the two digits
is used as index of "two_digits"
array of strings */
if (num[x] - '0' == 1) {
int sum = num[x] - '0' + num[x] - '0';
Console.WriteLine(two_digits[sum]);
return;
}
/* Need to explicitely handle 20 */
else if (num[x] - '0' == 2
&& num[x + 1] - '0' == 0) {
Console.WriteLine("twenty");
return;
}
/* Rest of the two digit
numbers i.e., 21 to 99 */
else {
int i = (num[x] - '0');
if (i > 0)
Console.Write(tens_multiple[i]
+ " ");
else
Console.Write("");
++x;
if (num[x] - '0' != 0)
Console.WriteLine(
single_digits[num[x] - '0']);
}
}
++x;
}
}
// Driver Code
public static void Main()
{
convert_to_words("9923".ToCharArray());
convert_to_words("523".ToCharArray());
convert_to_words("89".ToCharArray());
convert_to_words("8989".ToCharArray());
}
}
// This code is contributed
// by Mits
PHP
4)
{
echo "Length more than 4 " .
"is not supported\n";
return;
}
/* The first string is not used,
it is to make array indexing simple */
$single_digits = array("zero", "one", "two",
"three", "four", "five",
"six", "seven", "eight",
"nine");
/* The first string is not used,
it is to make array indexing simple */
$two_digits = array("", "ten", "eleven", "twelve",
"thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen",
"nineteen");
/* The first two string are not used,
they are to make array indexing simple*/
$tens_multiple = array("", "", "twenty", "thirty",
"forty", "fifty", "sixty",
"seventy", "eighty", "ninety");
$tens_power = array("hundred", "thousand");
/* Used for debugging purpose only */
echo $num.": ";
/* For single digit number */
if ($len == 1)
{
echo $single_digits[$num[0] - '0'] . " \n";
return;
}
/* Iterate while num
is not '\0' */
$x = 0;
while ($x < strlen($num))
{
/* Code path for first 2 digits */
if ($len >= 3)
{
if ($num[$x]-'0' != 0)
{
echo $single_digits[$num[$x] - '0'] . " ";
echo $tens_power[$len - 3] . " ";
// here len can be 3 or 4
}
--$len;
}
/* Code path for last 2 digits */
else
{
/* Need to explicitly handle
10-19. Sum of the two digits
is used as index of "two_digits"
array of strings */
if ($num[$x] - '0' == 1)
{
$sum = $num[$x] - '0' +
$num[$x] - '0';
echo $two_digits[$sum] . " \n";
return;
}
/* Need to explicitely handle 20 */
else if ($num[$x] - '0' == 2 &&
$num[$x + 1] - '0' == 0)
{
echo "twenty\n";
return;
}
/* Rest of the two digit
numbers i.e., 21 to 99 */
else
{
$i = $num[$x] - '0';
if($i > 0)
echo $tens_multiple[$i] . " ";
else
echo "";
++$x;
if ($num[$x] - '0' != 0)
echo $single_digits[$num[$x] -
'0'] . " \n";
}
}
++$x;
}
}
// Driver Code
convert_to_words("9923");
convert_to_words("523");
convert_to_words("89");
convert_to_words("8989");
// This code is contributed
// by Mithun Kumar
?>
输出:
9923: nine thousand nine hundred twenty three
523: five hundred twenty three
89: eighty nine
8989: eight thousand nine hundred eighty nine