|
发表于 2022-11-1 19:15:21
|
显示全部楼层
本楼为最佳答案
 像这样,写一个容器来存储这些表达式
- sh-5.1$ ls
- list.c list.h main.c
- sh-5.1$ cat main.c
- #include "list.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- typedef struct {
- int a, b;
- char op;
- int result;
- } exp_t;
- int calc(int a, char op, int b) {
- switch(op) {
- case '+': return a + b;
- case '-': return a - b;
- case '*': return a * b;
- case '/': return a / b;
- }
- return 0;
- }
- void output(const list_t *list, size_t index) {
- if(index >= list_size(list)) return;
- output(list, index + 1);
- exp_t e; list_get(list, index, &e, sizeof(e));
- printf("%d %c %d = %d\n", e.a, e.op, e.b, e.result);
- }
- int main(void) {
- srand(time(NULL));
- list_t *list = list_init();
- for(size_t i = 0; i < 10; ++i) {
- exp_t e;
- e.a = rand() % 100;
- e.b = rand() % 100;
- e.a = rand() % 2 ? e.a : -e.a;
- e.b = rand() % 2 ? e.b : -e.b;
- char op[] = {'+', '-', '*', '/'};
- e.op = op[rand() % 4];
- e.op = e.op == '/' && e.b == 0 ? op[0] : e.op;
- e.result = calc(e.a, e.op, e.b);
- list_append(list, &e, sizeof(e));
- }
- output(list, 0);
- list_deinit(list);
- return 0;
- }
- sh-5.1$ cat 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
- sh-5.1$ cat 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_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;
- }
- sh-5.1$ gcc -g -Wall -o main main.c list.c
- sh-5.1$ ls
- list.c list.h main main.c
- sh-5.1$ ./main
- 23 - -77 = 100
- 37 * -58 = -2146
- 94 * 19 = 1786
- 7 - -24 = 31
- 51 * 99 = 5049
- 68 + -48 = 20
- -95 * -80 = 7600
- -5 + -33 = -38
- 15 * 18 = 270
- -93 - 95 = -188
- sh-5.1$
复制代码 |
|