简体   繁体   English

根据argc是偶数还是奇数向char *数组添加0

[英]adding a 0 to an array of char* based on the argc being even or odd

so I'm working on an assignment where the user simply calls the parent.c class like "./parent 1 2 3 4 5 6 7 8" and the child.c class will compute the sum of the result. 所以我正在做一个分配,用户只需简单地调用parent.c类,例如“ ./parent 1 2 3 4 5 6 7 8”,child.c类将计算结果的总和。

It works by reading 2 numbers each on the line like this... "1 + 2" "3 + 4" "5 + 6" "7 + 8" 它可以像这样在行上读取2个数字来工作...“ 1 + 2”“ 3 + 4”“ 5 + 6”“ 7 + 8”

I've gotten it to do this successfully, but I hit a complete brick wall when the input ends up being odd. 我已经成功地做到了,但是当输入最终变得奇怪时,我碰到了一个完整的砖墙。 As I keep looping the child processes, each 2 numbers will keep adding up, but it becomes a problem when the loop comes to the end of the input and there are no longer 2 numbers to add, but simply one (odd number scenario). 当我继续循环子进程时,每个2个数字将继续累加,但是当循环到输入的末尾并且不再有2个数字可添加,而只是一个(奇数情况)时,这将成为一个问题。

So if the input became something such as... "./parent 1 2 3 4 5 6 7" or "./parent 1 2 3" 因此,如果输入变成诸如...“ ./ parent 1 2 3 4 5 6 7”或“ ./parent 1 2 3”

it will just flat out return as 0. 它将平整地返回为0。

My file will compute the even numbers, but it will not add up an odd amount of numbers. 我的文件将计算偶数,但不会合计奇数。 The overall goal I would like to accomplish is to just be able to add a zero if the input ever hits an odd number. 我要实现的总体目标是,如果输入达到奇数,则只能加一个零。 One attempted solution I had was to just check at the beginning of the while loop if the amount of values in data array was odd, and then increment index by one and then add a 0 to that very index. 我尝试过的一种解决方案是,仅在while循环的开头检查数据数组中的值的数量是否为奇数,然后将索引递增1,然后向该索引添加0。 But it still didn't provide the correct solution. 但是它仍然没有提供正确的解决方案。

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

To get around the " odd -issue" one simple approach would be to define char * data[argc + 1]; 为了解决“ odd问题”,一种简单的方法是定义char * data[argc + 1]; and initialise all it's elements to point to a literal "0" before setting the relevant elements to point to argv 's elements. 并初始化所有它的元素,指向文字"0"设置相关元素指向前argv的元素。

It seems to me you are making the code much more complicated than need. 在我看来,您使代码变得比需要复杂得多。 Some specific items: 一些特定的项目:

1) Why do you have two nested loops (ie a while with a for inside)? 1)你为什么有两个嵌套循环(即while具有for内部)? As far as I can see that will just lead to too many calls of child 据我所知,这只会导致child电话过多

2) workerid seems unused!? 2) workerid似乎未使用!?

3) arrayNum seems unused!? 3) arrayNum似乎未使用!

4) Why convert the return values into strings? 4)为什么将返回值转换为字符串? You want to calculate a sum so just keep it as integer 您想计算总和,所以只需将其保持为整数

Simplifying your code could give something like: 简化代码可以得到如下结果:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
  int status, id;
  int i = 1;
  int sum = 0;
  int numberloops = argc / 2;
  int index = 0;

  while(numberloops > index)
  {
    id = fork();
    if(id < 0)
    {
      perror("fork() ERROR.\n");
    }
    else if(id > 0)
    {
      waitpid(id, &status, 0);
      status = WEXITSTATUS(status);
      sum += status;
    }
    else
    {
      if (i == argc-1)
      {
        execlp("./child", "child", argv[i], "", NULL);
      }
      else
      {
        execlp("./child", "child", argv[i], argv[i+1], NULL);
      }
    }

    i = i + 2;
    index++;
  }

  printf("Final Sum: %d.\n\n", sum);
}

Example: 例:

./parent 1 2 3 4 5
1 + 2 = 3 Worker PID: 12961
3 + 4 = 7 Worker PID: 12962
5 + 0 = 5 Worker PID: 12963
Final Sum: 15.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM