我使用C編程語言通過以下方式完成了與標題相關的任務:
#include <stdio.h>
int main(){
int N;
scanf("%d", &N);
int ar[] = {2,3,4,5,6,7,8};
if (N/10 == ar[0]) printf("twenty");
if (N/10 == ar[1]) printf("thirty");
if (N/10 == ar[2]) printf("forty");
if (N/10 == ar[3]) printf("fifty");
if (N/10 == ar[4]) printf("sixty");
if (N/10 == ar[5]) printf("seventy");
if (N/10 == ar[6]) printf("eighty");
int ar1[] = {0,1,2,3,4,5,6,7,8,9};
if (N%10 == ar1[0]) printf("\n");
if (N%10 == ar1[1]) printf("one\n");
if (N%10 == ar1[2]) printf("two\n");
if (N%10 == ar1[3]) printf("three\n");
if (N%10 == ar1[4]) printf("four\n");
if (N%10 == ar1[5]) printf("five\n");
if (N%10 == ar1[6]) printf("six\n");
if (N%10 == ar1[7]) printf("seven\n");
if (N%10 == ar1[8]) printf("eight\n");
if (N%10 == ar1[9]) printf("nine\n");
}
有什么更聰明的方法可以做到這一點,因為我的代碼非常冗長且冗長?提前致謝。
uj5u.com熱心網友回復:
您可以將字串移動到陣列:
char *ar[] = { NULL, NULL, "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty"};
if (20 <= N && N <= 89)
printf("%s", ar[N / 10]);
使用類似的方案ar1。從 C99 開始,您可以使用指定的初始值設定項來提高可讀性:
char *ar[] = {
[2] = "twenty",
[3] = "thirty",
[4] = "forty",
[5] = "fifty",
[6] = "sixty",
[7] = "seventy",
[8] = "eighty",
};
uj5u.com熱心網友回復:
這對于您想要的東西來說可能有點矯枉過正,但是......
這是一個可以列印 0 到 999 之間值的英文單詞表示的函式:
/**
* Print a string representation of a century (3 digits). Assumes that n
* is between 0 and 999 and that there is sufficient space in `dst` for
* the resulting string.
*/
void printCentury( int n, char *dst )
{
static const char *units[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
static const char *teens[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
static const char *decades[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if ( n / 100 )
{
strcat( dst, units[n/100] );
strcat( dst, " hundred" );
n -= n/100 * 100;
if ( n )
strcat( dst, " and " );
}
if ( n / 10 > 1 )
{
strcat( dst, decades[n/10] );
n -= n / 10 * 10;
if ( n )
{
strcat( dst, "-" );
strcat( dst, units[n] );
}
}
else if ( n / 10 == 1 )
{
strcat( dst, teens[n-10] );
}
else if ( n )
{
strcat( dst, units[n] );
}
}
我們使用陣列來存盤我們的單詞,由 0 到 9 之間的值索引。要獲得“數百”數,我們將輸入除以 100。整數除法產生整數結果,因此123 / 100 == 1- 我們使用1索引到units陣列中,給出我們是字串"one",我們將其附加到dst. 我們立即追加" hundred",然后減去100從n給我們剩下的23值。由于該值不是 0,因此我們還附加" and "到字串。
為了得到“十”數,我們10再次除以-,整數除法給出整數結果,所以23 / 10 == 2。我們使用2索引到decades陣列中,給我們字串"twenty",我們將其附加到字串。然后我們20從該值中減去,得到剩余的3。
如果我們列印出一個decade字串 ( "twenty", "thirty", etc. ),我們會檢查“一個”數字是否為零 - 如果不是,我們會附加一個“-”,后跟單位字串。
有用于處理之間值的特殊情況11和19,因為11,12,13,15,以及18不按規定unit "teen"的圖案。
一些例子:
$ ./numbers 0
0 - zero
$ ./numbers 1
1 - one
$ ./numbers 10
10 - ten
$ ./numbers 100
100 - one hundred
$ ./numbers 101
101 - one hundred and one
$ ./numbers 111
111 - one hundred and eleven
$ ./numbers 121
121 - one hundred and twenty-one
We can use this function to print numbers greater than 999 - we just break our input into groups of three digits, so 12345678 is processed as 12 ("twelve"), 345 ("three hundred and forty-five"), 678 ("six hundred and seventy-eight"), we just need to add the appropriate magnitudes: "twelve million, three hundred and forty-five thousand, six hundred and seventy-eight". Here's that function:
/**
* Print a string representation of a number. Assumes a signed 32-bit
* int input and that there is sufficient space in dst for the
* result.
*/
void printNumber( int n, char *dst )
{
static int magval[] = { 0, 1000, 1000000, 1000000000 };
static const char *magnitudes[] = { "", "thousand", "million", "billion" };
int m = 0;
if ( n == 0 )
{
strcat( dst, "zero" );
return;
}
if ( n < 0 )
{
strcat( dst, "minus " );
n = -n;
}
while ( m < 4 && n >= magval[m] )
m ;
while ( --m )
{
if ( n / magval[m] )
{
/**
* print a comma after the last magnitude.
*/
if ( strlen( dst ) )
strcat( dst, ", " );
printCentury( n / magval[m], dst );
strcat( dst, " " );
strcat( dst, magnitudes[m] );
}
n -= n / magval[m] * magval[m];
}
if ( n )
{
/**
* Special case handling - we want 1001 to print as
* "one thousand and one", not "one thousand, one."
*/
if ( strlen( dst ) > 0 && n < 100 )
strcat( dst, " and " );
else if ( strlen( dst ) > 0 )
strcat( dst, ", " );
printCentury( n, dst );
}
}
We start by figuring out the magnitude - we do that by checking to see if n is greater than the n'th magnitude (0, 1000, 1000000, 1000000000). That gives us our starting index into the "magnitudes" array so we can pick the right magnitude string - "thousand", "million", or "billion".
Then it's a similar process to the printCentury function - we use integer division to give us the (up to) three leading digits of the number, print that "century", then subtract that magnitude to give us the next three digits. Again, there's a special case for zero, and there's logic to handle negative values (by printing "minus" and negating the input value).
This function is a bit more fiddly, since there are some special cases like "one billion and one" or "five thousand and fifty". We have to look ahead at the value and at what we've already written to make some decisions.
More examples:
$ ./numbers 1234
1234 - one thousand, two hundred and thirty-four
$ ./numbers 12345
12345 - twelve thousand, three hundred and forty-five
$ ./numbers 123456
123456 - one hundred and twenty-three thousand, four hundred and fifty-six
$ ./numbers 1234566
1234566 - one million, two hundred and thirty-four thousand, five hundred and sixty-six
$ ./numbers 12345668
12345668 - twelve million, three hundred and forty-five thousand, six hundred and sixty-eight
$ ./numbers 123456689
123456689 - one hundred and twenty-three million, four hundred and fifty-six thousand, six hundred and eighty-nine
$ ./numbers 1234566891
1234566891 - one billion, two hundred and thirty-four million, five hundred and sixty-six thousand, eight hundred and ninety-one
$ ./numbers -1234566891
-1234566891 - minus one billion, two hundred and thirty-four million, five hundred and sixty-six thousand, eight hundred and ninety-one
Complete program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
* Print a string representation of a century (3 digits). Assumes that n
* is between 0 and 999 and that there is sufficient space in `dst` for
* the resulting string.
*/
void printCentury( int n, char *dst )
{
static const char *units[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
static const char *teens[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
static const char *decades[] = { "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if ( n / 100 )
{
strcat( dst, units[n/100] );
strcat( dst, " hundred" );
n -= n/100 * 100;
if ( n )
strcat( dst, " and " );
}
if ( n / 10 > 1 )
{
strcat( dst, decades[n/10] );
n -= n / 10 * 10;
if ( n )
{
strcat( dst, "-" );
strcat( dst, units[n] );
}
}
else if ( n / 10 == 1 )
{
strcat( dst, teens[n-10] );
}
else if ( n )
{
strcat( dst, units[n] );
}
}
/**
* Print a string representation of a number. Assumes a signed 32-bit
* int input and that there is sufficient space in dst for the
* result.
*/
void printNumber( int n, char *dst )
{
static int magval[] = { 0, 1000, 1000000, 1000000000 };
static const char *magnitudes[] = { "", "thousand", "million", "billion" };
int m = 0;
if ( n == 0 )
{
strcat( dst, "zero" );
return;
}
if ( n < 0 )
{
strcat( dst, "minus " );
n = -n;
}
while ( m < 4 && n >= magval[m] )
m ;
while ( --m )
{
if ( n / magval[m] )
{
/**
* print a comma after the last magnitude.
*/
if ( strlen( dst ) )
strcat( dst, ", " );
printCentury( n / magval[m], dst );
strcat( dst, " " );
strcat( dst, magnitudes[m] );
}
n -= n / magval[m] * magval[m];
}
if ( n )
{
/**
* Special case handling - we want 1001 to print as
* "one thousand and one", not "one thousand, one."
*/
if ( strlen( dst ) > 0 && n < 100 )
strcat( dst, " and " );
else if ( strlen( dst ) > 0 )
strcat( dst, ", " );
printCentury( n, dst );
}
}
/**
* Main. Takes an integer value from the command line
* and prints the string representation.
*/
int main( int argc, char **argv )
{
if ( argc < 2 )
{
fprintf( stderr, "USAGE: %s <number>\n", argv[0] );
return 0;
}
int val = strtol( argv[1], NULL, 10 );
char text[1000] = {0};
printNumber( val, text );
printf( "%d - %s\n", val, text );
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/327469.html
