C语言编程输入一串数字输出英语表达

例如
输入:12345678.90123
输出:twelve million three hundred and forty-five thousand six hundred and seventy-eight point nine zero one two three.

我这里有一个类似的程序,楼主的程序写起来有点大,仅供参考,当然如果不闲麻烦,可以用swich的case 语句结合来写,而我认为数的判定方式有多种,在这里,对于整数部分我们可以用将数值“三位为一组”来进行分!
如:678为第一组,前面加上 thousand
345为第二组,前面加上 million
依次为 billion
对于每组中的三个数分别含 百位 十位 个位
而小数点后面的则可以不用分位数,直接接对应的数!
下面是类似程序:

#include<stdio.h>
void main()
{
char *Eng1[20]={"zero","one","two","three","four","five","six","seven",
"eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen",
"sixteen","seventeen","eighteen","nineteen"};
char *Eng2[8]={"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
int num;
printf("请输入数字: ");
scanf("%d",&num);
printf("对应的英文为: ");
if(num>=0&&num<=19)
printf("%s\n",Eng1[num]);
else if(num<100)
{
int s,y;
s=num/10;
y=num%10;
printf("%s %s\n",Eng2[s-2],Eng1[y]);
}
else if(num<1000)
{
int b,s,y;
b=num/100;
y=num%100;
if(y>9)
{
s=(num%100)/10;
y=(num%100)%10;
if(y==0)
printf("%s hundred and %s\n",Eng1[b],Eng2[s-2]);
else
printf("%s hundred and %s %s\n",Eng1[b],Eng2[s-2],Eng1[y]);
}
else
printf("%s hundred and %s\n",Eng1[b],Eng1[y]);
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-10-09

这个你要知道数字与字母的对应,ASC表中可以找得到,然后输出时改变输出的类型,有整型变为字符型,如下面的程序:

#include<stdio.h>

 

void main()

{

    int a1,a2,a3,a4,a5;

    printf("请输入a1,a2,a3,a4,a5的值:\n");

    scanf("%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5);

    printf("输出的结果为:%c%c%c%c%c\n",a1,a2,a3,a4,a5);

}

运行结果如下:

追问

例如
输入:12345678.90123
输出:twelve million three hundred and forty-five thousand six hundred and seventy-eight point nine zero one two three.
这种形式的

追答

刚才回答的时候还没有补充,就写了这个!这样的话有点麻烦!

追问

好像还有是那么容错功能。
比如输入一个错误的数字,例如12.123.1234,能够提醒错误。
貌似有点挑战耶

追答

第2个回答  2012-10-09
什么意思追问

我已说明 请大神劳神了

相似回答