如何将命令行参数中的数字解析为两个数组

时间:2014-12-02 01:30:04

标签: c arrays parsing stdio

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

我怎样才能做到这一点?我已经尝试了一个put(c)直到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;
}

1 个答案:

答案 0 :(得分:1)

我没有看到要回答的问题,所以这里是OP代码,带有我的评论,前缀为&#39; <--&#39;并按行缩进。

  1. 良好的缩进有助于提高可读性,    所以我缩进了代码    (并修改了一些结构和变量定义     与良好的编码实践保持一致)
  2. 以下是编译结果:

    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