读取未知数量的整数并用文字打印它们

时间:2016-05-27 17:29:39

标签: c input stdin

我正在尝试处理来自stdin的输入,但仍然遇到了墙壁。 我的目标是读取一串数字(0-99)并用文字打印每一个。 我的第一次尝试是:

int main(void) {

char *a[20] = {"zero","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"};

char *b[8] = {"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};

int num=0, tens=0, ones=0;

while (scanf("%d", &num)==1){
    tens = num/10;
    ones = num%10;

    if (tens>1){

        printf("%s ", b[tens-2]);
        printf("%s \n", a[ones]);
    }
    else
        printf("%s \n", a[num]);
}
printf("done");
return 0;
}

输出正确但scanf永远不会终止循环。

第二次尝试:

int main(void) {

char *a[20] = {"zero","one","two","three","four","five","six","seven","eight",
"nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
"seventeen","eighteen","nineteen"};

char *b[8] = {"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
char line[1024], *ptr = NULL;
long num;
int tens=0, ones=0;

if(fgets(line, sizeof(line), stdin)!=NULL){


    do {
        num = strtol(line, &ptr, 10);
        tens = num/10;
        ones = num%10;

        if (tens>1){

            printf("%s ", b[tens-2]);
            printf("%s \n", a[ones]);
        }
        else
            printf("%s \n", a[num]);
    }while (*ptr!= '\n');
}

printf("done");
return 0;
}

这里我收到编译错误,无法找到问题所以我不知道它是否有效。

[更新]:第二个代码运行,但输入多于一个数字 12 35 51它无限地打印第一个数字(十二个)。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你是如此亲密。您只需要验证strtol的返回值,并在调用endptr后根据strtol更新指针地址。您还应该检查转换范围之外的值,如讨论中所述。这就是所需要的:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(void) {

    char *a[] = {"zero","one","two","three","four","five","six",
                 "seven","eight","nine","ten","eleven","twelve",
                 "thirteen","fourteen","fifteen","sixteen",
                 "seventeen","eighteen","nineteen"};

    char *b[] = {"twenty","thirty","fourty","fifty","sixty",
                 "seventy","eighty","ninety"};
    char line[1024] = "";
    long num;
    int tens=0, ones=0;

    if (fgets (line, sizeof(line), stdin) != NULL) {

        char *p = line, *ep = NULL;
        errno = 0;

        while (errno == 0) {
            num = strtol (p, &ep, 10);      /* convert to long  */
            if (p == ep) break;             /* no digits, break */
            p = ep;                         /* update p to ep   */
            if (num < 0 || 99 < num) {      /* validate range   */
                fprintf (stderr, "error: %ld - out of range.\n", num);
                continue;
            }
            tens = num/10;
            ones = num%10;
            if (tens > 1) {
                printf ("%s ", b[tens-2]);
                printf ("%s \n", a[ones]);
            }
            else
                printf("%s \n", a[num]);
        }
    }

    printf ("done\n");

    return 0;
}

示例使用/输出

$ ./bin/n2string
12 55 61 -1 33 102 4
twelve
fifty five
sixty one
error: -1 - out of range.
thirty three
error: 102 - out of range.
four
done

仔细看看,如果您有任何问题,请告诉我。

答案 1 :(得分:0)

代码的目的似乎是打印输入的数字的文本版本。你的代码几乎没有变化,我只是添加了一个库并运行它。

#include <stdio.h>
#include <stdlib.h>

int main(void) {

    char *a[20] = {"zero","one","two","three","four","five","six","seven","eight",
                   "nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
                   "seventeen","eighteen","nineteen"};

    char *b[8] = {"twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"};
    char line[1024], *ptr = NULL;
    long num;
    int tens=0, ones=0;

    if(fgets(line, sizeof(line), stdin)!=NULL){


        do {
            num = strtol(line, &ptr, 10);
            tens = num/10;
            ones = num%10;

            if (tens>1){

                printf("%s ", b[tens-2]);
                printf("%s \n", a[ones]);
            }
            else
                printf("%s \n", a[num]);
        }while (*ptr!= '\n');
    }

    printf("done");
    return 0;
}

<强>测试

19
nineteen 
done

也许问题是你并不知道代码的目的。