我需要从 a 中总结出一些不同的数字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;
}
它可以完美地处理除负数之外的所有内容,它只是忽略-
并添加 10。
有人知道如何解决这个问题吗?我无法理解它。