0

我有这个代码片段。我需要从字节流中减去数字(它已经被 a 标记化了,)。

问题是,当我这样做时,sub = sub - atoi(pchNew)它会给出所有负数的总和。不幸的是,减法与加法和乘法不同/不简单。

我的问题是:我们如何在 while 循环中减去数字?

else if(strcmp(pch, "sub")==0)
{
    sub = 0;
    pch = strtok(NULL," ");

    pch = strtok(pch," ");
    //printf ("------->%s   :",pch);
    pchNew = strtok(pch, ",");
    do
    {
        sub = sub - atoi(pchNew);

        //write(STDOUT_FILENO,"IN LOOP\n",9);
        pchNew = strtok(NULL,",");

    } while(pchNew !=NULL);

    printf("The Subtraction is= %d\n", sub);
    fflush(stdout);
}

'更新:'

输入:

Client> sub 4,3,4,5

输出:

Server> The Subtraction is= -16

提前致谢,更安全

4

1 回答 1

0

您想取第一个并从中减去下一个,对吗?

else if(strcmp(pch, "sub")==0)
{
pch = strtok(NULL," ");

pch = strtok(pch," ");
//printf ("------->%s   :",pch);
pchNew = strtok(pch, ",");

sub = atoi(pchNew);
pchNew = strtok(NULL,",");

while(pchNew !=NULL)
{
sub = sub - atoi(pchNew);

//write(STDOUT_FILENO,"IN LOOP\n",9);
pchNew = strtok(NULL,",");

};

printf("The Subtraction is= %d\n", sub);
fflush(stdout);
}
于 2013-05-17T23:02:41.760 回答