在C中读取格式化的整数数组

时间:2016-12-11 13:42:34

标签: c arrays dynamic-allocation

我想从用户(控制台)读取数据。用户可以给出的示例数据:

0 : [ 83, 42, 7 ]
21:[3, 6, 8, 12, 9, 3, 6, 8, 12]
63 : [ 8, 12, 9, 3, 6, 8 ]
0 : [ 20, 31, 70 ]

输入以EOF结束。

我不知道整数数组有多长([]里面的数组)以及有多少行数据。我想用数字保存数组(括号中的整数 - []),这是该行的第一个数字。

编写动态数组不是问题:

typedef struct {
  int *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = (int *)malloc(initialSize * sizeof(int));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int element) {
  if (a->used == a->size) {
    a->size *= 2;
    a->array = (int *)realloc(a->array, a->size * sizeof(int));
  }
  a->array[a->used++] = element;
}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}

然后在int main()中:

Array messages;
initArray(&messages, 10);    
insertArray(&messages, x); // x is the number we are reading

我可以说密钥是数组中的第一个元素。

但是我不确定如何解析输入并填充messages数组。 (还必须将消息数组保存到另一个数组中)

具有相同键的行也应该保存到同一个数组中(但这是一个细节)。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

#define LINELEN 100
#define INITSIZE 10

typedef struct {
    int *array;
    int key;
    size_t used;
    size_t currsize;
} line_t;

typedef struct {
    line_t *lines;
    int numlines;
} array_t;

array_t *initialize_array(void);
void generate_line(array_t *A, int index);
void insert_into_array(array_t *A, int index, char *number);
void printfree_array(array_t *A);
void check_ptr(void *ptr, const char *msg);

int
main(int argc, char *argv[]) {
    char line[LINELEN];
    char *key;
    array_t *A;
    size_t arrsize = INITSIZE, count = 0;

    A = initialize_array();

    A->lines = malloc(arrsize * sizeof(line_t));
    check_ptr(A->lines, "Allocation");

    printf("Enter data into console:\n");
    while (fgets(line, LINELEN, stdin) != NULL && strlen(line) != 1) {

        if (arrsize == count) {
            arrsize *= 2;
            A->lines = realloc(A->lines, arrsize * sizeof(line_t));
            check_ptr(A->lines, "Reallocation");
        }

        generate_line(A, count);

        key = strtok(line, " :[],");

        A->lines[count].key = atoi(key);

        while ((key = strtok(NULL,  " :[],\n")) != NULL) {
            insert_into_array(A, count, key);
        }

        count++;
        A->numlines++;
    }

    printfree_array(A);

    return 0;
}

void
printfree_array(array_t *A) {
    int i, j;

    printf("Your array of structures:\n");
    for (i = 0; i < A->numlines; i++) {
        printf("key = %d, ", A->lines[i].key);

        printf("array = [");
        for (j = 0; j < A->lines[i].used; j++) {
            printf("%d", A->lines[i].array[j]);

            if (j != A->lines[i].used-1) {
                printf(", ");
            }
        }
        free(A->lines[i].array);
        printf("]\n");
    }
    free(A->lines);
    free(A);
}

void
insert_into_array(array_t *A, int index, char *number) {
    if (A->lines[index].currsize == A->lines[index].used) {
        A->lines[index].currsize *= 2;
        A->lines[index].array = realloc(A->lines[index].array, 
                                        A->lines[index].currsize * sizeof(int));
        check_ptr(A->lines[index].array, "Reallocation");
    }
    A->lines[index].array[A->lines[index].used++] = atoi(number);
}

void
generate_line(array_t *A, int index) {
    A->lines[index].currsize = INITSIZE;
    A->lines[index].used = 0;
    A->lines[index].key = 0;
    A->lines[index].array = malloc(A->lines[index].currsize * sizeof(int));
    check_ptr(A->lines[index].array, "Allocation");
}

array_t
*initialize_array(void) {
    array_t *A = malloc(sizeof(*A));
    check_ptr(A, "Allocation");

    A->lines = NULL;
    A->numlines = 0;

    return A;
}

void
check_ptr(void *ptr, const char *msg) {
    if (!ptr) {
        printf("Unexpected null pointer: %s\n", msg);
        exit(EXIT_FAILURE);
    }
}

节目输出here