gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:26:58 25 4
gpt4 key购买 nike

我试图从命令行获取两个参数,一个 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;
}

最佳答案

我没有看到要回答的问题,所以这里是 OP 代码,其中包含我的注释,前缀为 ' <-- ' 以及以下行缩进。

  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

关于c - 如何将命令行参数中的数字解析为两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27240409/

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