首页 新闻 会员 周边 捐助

英文数字翻译

1
悬赏园豆:10 [已解决问题] 解决于 2010-12-03 15:25

如何用c语言任意输入一个数字,输出它相应的英文翻译?

孙佳敏的主页 孙佳敏 | 初学一级 | 园豆:110
提问于:2010-11-28 11:01
< >
分享
最佳答案
0
代码
#include<stdio.h>
void main()
{
flag:
long num;//待译
long num_temp;
long i;
int temp[10];//存储各位数字
int j=0;
char *trans[3][10]={{"","one","two","three","four","five","six","seven","eight","nine"},{"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"},{"","ten","twenty","thirty","forty","fifty","sixty","sevevty","eighty","ninety"}};//译表

printf(
"请输入一个1到1,400,000,000之间的整数:\n");
scanf(
"%d",&num);
if (!(num<=1400000000 && num>=0))
{
printf(
"你输入的数字太大了,我无能为力!\n");
}
else//把该数的每一位由高到低分别存入temp[j-1],temp[j-2],...,temp[0]
{
for (i=1; ; i*=10)
{
num_temp
= num / i;
if (num_temp != 0)
{
temp[j
++] = num_temp % 10;
}
else
{
break;
}
}

switch (j)//j为该数的位数
{
case 0:
{
printf(
"zero ");
}
break;
case 1:
{
printf(
"%s ",trans[0][temp[j-1]]);
}
break;
case 2:
{
if (temp[j-1] != 1)
{
printf(
"%s ",trans[2][temp[j-1]]);
printf(
"%s ",trans[0][temp[j-2]]);
}
else
{
printf(
"%s ",trans[1][temp[j-2]]);
}
}
break;
case 3:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"hundred ");
if (temp[j-2] != 0)
{
printf(
"and ");
if (temp[j-2] != 1)
{
printf(
"%s ",trans[2][temp[j-2]]);
printf(
"%s ",trans[0][temp[j-3]]);
}
else
{
printf(
"%s ",trans[1][temp[j-3]]);
}
}
else if (temp[j-3] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-3]]);
}
}
break;
case 4:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"thousand ");
if (temp[j-2] != 0)
{
printf(
"%s ",trans[0][temp[j-2]]);
printf(
"hundred ");
if (temp[j-3] != 0)
{
printf(
"and ");
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
}
}
else if (temp[j-4] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-4]]);
}
}
else if(temp[j-3] != 0)
{
printf(
"and ");
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
}
}
else if (temp[j-4] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-4]]);
}
}
break;
case 5:
{
if (temp[j-1] != 1)
{
printf(
"%s ",trans[2][temp[j-1]]);
printf(
"%s ",trans[0][temp[j-2]]);
}
else
{
printf(
"%s ",trans[1][temp[j-2]]);
}
printf(
"thousand ");
if (temp[j-3] != 0)
{
printf(
"%s ",trans[0][temp[j-3]]);
printf(
"hundred ");
if (temp[j-4] != 0)
{
printf(
"and ");
if (temp[j-4] != 1)
{
printf(
"%s ",trans[2][temp[j-4]]);
printf(
"%s ",trans[0][temp[j-5]]);
}
else
{
printf(
"%s ",trans[1][temp[j-5]]);
}
}
else if (temp[j-5] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-5]]);
}
}
else if(temp[j-4] != 0)
{
printf(
"and ");
if (temp[j-4] != 1)
{
printf(
"%s ",trans[2][temp[j-4]]);
printf(
"%s ",trans[0][temp[j-5]]);
}
else
{
printf(
"%s ",trans[1][temp[j-5]]);
}
}
else if (temp[j-5] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-5]]);
}
}
break;
case 6:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"hundred ");
if (temp[j-2] != 0)
{
printf(
"and ");
if (temp[j-2] != 1)
{
printf(
"%s ",trans[2][temp[j-2]]);
printf(
"%s ",trans[0][temp[j-3]]);
}
else
{
printf(
"%s ",trans[1][temp[j-3]]);
}
}
else if (temp[j-3] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-3]]);
}
printf(
"thousand ");
if (temp[j-4] != 0)
{
printf(
"%s ",trans[0][temp[j-4]]);
printf(
"hundred ");
if (temp[j-5] != 0)
{
printf(
"and ");
if (temp[j-5] != 1)
{
printf(
"%s ",trans[2][temp[j-5]]);
printf(
"%s ",trans[0][temp[j-6]]);
}
else
{
printf(
"%s ",trans[1][temp[j-6]]);
}
}
else if (temp[j-6] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-6]]);
}
}
else if(temp[j-5] != 0)
{
printf(
"and ");
if (temp[j-5] != 1)
{
printf(
"%s ",trans[2][temp[j-5]]);
printf(
"%s ",trans[0][temp[j-6]]);
}
else
{
printf(
"%s ",trans[1][temp[j-6]]);
}
}
else if (temp[j-6] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-6]]);
}
}
break;
case 7:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"million ");
if( temp[j-2] != 0)
{
printf(
"%s ",trans[0][temp[j-2]]);
printf(
"hundred ");
if (temp[j-3] != 0)
{
printf(
"and ");
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
}
}
else if (temp[j-4] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-4]]);
}
printf(
"thousand ");
}
else
{
if (temp[j-3] != 0)
{
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
}
printf(
"thousand ");
}
else if (temp[j-4] != 0)
{
printf(
"%s ",trans[0][temp[j-4]]);
printf(
"thousand ");
}
}
if (temp[j-5] != 0)
{
printf(
"%s ",trans[0][temp[j-5]]);
printf(
"hundred ");
if (temp[j-6] != 0)
{
printf(
"and ");
if (temp[j-6] != 1)
{
printf(
"%s ",trans[2][temp[j-6]]);
printf(
"%s ",trans[0][temp[j-7]]);
}
else
{
printf(
"%s ",trans[1][temp[j-7]]);
}
}
else if (temp[j-7] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-7]]);
}
}
else if(temp[j-6] != 0)
{
printf(
"and ");
if (temp[j-6] != 1)
{
printf(
"%s ",trans[2][temp[j-6]]);
printf(
"%s ",trans[0][temp[j-7]]);
}
else
{
printf(
"%s ",trans[1][temp[j-7]]);
}
}
else if (temp[j-7] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-7]]);
}
}
break;
case 8:
{
if (temp[j-1] != 1)
{
printf(
"%s ",trans[2][temp[j-1]]);
printf(
"%s ",trans[0][temp[j-2]]);
}
else
{
printf(
"%s ",trans[1][temp[j-2]]);
}
printf(
"million ");
if( temp[j-3] != 0)
{
printf(
"%s ",trans[0][temp[j-3]]);
printf(
"hundred ");
if (temp[j-4] != 0)
{
printf(
"and ");
if (temp[j-4] != 1)
{
printf(
"%s ",trans[2][temp[j-4]]);
printf(
"%s ",trans[0][temp[j-5]]);
}
else
{
printf(
"%s ",trans[1][temp[j-5]]);
}
}
else if (temp[j-5] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-5]]);
}
printf(
"thousand ");
}
else
{
if (temp[j-4] != 0)
{
if (temp[j-4] != 1)
{
printf(
"%s ",trans[2][temp[j-4]]);
printf(
"%s ",trans[0][temp[j-5]]);
}
else
{
printf(
"%s ",trans[1][temp[j-5]]);
}
printf(
"thousand ");
}
else if (temp[j-5] != 0)
{
printf(
"%s ",trans[0][temp[j-5]]);
printf(
"thousand ");
}
}
if (temp[j-6] != 0)
{
printf(
"%s ",trans[0][temp[j-6]]);
printf(
"hundred ");
if (temp[j-7] != 0)
{
printf(
"and ");
if (temp[j-7] != 1)
{
printf(
"%s ",trans[2][temp[j-7]]);
printf(
"%s ",trans[0][temp[j-8]]);
}
else
{
printf(
"%s ",trans[1][temp[j-8]]);
}
}
else if (temp[j-8] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-8]]);
}
}
else if(temp[j-7] != 0)
{
printf(
"and ");
if (temp[j-7] != 1)
{
printf(
"%s ",trans[2][temp[j-7]]);
printf(
"%s ",trans[0][temp[j-8]]);
}
else
{
printf(
"%s ",trans[1][temp[j-8]]);
}
}
else if (temp[j-8] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-8]]);
}
}
break;
case 9:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"hundred ");
if (temp[j-2] != 0)
{
printf(
"and ");
if (temp[j-2] != 1)
{
printf(
"%s ",trans[2][temp[j-2]]);
printf(
"%s ",trans[0][temp[j-3]]);
}
else
{
printf(
"%s ",trans[1][temp[j-3]]);
}
}
else if (temp[j-3] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-3]]);
}
printf(
"million ");
if( temp[j-4] != 0)
{
printf(
"%s ",trans[0][temp[j-4]]);
printf(
"hundred ");
if (temp[j-5] != 0)
{
printf(
"and ");
if (temp[j-5] != 1)
{
printf(
"%s ",trans[2][temp[j-5]]);
printf(
"%s ",trans[0][temp[j-6]]);
}
else
{
printf(
"%s ",trans[1][temp[j-6]]);
}
}
else if (temp[j-6] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-6]]);
}
printf(
"thousand ");
}
else
{
if (temp[j-5] != 0)
{
if (temp[j-5] != 1)
{
printf(
"%s ",trans[2][temp[j-5]]);
printf(
"%s ",trans[0][temp[j-6]]);
}
else
{
printf(
"%s ",trans[1][temp[j-6]]);
}
printf(
"thousand ");
}
else if (temp[j-6] != 0)
{
printf(
"%s ",trans[0][temp[j-6]]);
printf(
"thousand ");
}
}
if (temp[j-7] != 0)
{
printf(
"%s ",trans[0][temp[j-7]]);
printf(
"hundred ");
if (temp[j-8] != 0)
{
printf(
"and ");
if (temp[j-8] != 1)
{
printf(
"%s ",trans[2][temp[j-8]]);
printf(
"%s ",trans[0][temp[j-9]]);
}
else
{
printf(
"%s ",trans[1][temp[j-9]]);
}
}
else if (temp[j-9] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-9]]);
}
}
else if(temp[j-8] != 0)
{
printf(
"and ");
if (temp[j-8] != 1)
{
printf(
"%s ",trans[2][temp[j-8]]);
printf(
"%s ",trans[0][temp[j-9]]);
}
else
{
printf(
"%s ",trans[1][temp[j-9]]);
}
}
else if (temp[j-9] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-9]]);
}
}
break;
case 10:
{
printf(
"%s ",trans[0][temp[j-1]]);
printf(
"billion ");
if (temp[j-2] != 0)
{
printf(
"%s ",trans[0][temp[j-2]]);
printf(
"hundred ");
if (temp[j-3] != 0)
{
printf(
"and ");
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
}
}
else if (temp[j-4] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-4]]);
}
printf(
"million ");
}
else
{
if (temp[j-3] != 0)
{
if (temp[j-3] != 1)
{
printf(
"%s ",trans[2][temp[j-3]]);
printf(
"%s ",trans[0][temp[j-4]]);
printf(
"million ");
}
else
{
printf(
"%s ",trans[1][temp[j-4]]);
printf(
"million ");
}
}
else if (temp[j-4] != 0)
{
printf(
"%s ",trans[0][temp[j-4]]);
printf(
"million ");
}
}
if( temp[j-5] != 0)
{
printf(
"%s ",trans[0][temp[j-5]]);
printf(
"hundred ");
if (temp[j-6] != 0)
{
printf(
"and ");
if (temp[j-6] != 1)
{
printf(
"%s ",trans[2][temp[j-6]]);
printf(
"%s ",trans[0][temp[j-7]]);
}
else
{
printf(
"%s ",trans[1][temp[j-7]]);
}
}
else if (temp[j-7] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-7]]);
}
printf(
"thousand ");
}
else
{
if (temp[j-6] != 0)
{
if (temp[j-6] != 1)
{
printf(
"%s ",trans[2][temp[j-6]]);
printf(
"%s ",trans[0][temp[j-7]]);
}
else
{
printf(
"%s ",trans[1][temp[j-7]]);
}
printf(
"thousand ");
}
else if (temp[j-7] != 0)
{
printf(
"%s ",trans[0][temp[j-7]]);
printf(
"thousand ");
}
}
if (temp[j-8] != 0)
{
printf(
"%s ",trans[0][temp[j-8]]);
printf(
"hundred ");
if (temp[j-9] != 0)
{
printf(
"and ");
if (temp[j-9] != 1)
{
printf(
"%s ",trans[2][temp[j-9]]);
printf(
"%s ",trans[0][temp[j-10]]);
}
else
{
printf(
"%s ",trans[1][temp[j-10]]);
}
}
else if (temp[j-10] != 0)
{
printf(
"and ");
printf(
"%s ",trans[0][temp[j-10]]);
}
}
else if(temp[j-9] != 0)
{
printf(
"and ");
if (temp[j-9] != 1)
{
printf(
"%s ",trans[2][temp[j-9]]);
printf(
"%s ",trans[0][temp[j-10]]);
}
else
{
printf(
"%s ",trans[1][temp[j-10]]);
}
}
else if (temp[j-10] != 0)
{
printf(
"and ");
printf(
"%s",trans[0][temp[j-10]]);
}
}
break;
default :break;
}
printf(
"\n");
}
goto flag;
}

 

参考:http://hi.baidu.com/lqgds1234567/blog/item/0df6915619c09c53d0090614.html

收获园豆:10
artwl | 专家六级 |园豆:16736 | 2010-11-28 18:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册