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);
size_t index;
} 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, 0}, {'-', sub, 1}, {'*', mul, 2}, {'/', div_, 3}};
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("错误");
}
}
list_t *read_history(const char *filename) {
list_t *list = list_init();
FILE *fp = fopen(filename, "rb");
if(!fp) return list;
size_t size; fread(&size, 1, sizeof(size), fp);
for(size_t i = 0; i < size; ++i) {
exp_t e; fread(&e, 1, sizeof(e), fp);
e.op = op_table[e.op.index];
list_append(list, &e, sizeof(e));
}
fclose(fp);
return list;
}
void save_history(const list_t *list, const char *filename) {
FILE *fp = fopen(filename, "wb");
if(!fp) return;
size_t size = list_size(list);
fwrite(&size, 1, sizeof(size), fp);
for(size_t i = 0; i < size; ++i) {
exp_t e; list_get(list, i, &e, sizeof(e));
fwrite(&e, 1, sizeof(e), fp);
}
fclose(fp);
}
int main(void) {
srand(time(NULL));
//list_t *list = list_init();
list_t *list = read_history("history.dat");
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;}
}
save_history(list, "history.dat");
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)) if(!list_delete(list, 0)) return false;
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$