小编Zen*_*ipu的帖子

总结来自C中的char*的数字

我需要总结一些不同的数字char*.

char类似于"12,38,40"结果为90的位置,但也可以是"12/5?10,-20"结果为7的位置.

我已经拥有的代码是:

extern int Add()
{
    char *string = "ab230c,i d*(s370*(20k-100d";
    char *totaal = string;

    int Sum = 0;

    while (*totaal)
    {
        if (isdigit(*totaal)) 
        {
            int getal = strtol(totaal, &totaal, 10);
            printf("%ld\n", getal);

            if(getal >= 0)
            {
                Sum += getal;
            }
        }
        else 
        {
            totaal++;
        }
    }
    printf("the sum of all numbers is: %d\n", Sum);
    return Sum;
}
Run Code Online (Sandbox Code Playgroud)

除负数之外,它完全处理所有事情,它只是忽略-并添加10.

有人知道如何解决这个问题吗?我无法理解它.

c int integer char strtol

2
推荐指数
1
解决办法
353
查看次数

标签 统计

c ×1

char ×1

int ×1

integer ×1

strtol ×1