簡體   English   中英

在C中,如何使用scanf掃描1,000,000忽略逗號

[英]In C, How to use scanf to scan in 1,000,000 ignoring the commas

在C中,在像1,000,000這樣的數字上使用scanf時忽略逗號的最佳方法是什么?

我想說最好的辦法就是不要使用scanf 至少不使用任何數字格式。

而是將其作為字符串讀取,然后刪除逗號,最后轉換為數字

有許多方法可以刪除逗號(或任何其他要跳過的字符)。 最簡單(也是最靈活)之一就是簡單地將兩個指針向下移動輸入字符串,將所有數字移到一起,忽略逗號。 (你必須確保在最后一位數后終止 )。

一個例子是:

#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_MININT_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);
}
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM