【问题标题】:How can I parse numbers from a command line argument into two arrays如何将命令行参数中的数字解析为两个数组
【发布时间】:2014-12-02 01:30:03
【问题描述】:

我正在尝试从命令行获取两个参数,一个 char 和一个 x 长度的 1 和 0 的 int 字符串。我想检查它是否只有 5 个 1 和 0,如果是,我会将它们放入一个数组中。如果超过 5,则为 10,如 10011 10110;在这种情况下,我想将输入解析为两个不同的数组。第一个数字到 A,第二个到 B,依此类推,直到我有两个数组,例如 a = 10101,b = 01110。

我怎样才能做到这一点?我已经尝试了 put(c) until EOF while 循环,但似乎无法从中得到任何东西。下面是我的整个程序,它最终会将数组放入 LLL。

#include <stdio.h>
#include <stdlib.h>

struct nodeA {
  int dork; 
  struct nodeA * next;
};

typedef struct nodeA d1;

struct nodeB {
  int dork;
  struct nodeB * next;
};

typedef struct nodeB d2;

struct mathOp {
  char operation;
  struct mathOp * next;
};

typedef struct mathOp dorkOp;

int main(int argc, char * argv[]) {
  char a;
  int b;    

  d1 * curr = NULL, * head = NULL;
  dorkOp * curr3 = NULL, * head3 = NULL;
  int i = 0;

  int aA[8] = {0};
  int aB[8] = {0};
  int tempA, tempB;

  if (argc != 3) {
    printf("Program takes <char> <int>\n");
    exit(-1);
  }

  a = atoi (argv[1]);
  b = atoi (argv[2]);

  printf("Test call a = %c, b = %d \n", a, b);

  tempA = b;
  tempB = b;

  for (i = 0; i < 1; i++) {
    curr3 = (dorkOp *)malloc(sizeof(dorkOp));
    curr3->operation = a;
    curr3->next = head3;
    head = curr;
  }

  while (curr3) {
    printf("%c\n", curr3->operation);
    curr3 = curr3->next;
  }

  for (i = 0; i < 1; i++) {
    curr = (d1 *)malloc(sizeof(d1));
    curr->dork = tempA;
    curr->next = head;
    head = curr;
  }

  while (curr) {
    printf("%d\n", curr->dork);
    curr = curr->next;
  }

  return 0;
}

【问题讨论】:

  • 您的大部分代码似乎与您的问题无关。只需检查argv[1][0]argv[1][1] 等,检查它们是否等于'1''0',如果是,请将它们复制到您的数组中。
  • 我看不到您如何将输入 10011 10110 转换为两个值 a = 10101, b = 01110。到目前为止,您的问题还没有被完全描述(充其量 - 最坏的情况是完全难以理解)。正如 Paul Griffiths 所指出的,大部分代码与您的问题无关;不需要结构类型来解决将转换后的参数复制到一个或多个数组的问题。我认为你应该暂时删除这个问题,弄清楚如何用两个样本输入(一个短,一个长)、相应的输出以及如何获得输出的解释来询问它。然后取消删除。
  • @JonathanLeffler:我相信转换逻辑是a[] 最终将由input[0]input[2]input[4] 等组成,而b[] 最终将由input[1]input[3] 等。
  • @PaulGriffiths:好的——如果我们将 a = 10101 视为类似以下内容的简写:a[] = { 1, 0, 1, 0, 1 };(这是实现的结果,而不是您实现它的方式)。我仍然不清楚程序将如何被调用:./a.out X "10011 10110",或者什么。并且长度是严格的 5 还是 11(或者是省略了空格,所以是 5 或 10)?当输入错误时程序应该怎么做(比如X作为字符,被atoi()转换成int)?但是,在一定范围内,你说的话是有道理的。
  • @JonathanLeffler:同意,我对putc(c) 的尝试有点迷惑。

标签: c arrays parsing stdio


【解决方案1】:

我没有看到要回答的问题,所以这里是 OPs 代码,带有我的 cmets,前缀为 '&lt;--' 和以下行缩进。

  1. 良好的缩进有助于提高可读性, 所以我缩进了代码 (并修改了一些结构和变量定义 以符合良好的编码习惯)

编译结果如下:

50:16: warning: variable 'tempB' set but not used
48:9: warning: unused variable 'aB'
47:9: warning: unused variable 'aA'

我没有在以下代码中修复任何这些项目。 顺便说一句,为什么要发布无法编译的代码?

顺便说一句:此代码由 OP 提供,不执行 OP 备注中列出的任何操作。

#include <stdio.h>
#include <stdlib.h>

