鱼C论坛

 找回密码
 立即注册
查看: 2739|回复: 3

向大佬请教一个问题,

[复制链接]
发表于 2021-11-17 19:29:20 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

使用道具 举报

发表于 2021-11-17 20:10:47 | 显示全部楼层
       利用结构体是因为看到了它的好处,请你现在告诉我,你这样做是看上了结构体的什么好处?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-11-17 20:20:39 | 显示全部楼层
本帖最后由 人造人 于 2021-11-17 20:27 编辑

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

main.c
  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>

  5. list_t *split(const char *str, char sep) {
  6.     list_t *res = list_init();
  7.     const char *begin = str;
  8.     for(size_t i = 0; ; ++i) {
  9.         if(*begin == '\0') break;
  10.         if(str[i] == '\0' || str[i] == sep) {
  11.             size_t size = &str[i] - begin;
  12.             char *temp = malloc(size + 1);
  13.             memcpy(temp, begin, size);
  14.             temp[size] = '\0';
  15.             list_append(res, temp, size + 1);
  16.             free(temp);
  17.             begin = str[i] != '\0' ? &str[i] + 1 : &str[i];
  18.         }
  19.     }
  20.     return res;
  21. }

  22. int main(void) {
  23.     char buff[1024]; fgets(buff, 1024, stdin);
  24.     buff[strlen(buff) - 1] = '\0';
  25.     list_t *list = split(buff, ' ');
  26.     for(size_t i = 0; i < list_size(list); ++i) {
  27.         list_get(list, i, buff, 1024);
  28.         puts(buff);
  29.     }
  30.     list_deinit(list);
  31.     return 0;
  32. }
复制代码


list.h
  1. #ifndef _LIST_H_
  2. #define _LIST_H_

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

  5. struct list_node_tag {
  6.     void *data; size_t size;
  7.     struct list_node_tag *next;
  8. };

  9. typedef struct {
  10.     struct list_node_tag *head;
  11.     size_t size;
  12. } list_t;

  13. list_t *list_init(void);
  14. void list_deinit(list_t *list);
  15. bool list_clean(list_t *list);
  16. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  17. bool list_delete(list_t *list, size_t index);
  18. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  19. bool list_append(list_t *list, const void *data, size_t size);
  20. size_t list_size(const list_t *list);
  21. bool list_empty(const list_t *list);

  22. #endif
复制代码


list.c
  1. #include "list.h"
  2. #include <stdlib.h>
  3. #include <memory.h>

  4. list_t *list_init(void) {
  5.     list_t *list = malloc(sizeof(*list));
  6.     if(!list) return NULL;
  7.     list->head = NULL;
  8.     list->size = 0;
  9.     return list;
  10. }

  11. void list_deinit(list_t *list) {
  12.     if(!list) return;
  13.     list_clean(list);
  14.     free(list);
  15. }

  16. bool list_clean(list_t *list) {
  17.     if(!list) return false;
  18.     while(!list_empty(list)) list_delete(list, 0);
  19.     return true;
  20. }

  21. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  22.     if(!list) return false;
  23.     if(list_size(list) < index) return false;
  24.     if(!data) return false;
  25.     struct list_node_tag **current = &list->head;
  26.     while(index--) current = &(*current)->next;
  27.     struct list_node_tag *node = malloc(sizeof(*node));
  28.     if(!node) return false;
  29.     node->data = malloc(size);
  30.     if(!node->data) {free(node); return false;}
  31.     memcpy(node->data, data, size);
  32.     node->size = size;
  33.     node->next = *current;
  34.     *current = node;
  35.     ++list->size;
  36.     return true;
  37. }

  38. bool list_delete(list_t *list, size_t index) {
  39.     if(!list) return false;
  40.     if(list_size(list) <= index) return false;
  41.     struct list_node_tag **current = &list->head;
  42.     while(index--) current = &(*current)->next;
  43.     struct list_node_tag *temp = *current;
  44.     *current = temp->next;
  45.     free(temp->data); free(temp);
  46.     --list->size;
  47.     return true;
  48. }

  49. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  50.     if(!list) return false;
  51.     if(list_size(list) <= index) return false;
  52.     if(!data) return false;
  53.     struct list_node_tag *const *current = &list->head;
  54.     while(index--) current = &(*current)->next;
  55.     struct list_node_tag *temp = *current;
  56.     if(temp->size > size) return false;
  57.     memcpy(data, temp->data, temp->size);
  58.     return true;
  59. }

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

  64. size_t list_size(const list_t *list) {
  65.     if(!list) return 0;
  66.     return list->size;
  67. }

  68. bool list_empty(const list_t *list) {
  69.     if(!list) return true;
  70.     return list_size(list) == 0;
  71. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-11-17 20:22:22 | 显示全部楼层
  1. $ ./main
  2. 123 45678 0 zxcvbbnm,./ qewruip[]'\=- z
  3. 123
  4. 45678
  5. 0
  6. zxcvbbnm,./
  7. qewruip[]'\=-
  8. z
  9. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 05:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表