gpt4 book ai didi

c++ - while 循环中的减法。 C++

转载 作者:太空宇宙 更新时间:2023-11-04 04:15:32 25 4
gpt4 key购买 nike

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

问题是,当我执行 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

提前致谢,更安全

最佳答案

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

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);
}

关于c++ - while 循环中的减法。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16618999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com