程式是打數字翻成英文
#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-21 18:41:26 · 1 個解答 · 發問者 小璋 1 in 電腦與網際網路 ➔ 程式設計
越詳細越好
2006-12-21 18:41:44 · update #1
前面的不必說了吧
以下把數字從右邊分段(三位一段)
k = strlen(buf);
j = (k-1)/3+1; /*可以分幾段 */
for (i=1; i
sprintf(tmp, "%3.3s", &(buf[k-i*3])); /* 從每一段的頭開始抓三個數字到 tmp */
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");
/* 副程式 */
if (x > 99) /* 百位數的讀法祇有 1~ 9 */
{
a = x/100; /* 求出百位數 */
printf("%s hundred ", num[a]);
x = x % 100; /* 拿掉百位數 */
}
if (x > 19) /* 19 以上都是 ????ty-??? */
{
a = x/10; /* 第一個 ????ty */
printf("%s-", &(num10[a]));
a = x % 10; /* 第二個 ??? 部分 */
printf("%s ", &(num[a]));
}
else if (x > 9) /* 10~19 有專屬讀法 */
{
a = x-10; /* 配合teen的指標從 0 開始 */
printf("%s", &(teen[a]));
}
else /* 1~9 就直接讀 */
{
printf("%s ", &(num[x]));
}
}
2006-12-22 02:53:43 · answer #1 · answered by JJ 7 · 0⤊ 0⤋