输入为字符串,输出为int,例如输入,输出应为1

时间:2014-05-05 06:51:24

标签: c string type-conversion

我已经尝试了atoi()函数和strtol()函数但没有获得所需的输出。如何将字符串转换为数值,就像输入一千个输出应该是1000。 谢谢

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



int main ()
{
    int i;
    char buffer [256];

    printf ("Enter a number: ");
    fgets (buffer, 256, stdin);

    i = atoi (buffer);
    printf ("The value entered is %d.",i);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

用自然语言解析数字并非易事。您可以通过字符串匹配检测小数字,并且通过拆分它们很容易解析像“二十三”这样的数字,但一般的方法很难。例如,“三十万”中的“百”这个词指的是100,000,而不是100。

这没有库函数,因为数字更好,更普遍地表示为十进制数。此外,这样的功能只能用一种语言工作。

但你可以自己动手。下面的代码将单词划分为多个值(例如四,二十三)和乘数(例如千)。两者都是可选的。它保留了一堆这样的价值乘数对来跟踪“优先级”:“一万四千”意味着(1 * 100 + 4) * 1000,但“一千一百四”意味着1 * 1000 + 1 * 100 + 4。当第一个乘数小于以下时,首先计算它。

代码区分大小写,但它可以用于字符串文字。一个小型测试套件位于下面的main函数中。

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



static const char *next(const char **p, int *len)
{
    const char *t = NULL;

    while (**p == ' ' || **p == '-') (*p)++;
    if (**p == '\0') return NULL;

    t = *p;
    while (**p && **p != ' ' && **p != '-') (*p)++;
    if (len) *len = *p - t;

    return t;
}

static int eq(const char *q, const char *p, int len)
{
    if (p == NULL) return 0;

    while (len--) {
        if (*p != *q) return 0;
        if (*p == '\0' || *q == '\0') return 0;
        p++; q++;
    }

    return (*q == '\0');
}

static int in(const char **q, const char *p, int len)
{
    int ix = 0;

    if (p == NULL) return 0;

    while (*q) {
        if (eq(*q, p, len)) return ix;
        ix++;
        q++;
    }

    return -1;
}

#define MAX 32

int parse_num(const char *p, int64_t *n)
{
    static const char *ones[] = {
        "zero", "one", "two", "three", "four", "five", "six",
        "seven", "eight", "nine", "ten", "eleven", "twelve",
        "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen", NULL
    };

    static const char *tens[] = {
        "-", "-", "twenty", "thirty", "forty", "fifty",
        "sixty", "seventy", "eighty", "ninety", NULL
    };

    int64_t value[MAX];
    int64_t base[MAX];
    int nvalue = 0;

    const char *t;
    int neg = 0;
    int len;
    int x;

    *n = 0;

    t = next(&p, &len);
    if (eq("minus", t, len)) {
        neg = 1;
        t = next(&p, &len);
    }

    while (t) {
        int64_t num = 0;
        int64_t mult = 1;
        int valid = 0;

        if (nvalue && eq("and", t, len)) t = next(&p, &len);

        x = in(tens, t, len);
        if (x >= 0) {
            num += x * 10;
            t = next(&p, &len);
            valid = 1;
        }

        x = in(ones, t, len);
        if (x >= 0) {
            num += x;
            t = next(&p, &len);
            valid = 1;
        }

        if (valid == 0) {
            long int l;
            char *end;

            l = strtol(t, &end, 10);
            if (end == p) {                
                num = l;
                valid = 1;
                t = next(&p, &len);
            }
        }

        if (eq("quadrillion", t, len))  mult = 1000000000000000LL;
        if (eq("trillion", t, len))     mult = 1000000000000LL;
        if (eq("billion", t, len))      mult = 1000000000LL;
        if (eq("million", t, len))      mult = 1000000LL;
        if (eq("thousand", t, len))     mult = 1000LL;
        if (eq("hundred", t, len))      mult = 100LL;

        if (mult > 1) {
            valid = 1;
            t = next(&p, &len);
        }

        if (valid == 0) return 0;        

        if (nvalue && base[nvalue - 1] == mult) return 0;

        while (nvalue && base[nvalue - 1] < mult) {
            nvalue--;
            num += value[nvalue] * base[nvalue];
        }

        if (nvalue == MAX) return 0;
        if (mult > 1 && num == 0) num = 1;

        value[nvalue] = num;
        base[nvalue] = mult;
        nvalue++;
    }

    if (t != NULL) return 0;
    if (nvalue == 0) return 0;

    while (nvalue--) {
        *n += value[nvalue] * base[nvalue];
    }
    if (neg) *n = -*n;

    return 1;
}

int main()
{
    const char *str[] = {
        "zero",
        "three",
        "minus one",
        "ten",
        "twenty-one",
        "eighty",
        "eight hundred eighty-eight",
        "three hundred and nineteen",
        "eleven hundred",
        "one hundred and twenty one",
        "twenty-four thousand",
        "thirty thousand one",
        "two million",
        "two hundred thirty thousand and eleven",
        "three million two hundred and thirty thousand and eleven",
        "minus eight trillion",
        "fifty-five billion one million nine thousand and twelve",
        "one thousand million",
        "nine hundred thousand",
        "nine thousand hundred",
        "one hundred thousand million",
        "nineteen hundred eighty-four",
        "4 billion 294 million 967 thousand 296",
        "two thousand thousand",
        "minus",
        "thirty-something",
        NULL
    };
    const char **p = str;

    while (*p) {
        int64_t n;
        int res;

        res = parse_num(*p, &n);
        if (res) {
            printf("%18" PRId64, n);
        } else {
            printf("%18s", "---");
        }
        printf("  %s\n", *p);
        p++;
    }

    return 0;
}

答案 1 :(得分:0)

strtol,atoi仅用于类型转换..他们不知道它的价值是什么。

他们只是将整数599转换为字符串599 ..但不是“五百九十九”

http://www.tutorialspoint.com/c_standard_library/c_function_strtol.htm

http://www.tutorialspoint.com/c_standard_library/c_function_atoi.htm

相关问题