English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

將一個數字字串轉成英文輸出

例如 : 123456 one hundred twenty-three thousand four hundred fifty-six

2006-12-19 07:50:57 · 1 個解答 · 發問者 小璋 1 in 電腦與網際網路 程式設計

可以請你簡單解釋一下嗎?

2006-12-19 12:55:31 · update #1

1 個解答

#include
#include

char num[10][6], num10[10][8], teen[9][10], unit[4][10];

main()
{
char buf[13], tmp[4];
int i, j, k, a[5];

sprintf(num[0], "");
sprintf(num[1], "one");
sprintf(num[2], "two");
sprintf(num[3], "three");
sprintf(num[4], "four");
sprintf(num[5], "five");
sprintf(num[6], "six");
sprintf(num[7], "seven");
sprintf(num[8], "eight");
sprintf(num[9], "nine");

sprintf(teen[0], "ten");
sprintf(teen[1], "eleven");
sprintf(teen[2], "twelve");
sprintf(teen[3], "thirteen");
sprintf(teen[4], "fourteen");
sprintf(teen[5], "fifteen");
sprintf(teen[6], "sixteen");
sprintf(teen[7], "seventeen");
sprintf(teen[8], "eighteen");
sprintf(teen[9], "nineteen");

sprintf(num10[0], "");
sprintf(num10[1], "ten");
sprintf(num10[2], "twenty");
sprintf(num10[3], "thirty");
sprintf(num10[4], "forty");
sprintf(num10[5], "fifty");
sprintf(num10[6], "sixty");
sprintf(num10[7], "seventy");
sprintf(num10[8], "eighty");
sprintf(num10[9], "ninety");

sprintf(unit[1], "");
sprintf(unit[2], "thousand ");
sprintf(unit[3], "million ");
sprintf(unit[4], "billion ");

printf("(up to 12) => ");
scanf("%s", &buf);

k = strlen(buf);
j = (k-1)/3+1;
for (i=1; i {
sprintf(tmp, "%3.3s", &(buf[k-i*3]));
a[i] = atoi(tmp);
}
j=k-i*3+3;
sprintf(tmp, "%*.*s", j,j,buf);
a[i] = atoi(tmp);

for (i=i ; i>0; i--)
{
to_read(a[i]);
printf("%s", unit[i]);
}
printf("\n");

system("PAUSE");
return 0;
}

to_read(x)
int x;
{
int a;

if (x > 99)
{
a = x/100;
printf("%s hundred ", num[a]);
x = x % 100;
}

if (x > 19)
{
a = x/10;
printf("%s-", &(num10[a]));
a = x % 10;
printf("%s ", &(num[a]));
}
else if (x > 9)
{
a = x-10;
printf("%s", &(teen[a]));
}
else
{
printf("%s ", &(num[x]));
}
}

2006-12-19 09:51:02 · answer #1 · answered by JJ 7 · 0 0

fedest.com, questions and answers