- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我正在做一个作业,用户只需调用 parent.c 类,如“./parent 1 2 3 4 5 6 7 8”,child.c 类将计算结果的总和。
它的工作原理是像这样在一行中分别读取 2 个数字...“1+2”“3+4”“5+6”“7+8”
我已经成功地做到了这一点,但是当输入最终变得奇数时,我完全碰壁了。当我不断循环子进程时,每 2 个数字将不断相加,但是当循环到达输入末尾并且不再有 2 个数字要添加,而只有一个(奇数情况)时,这就成了一个问题。
因此,如果输入变成诸如...“./parent 1 2 3 4 5 6 7”或“./parent 1 2 3”
它只会平整返回 0。
我的文件将计算偶数,但不会将奇数相加。我想要实现的总体目标是,如果输入达到奇数,就能够添加一个零。我尝试过的一个解决方案是在 while 循环开始时检查数据数组中的值数量是否为奇数,然后将索引递增 1,然后向该索引添加 0。但它仍然没有提供正确的解决方案。
父类.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
int main(int argc, char *argv[])
{
int status, workerid, id;
int i = 0;
int loopcount = 0;
char* data[argc];
for(i = 1; i <= argc; i++)
{
data[i] = argv[i];
}
int numberloops = argc / 2;
int index = 0;
while(numberloops > index)
{
loopcount = 0;
for(i = 1; i <= argc; i += 2)
{
loopcount++;
id = fork();
if(id < 0)
{
perror("fork() ERROR.\n");
}
else if(id > 0)
{
workerid = waitpid(id, &status, 0);
status = WEXITSTATUS(status);
//update data array
char* statStr;
statStr = (char*) malloc(16);
snprintf(statStr, sizeof(statStr), "%d", status);
data[loopcount] = statStr;
}
else
{
execlp("./child", "child", data[i], data[i+1], NULL);
}
}
int arrayNum = atoi(data[loopcount]);
// Adds a 0 to the array.
while(loopcount < argc)
{
loopcount++;
data[loopcount] = 0;
}
//change argc (number of values in data array)
if(argc % 2 == 1)
{
argc = (argc + 1) / 2;
}
else
{
argc /= 2;
}
index++;
}
printf("Final Sum: %s.\n\n", data[1]);
}
child .c
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int x = atoi(argv[1]);
int y = atoi(argv[2]);
int sum = x + y;
printf("%d + %d = %d Worker PID: %d\n", x, y, sum, getpid());
exit(sum);
}
最佳答案
要解决“奇怪
-问题”,一种简单的方法是定义 char * data[argc + 1];
并初始化它指向的所有元素将相关元素设置为指向 argv
的元素之前的文字 "0"
。
关于c - 根据 argc 是偶数还是奇数,将 0 添加到 char* 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48535983/
我正在将 HP-Unix 下的大量 Oracle Pro*C 代码迁移到 Linux 环境。 程序中只定义了这样一个main方法: main _2a((argc,argv), int argc, ch
我正在使用库中的一个函数,该函数调用主程序中的 (argc,argv) 命令行函数直接传递给它;但是,我只想将一些参数传递给它,其余的在主代码中评估而不是函数。以下是带有评估参数的主要代码示例。 in
我在SO里游荡,看到了this question .然后我开始怀疑我是否可以溢出 argc。 标准规定 argv[argc] 必须是空指针,但如果 argc 溢出,则为 false。 (我 wrote
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
出于某种原因,如果我写“a+bi * c+di”而不是“a+bi + c+di”,我的 argc 参数从 4 更改为 6,但我不知道为什么。发生了什么事以及如何解决? 谢谢 #include #in
我有以下代码: void parse(char *commandLine) { int rc = 0; int argc = 0; char *cmdLine; cha
这是此问题的后续问题:Minimal example of Python interpreter embedding segfaults . 当我这样编译程序时: gcc -fno-diagnosti
根据this paper 和一些 stackoverflow 帖子,argc 在堆栈的顶部,argv 在它下面。我已经尝试了大约 3-4 种不同的方法: 将其弹出到初始化变量 (.data) - 通过
这个问题在这里已经有了答案: OpenCV argc and argv confusion (1 个回答) 关闭 9 年前。 我不明白,OpenCV 中用于加载图像的代码的功能是什么。 if(arg
我的教授和几个学生正在争论 argv 是否以 null 终止。我的 friend 写了一个小程序,它打印出 null 但另一个 child 说他可能只是在读入空白内存。有人可以解决这个讨论吗? 最佳答
所以我进行了 PHPUnit 测试,并在一个函数中找到了这段代码。 global $argv, $argc; echo $argc; print_r($argv); 我明白这些变量代表什么(从命令行传
我编写了一个打印输入的代码: int main(int argc, char *argv[]) { printf("%c", *argv[1]); return 0; } 当我输入
我将一个 main(int argc, char *argv[]) 函数重命名为普通子函数,例如 a(int argc, char *argv[]);并在我的 main() 中调用 a() 函数。然后
int main( const int argc , const char[] const argv) 如Effective C++ Item#3 指出“尽可能使用 const”,我开始思考“为什么不
我知道 argc 计算参数的数量,argv 接受参数,但它们是我可以确定我的参数是否来自 argv 以某种格式输入,如括号内和非括号内。比如./a.out "Hello Word"和./a.out H
我是 C++ 的新手,我正在尝试了解它的工作原理。所以我有下一个代码: int _tmain(int argc, _TCHAR* argv[]) { std::wcin.get();//for con
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我正在尝试查找 C++ 程序中的段错误。我注意到在发生段错误时 argc 已被修改为非常大的数字。该数字恰好是我的应用程序地址空间中的一个地址。这让我相信有什么东西正在破坏我的主堆栈框架。问题是,如何
为什么会出现以下情况: #include int main(int argc, char *argv[]) { char array[3] = {'1', '2', '3'};
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我是一名优秀的程序员,十分优秀!