紫苑天星 发表于 2021-11-17 19:29:20

向大佬请教一个问题,

本人萌新一个,刚刚学到结构体,前几天突发奇想,想到能不能用结构体编写一个代码,当你输入一个字符串时,这个程序就会自动把字符串截成多个字符串,并输入到结构体中,然后输出。我想了很久都没想到要怎么编写这样的一个代码。请大佬帮帮忙。

jackz007 发表于 2021-11-17 20:10:47

       利用结构体是因为看到了它的好处,请你现在告诉我,你这样做是看上了结构体的什么好处?

人造人 发表于 2021-11-17 20:20:39

本帖最后由 人造人 于 2021-11-17 20:27 编辑

你的描述不是很清楚,我按照我的理解写了一个,不知道是不是你期望的,我感觉应该不是

main.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

list_t *split(const char *str, char sep) {
    list_t *res = list_init();
    const char *begin = str;
    for(size_t i = 0; ; ++i) {
      if(*begin == '\0') break;
      if(str == '\0' || str == sep) {
            size_t size = &str - begin;
            char *temp = malloc(size + 1);
            memcpy(temp, begin, size);
            temp = '\0';
            list_append(res, temp, size + 1);
            free(temp);
            begin = str != '\0' ? &str + 1 : &str;
      }
    }
    return res;
}

int main(void) {
    char buff; fgets(buff, 1024, stdin);
    buff = '\0';
    list_t *list = split(buff, ' ');
    for(size_t i = 0; i < list_size(list); ++i) {
      list_get(list, i, buff, 1024);
      puts(buff);
    }
    list_deinit(list);
    return 0;
}


list.h
#ifndef _LIST_H_
#define _LIST_H_

#include <stddef.h>
#include <stdbool.h>

struct list_node_tag {
    void *data; size_t size;
    struct list_node_tag *next;
};

typedef struct {
    struct list_node_tag *head;
    size_t size;
} list_t;

list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);

#endif


list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>

list_t *list_init(void) {
    list_t *list = malloc(sizeof(*list));
    if(!list) return NULL;
    list->head = NULL;
    list->size = 0;
    return list;
}

void list_deinit(list_t *list) {
    if(!list) return;
    list_clean(list);
    free(list);
}

bool list_clean(list_t *list) {
    if(!list) return false;
    while(!list_empty(list)) list_delete(list, 0);
    return true;
}

bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) < index) return false;
    if(!data) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *node = malloc(sizeof(*node));
    if(!node) return false;
    node->data = malloc(size);
    if(!node->data) {free(node); return false;}
    memcpy(node->data, data, size);
    node->size = size;
    node->next = *current;
    *current = node;
    ++list->size;
    return true;
}

bool list_delete(list_t *list, size_t index) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    *current = temp->next;
    free(temp->data); free(temp);
    --list->size;
    return true;
}

bool list_get(const list_t *list, size_t index, void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    if(!data) return false;
    struct list_node_tag *const *current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    if(temp->size > size) return false;
    memcpy(data, temp->data, temp->size);
    return true;
}

bool list_append(list_t *list, const void *data, size_t size) {
    if(!list) return false;
    return list_insert(list, list_size(list), data, size);
}

size_t list_size(const list_t *list) {
    if(!list) return 0;
    return list->size;
}

bool list_empty(const list_t *list) {
    if(!list) return true;
    return list_size(list) == 0;
}

人造人 发表于 2021-11-17 20:22:22

$ ./main
123 45678 0 zxcvbbnm,./ qewruip[]'\=- z
123
45678
0
zxcvbbnm,./
qewruip[]'\=-
z
$
页: [1]
查看完整版本: 向大佬请教一个问题,