sh-5.1$ ls
list.c list.h main main.c
sh-5.1$ cat main.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
char ch;
int (*func)(int a, int b);
} op_t;
typedef struct {
int a, b, c;
op_t op;
} exp_t;
int add(int a, int b) {return a + b;}
int sub(int a, int b) {return a - b;}
int mul(int a, int b) {return a * b;}
int div_(int a, int b) {return a / b;}
op_t op_table[] = {{'+', add}, {'-', sub}, {'*', mul}, {'/', div_}};
void menu(void) {
puts("*********");
puts("请选择运算符:");
puts("加法运算,请按1;");
puts("减法运算,请按2;");
puts("乘法运算,请按3;");
puts("除法运算,请按4;");
puts("历史记录,请按5;");
puts("退出程序,请按0;");
puts("*********");
}
void history(const list_t *list) {
for(size_t i = 0; i < list_size(list); ++i) {
exp_t e; list_get(list, i, &e, sizeof(e));
printf("%d%c%d=%d", e.a, e.op.ch, e.b, e.c);
if(e.c == e.op.func(e.a, e.b)) puts("正确");
else puts("错误");
}
}
int main(void) {
srand(time(NULL));
list_t *list = list_init();
while(1) {
menu();
int command; scanf("%d", &command);
if(command == 0) break;
if(command == 5) {history(list); continue;}
if(!(command >= 1 && command <= 4)) {puts("输入错误,请重新输入"); continue;}
exp_t e = {.a = rand() % 99 + 1, .b = rand() % 99 + 1, .op = op_table[command - 1]};
printf("%d%c%d=", e.a, e.op.ch, e.b);
scanf("%d", &e.c);
if(e.c == e.op.func(e.a, e.b)) puts("你很棒,加油!");
else puts("很遗憾!");
list_append(list, &e, sizeof(e));
if(list_size(list) > 10) list_delete(list, 0);
repeat: printf("是否继续答题?(Y/n) ");
char q; getchar(); scanf("%c", &q);
if(q == 'n' || q == 'N') break;
if(!(q == 'y' || q == 'Y')) {puts("输入错误,请重新输入"); goto repeat;}
}
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;
bool result = true;
while(!list_empty(list) && result) result = list_delete(list, 0);
return result;
}
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$