kkken 发表于 2021-11-20 17:33:52

C++小白求助

本帖最后由 kkken 于 2021-11-20 17:35 编辑

想输出等于的内容,而它们是在?/&之间,加号是空一格的意思。
eg
cin>>anb?ef=12+3&g=ab&sk=&hijk=12-A
cout:
equals
equals [ ]
equals
equals

左边的[ ] 大小要一致。
只能用iostream 和cstring。

谢谢各位大神。

人造人 发表于 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 == '\0' || str == sep) {
            size_t size = &str - begin;
            char *temp = (char *)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;
}

void split_equals(const list_t *list, list_t *left, list_t *raw_right) {
    char buff;
    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;
    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; fgets(buff, 1024, stdin);
    buff = '\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
equals
equals
equals [ ]
equals
$

人造人 发表于 2021-11-20 20:24:10

你不让我用 vector,还不让我自己写一个了?
我本来是打算用 vector 存储中间数据的,但是题目不让用,而且最近刚好写了 list,用C语言写的,就拿过来用一下

人造人 发表于 2021-11-20 20:25:14

还有,最后必须是反序输出?

人造人 发表于 2021-11-20 20:29:12

只能用iostream 和cstring
那么,能不能这样?
#include "list.h"

人造人 发表于 2021-11-20 20:33:38

不能?
那这样呢?这次可是只有 iostream 和 cstring

#include <iostream>
#include <cstring>

#ifndef _LIST_H_
#define _LIST_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_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;
}

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 = (char *)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;
}

void split_equals(const list_t *list, list_t *left, list_t *raw_right) {
    char buff;
    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;
    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; fgets(buff, 1024, stdin);
    buff = '\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;
}

人造人 发表于 2021-11-20 20:34:43

$ g++-debug -o main main.cpp
$ ./main
anb?ef=12+3&g=ab&sk=&hijk=12-A
equals
equals
equals [ ]
equals
$

kkken 发表于 2021-11-23 16:45:45

人造人 发表于 2021-11-20 20:25
还有,最后必须是反序输出?

须反序输出

人造人 发表于 2021-11-23 17:00:09

kkken 发表于 2021-11-23 16:45
须反序输出

那你改一下代码不就好了
#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 == '\0' || str == sep) {
            size_t size = &str - begin;
            char *temp = (char *)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;
}

void split_equals(const list_t *list, list_t *left, list_t *raw_right) {
    char buff;
    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;
    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) {
    for(size_t i = list_size(left) - 1; i != (size_t)-1; --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; fgets(buff, 1024, stdin);
    buff = '\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;
}

18408238295 发表于 2021-11-23 20:15:23

{:10_256:}
页: [1]
查看完整版本: C++小白求助