我怎样才能把这个值带到一个数组中?
How can I take this values to an array?
我正在尝试从用户那里获取此输入。
ARRAY [1,2,3,4,5,6]
并将数字传递给数组,但它不起作用。
这是我需要帮助的部分
else if (strncmp(input, "CONSTRUCT", 9) == 0)
{
printf("CONSTRUCT\n");
// CONSTRUCT [value1,value2,value3,...,valueN]
int i = 0;
char *token;
char *str = strdup(input);
char **array = str_split(str, '[');
char **array2 = str_split(array[1], ']');
char **array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
for (i = 0; i < 100; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < 100; i++)
{
root = insert(root, array4[i]);
}
printf("\n");
}
这是整个程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node
{
int data_element;
struct node *left, *right;
};
struct node *new_node(int data_element)
{
struct node *temp = (struct node *)malloc(sizeof(struct node)); // Allocating memory to the node
temp->data_element = data_element;
temp->left = temp->right = NULL;
return temp;
}
void display(struct node *root) // A function for the inroder traversal of the binary tree
{
if (root != NULL)
{
display(root->left);
printf("%d \n", root->data_element);
display(root->right);
}
}
struct node *insert(struct node *node, int data_element) // Function to insert a new node
{
if (node == NULL)
return new_node(data_element); // Return a new node if the tree if empty
if (data_element < node->data_element)
{
node->left = insert(node->left, data_element);
}
else if (data_element > node->data_element)
{
node->right = insert(node->right, data_element);
}
else if (data_element == node->data_element)
{
node->left = insert(node->left, data_element);
}
return node;
}
// DELETE
struct node *minValueNode(struct node *node)
{
struct node *current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node *Delete(struct node *root, int data_element)
{
if (root == NULL)
return root;
if (data_element < root->data_element)
root->left = Delete(root->left, data_element);
else if (data_element > root->data_element)
root->right = Delete(root->right, data_element);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = minValueNode(root->right);
root->data_element = temp->data_element;
root->right = Delete(root->right, temp->data_element);
}
return root;
}
// PARENT
struct node *parent(struct node *root, int data_element)
{
if (root == NULL)
return NULL;
if (root->data_element == data_element)
return NULL;
if (root->data_element > data_element)
return parent(root->left, data_element);
else
return parent(root->right, data_element);
}
char **str_split(char *a_str, const char a_delim)
{
char **result = 0;
size_t count = 0;
char *tmp = a_str;
char *last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char *) * count);
if (result)
{
size_t idx = 0;
char *token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
// take a string like this CONSTRUCT [31,65,3,10,5,100,3,12] return a array of int
int main()
{
struct node *root = NULL;
root = insert(root, 31);
root = insert(root, 65);
root = insert(root, 3);
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 100);
root = insert(root, 3);
root = insert(root, 12);
while (1)
{
char input[50];
scanf("%s", input);
if (strncmp(input, "EXIT", 4) == 0)
{
exit(0);
}
else if (strncmp(input, "CONSTRUCT", 9) == 0)
{
printf("CONSTRUCT\n");
// CONSTRUCT [value1,value2,value3,...,valueN]
int i = 0;
char *token;
char *str = strdup(input);
char **array = str_split(str, '[');
char **array2 = str_split(array[1], ']');
char **array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
for (i = 0; i < 100; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < 100; i++)
{
root = insert(root, array4[i]);
}
printf("\n");
}
else if (strncmp(input, "INSERT", 6) == 0)
{
printf("INSERT\n");
}
else if (strncmp(input, "LIST", 4) == 0)
{
printf("LIST\n");
display(root);
}
else if (strncmp(input, "PARENT", 6) == 0)
{
// PARENT 12
printf("PARENT\n");
char *token;
token = strtok(input, " ");
token = strtok(NULL, " ");
int data_element = atoi(token);
struct node *temp = parent(root, data_element);
if (temp == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", temp->data_element);
}
}
else if (strncmp(input, "DELETE", 6) == 0)
{
}
}
}
在这里你只是 运行 离开数组的末尾
char** array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
array3 的大小动态调整为数字的数量 + 1,但您尝试访问 100 个条目
您在列表末尾放置了一个空条目,使用它
int count = 0;
for (i = 0; i < 100; i++)
{
if (array3[i] == NULL)
break;
count++;
array4[i] = atoi(array3[i]);
}
for (i = 0; i < count; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < count; i++)
{
root = insert(root, array4[i]);
}
我看到了你对 space 的评论。此代码不适用于 'CONSTRUCT' 之后的 space,那是因为
scanf("%s", input);
读到第一个 space - 你想要 fgets。
我正在尝试从用户那里获取此输入。
ARRAY [1,2,3,4,5,6]
并将数字传递给数组,但它不起作用。
这是我需要帮助的部分
else if (strncmp(input, "CONSTRUCT", 9) == 0)
{
printf("CONSTRUCT\n");
// CONSTRUCT [value1,value2,value3,...,valueN]
int i = 0;
char *token;
char *str = strdup(input);
char **array = str_split(str, '[');
char **array2 = str_split(array[1], ']');
char **array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
for (i = 0; i < 100; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < 100; i++)
{
root = insert(root, array4[i]);
}
printf("\n");
}
这是整个程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct node
{
int data_element;
struct node *left, *right;
};
struct node *new_node(int data_element)
{
struct node *temp = (struct node *)malloc(sizeof(struct node)); // Allocating memory to the node
temp->data_element = data_element;
temp->left = temp->right = NULL;
return temp;
}
void display(struct node *root) // A function for the inroder traversal of the binary tree
{
if (root != NULL)
{
display(root->left);
printf("%d \n", root->data_element);
display(root->right);
}
}
struct node *insert(struct node *node, int data_element) // Function to insert a new node
{
if (node == NULL)
return new_node(data_element); // Return a new node if the tree if empty
if (data_element < node->data_element)
{
node->left = insert(node->left, data_element);
}
else if (data_element > node->data_element)
{
node->right = insert(node->right, data_element);
}
else if (data_element == node->data_element)
{
node->left = insert(node->left, data_element);
}
return node;
}
// DELETE
struct node *minValueNode(struct node *node)
{
struct node *current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node *Delete(struct node *root, int data_element)
{
if (root == NULL)
return root;
if (data_element < root->data_element)
root->left = Delete(root->left, data_element);
else if (data_element > root->data_element)
root->right = Delete(root->right, data_element);
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = minValueNode(root->right);
root->data_element = temp->data_element;
root->right = Delete(root->right, temp->data_element);
}
return root;
}
// PARENT
struct node *parent(struct node *root, int data_element)
{
if (root == NULL)
return NULL;
if (root->data_element == data_element)
return NULL;
if (root->data_element > data_element)
return parent(root->left, data_element);
else
return parent(root->right, data_element);
}
char **str_split(char *a_str, const char a_delim)
{
char **result = 0;
size_t count = 0;
char *tmp = a_str;
char *last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char *) * count);
if (result)
{
size_t idx = 0;
char *token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
// take a string like this CONSTRUCT [31,65,3,10,5,100,3,12] return a array of int
int main()
{
struct node *root = NULL;
root = insert(root, 31);
root = insert(root, 65);
root = insert(root, 3);
root = insert(root, 10);
root = insert(root, 5);
root = insert(root, 100);
root = insert(root, 3);
root = insert(root, 12);
while (1)
{
char input[50];
scanf("%s", input);
if (strncmp(input, "EXIT", 4) == 0)
{
exit(0);
}
else if (strncmp(input, "CONSTRUCT", 9) == 0)
{
printf("CONSTRUCT\n");
// CONSTRUCT [value1,value2,value3,...,valueN]
int i = 0;
char *token;
char *str = strdup(input);
char **array = str_split(str, '[');
char **array2 = str_split(array[1], ']');
char **array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
for (i = 0; i < 100; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < 100; i++)
{
root = insert(root, array4[i]);
}
printf("\n");
}
else if (strncmp(input, "INSERT", 6) == 0)
{
printf("INSERT\n");
}
else if (strncmp(input, "LIST", 4) == 0)
{
printf("LIST\n");
display(root);
}
else if (strncmp(input, "PARENT", 6) == 0)
{
// PARENT 12
printf("PARENT\n");
char *token;
token = strtok(input, " ");
token = strtok(NULL, " ");
int data_element = atoi(token);
struct node *temp = parent(root, data_element);
if (temp == NULL)
{
printf("NULL\n");
}
else
{
printf("%d\n", temp->data_element);
}
}
else if (strncmp(input, "DELETE", 6) == 0)
{
}
}
}
在这里你只是 运行 离开数组的末尾
char** array3 = str_split(array2[0], ',');
int array4[100];
for (i = 0; i < 100; i++)
{
array4[i] = atoi(array3[i]);
}
array3 的大小动态调整为数字的数量 + 1,但您尝试访问 100 个条目
您在列表末尾放置了一个空条目,使用它
int count = 0;
for (i = 0; i < 100; i++)
{
if (array3[i] == NULL)
break;
count++;
array4[i] = atoi(array3[i]);
}
for (i = 0; i < count; i++)
{
printf("%d\n", array4[i]);
}
for (i = 0; i < count; i++)
{
root = insert(root, array4[i]);
}
我看到了你对 space 的评论。此代码不适用于 'CONSTRUCT' 之后的 space,那是因为
scanf("%s", input);
读到第一个 space - 你想要 fgets。