【问题标题】:In C, How to use scanf to scan in 1,000,000 ignoring the commas在 C 中,如何使用 scanf 扫描 1,000,000 忽略逗号
【发布时间】:2016-04-07 06:03:35
【问题描述】:

在 C 语言中,在像 1,000,000 这样的数字上使用 scanf 时忽略逗号的最佳方法是什么?

【问题讨论】:

  • while (scanf("%4[^,],%4[^,],%79[^,],%d", sem, type, title, &value) == 4)试试这个
  • 我不知道,但也许可以更改locale?

标签: c scanf


【解决方案1】:

我想说最好的方法是不为此使用scanf。至少不使用任何数字格式。

改为将其读取为字符串,然后删除逗号,最后删除convert to a number。

【讨论】:

    【解决方案2】:

    有多种方法可以删除逗号(或您想跳过的任何其他字符)。最简单(也是最灵活)的方法之一是简单地将两个指针向下移动输入字符串,将所有数字移到一起,忽略逗号。 (您必须确保在最后一位数字之后nul-terminate)。

    一个例子是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <limits.h>
    
    #define MAXC 25
    
    long xstrtol (char *p, char **ep, int base);
    
    int main (void) {
    
        char string[MAXC] = "";
        char *rp, *wp;          /* a read pointer and write pointer */
        int c;
        long n = 0;
    
        rp = wp = string;
    
        printf ("enter number with ',': ");
        if (!fgets (string, MAXC, stdin)) {
            fprintf (stderr, "error: insufficient input.\n");
            return 1;
        }
    
        /* work down each char in string, shifting number together */
        while (*rp && (('0' <= *rp && *rp <= '9') || *rp == ',')) {
            if (*rp == ',') { rp++; continue; }     /* skip commas */
            *wp++ = *rp;               /* shift digits together    */
            rp++;
        }
        *wp = 0;   /* nul-terminate */
    
        if (*rp != '\n') /* flush input buffer */
            while ((c = getchar()) != '\n' && c != EOF) {}
    
        printf ("\n string : %s\n", string);
        n = xstrtol (string, &rp, 10);       /* convert string to long */
    
        printf(" n      : %ld\n\n", n);
    
        return 0;
    }
    
    /** a simple strtol implementation with error checking.
     *  any failed conversion will cause program exit. Adjust
     *  response to failed conversion as required.
     */
    long xstrtol (char *p, char **ep, int base)
    {
        errno = 0;
    
        long tmp = strtol (p, ep, base);
    
        /* Check for various possible errors */
        if ((errno == ERANGE && (tmp == LONG_MIN || tmp == LONG_MAX)) ||
            (errno != 0 && tmp == 0)) {
            perror ("strtol");
            exit (EXIT_FAILURE);
        }
    
        if (*ep == p) {
            fprintf (stderr, "No digits were found\n");
            exit (EXIT_FAILURE);
        }
    
        return tmp;
    }
    

    (注意:xstrtol 只是提供了将string 转换为long 的错误检查)

    使用/输出

    $ ./bin/nwithcomma
    enter number with ',': 1,234,567
    
     string : 1234567
     n      : 1234567
    

    查看一下,如果您有任何问题,请告诉我。如果要将结果转换为int,可以检查INT_MIN 和INT_MAX,但留下答案就像long 一样容易。

    作为一个函数

    正如所指出的,它作为一个函数会更有用。可以把代码移到一个简单的转换函数上,调整主代码如下:

    ...
    #define MAXC 25
    
    long fmtstrtol (char *s);
    long xstrtol (char *p, char **ep, int base);
    
    int main (void) {
    
        char string[MAXC] = "";
    
        printf ("enter number with ',': ");
        if (!fgets (string, MAXC, stdin)) {
            fprintf (stderr, "error: insufficient input.\n");
            return 1;
        }
    
        printf(" number : %ld\n\n", fmtstrtol (string));
    
        return 0;
    }
    
    /* convert comma formatted string to long */
    long fmtstrtol (char *s)
    {
        if (!s || !*s) {
            fprintf (stderr, "fmtstrtol() error: invalid string.\n");
            exit (EXIT_FAILURE);
        }
    
        char *rp, *wp;  /* read pointer, write pointer */
        int c;
    
        rp = wp = s;
    
        while (*rp && (('0' <= *rp && *rp <= '9') || *rp == ',')) {
            if (*rp == ',') { rp++; continue; }
            *wp++ = *rp;
            rp++;
        }
        *wp = 0;
    
        if (*rp != '\n') /* flush input buffer */
            while ((c = getchar()) != '\n' && c != EOF) {}
    
        return xstrtol (s, &rp, 10);
    }
    ...
    

    【讨论】:

    • 为什么要有完整的工作程序?它可能是一个函数。
    • 没错,这只是一个自上而下的示例,我没有将其重新格式化为函数:(
    • @hacks - 纠正懒惰 - 提供的功能:)
    • 感谢您的努力。
    猜你喜欢
    • 2021-10-31
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多