求C语言程序代码指导

写一个程序,用户键入大于0小于1000的数字,程序将数字用英文读出,例如123
读出one hundred and twenty-three 或者 one two three

#include <stdio.h>
  
//数组num_to_word用于定义数字到英文单词的转换规则    
char* num_to_word[10]={
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "siven",
    "eight",
    "nine"
};

//递归输出一个整数的每一位
void output_number_by_english(int num)
{
    int remainder=0;
    if(num>0)
    {
        //递归调用自身
        output_number_by_english(num/10);
        //将打印语句写在递归之后是为了按高位到低位的顺序打印
        printf("%s ", num_to_word[num%10]);
    }
}

void main()
{
    int input=-1;

    //输入,进行了整数范围判断
    while(input<0 || input>1000)
    {
        printf("Please input a number between 0 and 1000:\n");
        scanf("%d", &input);
    }
    
    output_number_by_english(input);
}

追问

我还没有学到递归调用和remainder...
input= -1是什么也看不懂。。
新手请包涵

追答

额,remainder表示整数除以10之后的余数。input=-1没有什么特殊含义,只是为了初始化input变量。
这个递归函数的过程是这样的,举个例子吧,假设有个数是123,
第一次调用函数的时候,参数是123,在这个函数里面首先判断123是否为0,如果不为0,则把123除以10,得到商是12,余数是3;
紧接着又第二次调用这个函数,此时的参数为12,也不为0,所以把12除以10,商1余2;
接下来进行第三次调用,参数为1,不等于0,所以又把1除以10,商0,余1;
当第四次调用的时候,参数为0,所以直接返回,
然后进入第三层调用,此时应该执行printf语句了,打印出余数1对应的英文one ;
然后进入第二层调用,执行printf语句,打印出余数two ;
最后进入第一层调用,打印出three ;
所以最后就打印出了one two three

追问

额。。如果不用数组能解决这题吗- -

追答

可以啊,用switch-case或者if-esle语句都行

追问

那就要if 27次了……因为是三位数。

追答

不用啊,三位数只需for或while循环三次就ok了

追问

那怎么把数字和英文词对应起来,不用数组的话就只好一一对应了、、那就是27次case
类似:
case 个位余数为1 输出one
case 百位余数为2 输出two hundred。。
之类的

追答

void output_number_by_english(int num)
{
    int remainder=0;
    if(num>0)
    {
        //递归调用自身
        output_number_by_english(num/10);
        //将打印语句写在递归之后是为了按高位到低位的顺序打印
        remainder = num%10;
        switch(remainder)
        {
        case 0:
            printf("%s ","zero");
            break;
        case 1:
            printf("%s ","one");
            break;
        case 2:
            printf("%s ","two");
            break;
        case 3:
            printf("%s ","three");
            break;
        case 4:
            printf("%s ","four");
            break;
        case 5:
            printf("%s ","five");
            break;
        case 6:
            printf("%s ","six");
            break;
        case 7:
            printf("%s ","seven");
            break;
        case 8:
            printf("%s ","eight");
            break;
        case 9:
            printf("%s ","nine");
            break;
        }
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-28
void main()
{
int a,b,c;
scanf("%d",&a);
while(a)
{
c=1;

while(1)

{
b=a%10;
if(b<10)

{

if(c==1)

break;

else

{

a=a-c*10;

break;
}

}

c=c*10;

}
swtich(b)
{
case 1:
printf("one");
case 2:
printf("one");
case 3:
printf("one");
case 4:
printf("one");

case 5:
printf("one");
case 6:
printf("one");
case 7:
printf("one");
case 8:
printf("one");
case 9:
printf("one");
case 0:
printf("one");
}

}
第2个回答  2013-09-28
给你思想:
定义字符串数组,分别对应每个数字的英文串
输入数字
对数字进行分析,从高位分别截取其数字
截取一个,输出字符串数组中的字符串.
不知道再贴代码...