|
发表于 2021-11-20 20:19:34
|
显示全部楼层
解析这个字符串不让用库函数就不好整,还要限定 只能用iostream 和cstring,就更不好整了
main.cpp
- #include "list.h"
- #include <iostream>
- #include <cstring>
- 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[i] == '\0' || str[i] == sep) {
- size_t size = &str[i] - begin;
- char *temp = (char *)malloc(size + 1);
- memcpy(temp, begin, size);
- temp[size] = '\0';
- list_append(res, temp, size + 1);
- free(temp);
- begin = str[i] != '\0' ? &str[i] + 1 : &str[i];
- }
- }
- return res;
- }
- void split_equals(const list_t *list, list_t *left, list_t *raw_right) {
- char buff[1024];
- list_clean(left); list_clean(raw_right);
- for(size_t i = 0; i < list_size(list); ++i) {
- list_get(list, i, buff, 1024);
- list_t *temp = split(buff, '=');
- list_get(temp, 0, buff, 1024);
- list_append(left, buff, strlen(buff) + 1);
- if(list_size(temp) == 2) {
- list_get(temp, 1, buff, 1024);
- list_append(raw_right, buff, strlen(buff) + 1);
- } else list_append(raw_right, " ", 2);
- list_deinit(temp);
- }
- }
- void output(const list_t *left, const list_t *raw_right) {
- size_t count = 0;
- char buff[1024];
- for(size_t i = 0; i < list_size(left); ++i) {
- list_get(left, i, buff, 1024);
- size_t temp = strlen(buff);
- count = count < temp ? temp : count;
- }
- for(size_t i = 0; i < list_size(left); ++i) {
- list_get(left, i, buff, 1024);
- std::cout << '[' << buff;
- size_t space_count = count - strlen(buff);
- for(size_t j = 0; j < space_count; ++j) std::cout << ' ';
- std::cout << "] equals [";
- list_get(raw_right, i, buff, 1024);
- list_t *right = split(buff, '+');
- const char *sep = "";
- for(size_t j = 0; j < list_size(right); ++j) {
- list_get(right, j, buff, 1024);
- std::cout << sep << buff;
- sep = " ";
- }
- list_deinit(right);
- std::cout << ']' << std::endl;
- }
- }
- int main(void) {
- char buff[1024]; fgets(buff, 1024, stdin);
- buff[strlen(buff) - 1] = '\0';
- list_t *list = split(buff, '&');
- list_get(list, 0, buff, 1024);
- list_t *temp = split(buff, '?');
- list_get(temp, 1, buff, 1024);
- list_set(list, 0, buff, strlen(buff) + 1); // 去掉?前面的内容
- list_deinit(temp);
- list_t *left = list_init();
- list_t *raw_right = list_init();
- split_equals(list, left, raw_right);
- list_deinit(list);
- output(left, raw_right);
- list_deinit(raw_right);
- list_deinit(left);
- 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_set(list_t *list, size_t index, const 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 = (list_t *)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 = (struct list_node_tag *)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_set(list_t *list, size_t index, const void *data, size_t size) {
- bool res = list_delete(list, index);
- return res ? list_insert(list, index, data, size) : res;
- }
- 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;
- }
复制代码
- $ g++-debug -o main main.cpp list.c
- $ ./main
- anb?ef=12+3&g=ab&sk=&hijk=12-A
- [ef ] equals [12 3]
- [g ] equals [ab]
- [sk ] equals [ ]
- [hijk] equals [12-A]
- $
复制代码 |
|