struct nodeA
{
    int dork;
    struct nodeA * next;
};

typedef struct nodeA d1;

struct nodeB
{
    int dork;
    struct nodeB * next;
};

typedef struct nodeB d2;

struct mathOp
{
    char operation;
    struct mathOp * next;
};

typedef struct mathOp dorkOp;


int main(int argc, char * argv[])
{
    char a;
    int b;
    d1 * curr = NULL;
    d1 * head = NULL;

    //d2 * curr2 = NULL;
    //d2 * head2 = NULL;

    dorkOp * curr3 = NULL;
    dorkOp * head3 = NULL;

    int i = 0;
    //int array[10] = {0};

    int aA[8] = {0};
    int aB[8] = {0};

    int tempA;
    int tempB;


    if(argc != 3)
    {
        printf("Program takes <char> <int>\n");
        exit(-1);
    }

    a = atoi (argv[1]);  
    // <-- argv[1] is expected to be a char, 
    //     and 'a' is defined as a char
    //     so the result of atoi() will be 0
    //     and 'a = 0' probably will not yield anything useful 

    b = atoi (argv[2]); 
    // <-- argv[2] is a null terminated string of 1s and 0s, 
    //     which would be easy to parse, so why the conversion? 

    printf("Test call a = %c, b = %d \n", a, b); 
    // <-- 'a' is an unprintable char, so the output is doubtful

    tempA = b;  // <-- shouldn't this be: tempA = a;
    tempB = b;

    for(i = 0; i < 1; i++)  
    // <-- waste of a 'for' because only room for one dorkOp 
    //     and the 'for' will only execute once
    //     suggest removing the 'for' but not the enclosed code block
    {
        curr3 = (dorkOp *)malloc(sizeof(dorkOp));
        // <-- in C, casting the returned value from malloc is 
        //     bad programming practice, for several reasons
        // <-- the returned value from malloc() needs to be checked
        //     to assure successful operation
        //     if not successful, 
        //     then, following lines are dereferencing an offset from 0
        //           which will result in a seg fault event
        curr3->operation = a;
        // <-- 'a' contains 0x00, (see above comment about setting 'a')
        //     so curr3->operation now contains 0x00
        curr3->next = head3; 
        // <-- head3 contains NULL
        //     so this line places NULL in curr3->next
        head = curr;  
        // <-- curr AND head are already both ptrs containing NULL
        //     so this line has no effect
    }

    while(curr3)
    {
        printf("%c\n", curr3->operation);
        // <-- curr3->operation was (see above) set to 0x00
        //     which is a non-printable char
        //     so the displayed output is in doubt
        curr3 = curr3->next;
        // <-- curr3, on the first pass through this loop is set to NULL
    }

    for(i = 0; i < 1; i++)
    // <-- waste of a 'for' because only room for one d1 
    //     and the 'for' will only execute once
    //     suggest removing the 'for' but not the enclosed code block
    {
        curr = (d1 *)malloc(sizeof(d1));
        // <-- in C, casting the returned value from malloc is 
        //     bad programming practice, for several reasons
        // <-- the code needs to check the returned value from malloc
        //     to assure a successful operation
        //     if not successful
        //     then, the following code will be dereferencing from address 0
        //           which will result in a seg fault event
        curr->dork = tempA;
        curr->next = head;
        // <-- head contains NULL so now curr-> contains NULL
        head = curr; 
    }

    // <-- following loop will only execute once
    //     so why make it a loop?
    while(curr)
    {
        printf("%d\n", curr->dork);
        curr = curr->next;
    }

    return 0;
}  // end function: main

【讨论】:

  • 我以前被告知要发布我的整个程序,所以我只是习惯这样做。代码确实编译。在我弄清楚如何根据输入将 argv[1] 参数读入两个单独的数组之后,将使用这些结构。
  • 输入为 5 个整数,1 或 0。例如10001. 也可以是十个整数,例如1000110001. 将输入读入数组时,我需要知道它是 5 还是 10。如果它是十我需要解析数字,第一个数字进入数组 a,第二个进入数组 b,第三个进入数组 a,依此类推。
  • 感谢 cmets。
  • "b = atoi (argv[2]); //
  • 如果你认为代码可以编译,那么你有很多,如果不是大多数编译器警告被禁用。在我的回答中,我确实列出了三个阻止此代码成功编译的项目。 IE。警告并不是对代码有多好的轻描淡写。相反,他们对代码有多糟糕提出了警告。
猜你喜欢
  • 2019-09-04
  • 2021-12-11
  • 2022-01-11
  • 2023-03-21
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
相关资源
最近更新 更多