鱼C论坛

 找回密码
 立即注册
查看: 2752|回复: 5

求大佬写一下第七题,谢谢

[复制链接]
发表于 2022-11-22 11:07:53 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目二:先做一菜单,菜单格式如下:
*********
请选择运算符:
加法运算,请按1;
减法运算,请按2;
乘法运算,请按3;
除法运算,请按4;
*****
程序要求:先显示菜单,客户选择1-4中任意一个数字,就做相应的计算。然后要求客户输入两个数,运算出结果。比如客户选择了...
在题目二的基础上进行改进。程序要求:先显示菜单,客户选择了数字,就做相应的计算。然后系统随机产生两个整数,列出式子,要求客户写出答案,最后系统判断客户答题是否正确,正确显示“你很棒,加油!”,错误显示“很遗憾!”。比如客户选择了2,系统就随机产生一个式子(例23-12=),然后要求客户做减法。                                                    在题目三的基础上进行改进。客户做完一题后,询问客户是否继续答题(继续请输入Y,退出请输入N)。如客户选择继续答题,就重复显题目四,否则退出。
即用户可以循环做答,直到其输入N.
在题目四的基础上进行改进。客户做完多道题目后,可以查询最近十道题的答题情况(如没有做到十道题,就显示全部的题目)。查询显示结果的格式如下:
2*3=6正确
2*4=7错误
提示:要用到数组
6、在题目五的基础上进行改进。 客户做完多道题目后(题目的数量是不确定的,不能使用数组),可以查询所有的答题情况。
提示:要用到链表
7、在题目六的基础上进行改进。 客户可以任何时候都可以查询所有的答题情况(包括重启电脑后)。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-11-22 11:08:49 From FishC Mobile | 显示全部楼层
大佬,尽量简化一下,谢谢了,用c语言做,真的太感谢了,求大佬解答
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-22 14:03:54 | 显示全部楼层
  1. sh-5.1$ cat main.c
  2. #include "list.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>

  6. typedef struct {
  7.     char ch;
  8.     int (*func)(int a, int b);
  9.     size_t index;
  10. } op_t;

  11. typedef struct {
  12.     int a, b, c;
  13.     op_t op;
  14. } exp_t;

  15. int add(int a, int b) {return a + b;}
  16. int sub(int a, int b) {return a - b;}
  17. int mul(int a, int b) {return a * b;}
  18. int div_(int a, int b) {return a / b;}

  19. op_t op_table[] = {{'+', add, 0}, {'-', sub, 1}, {'*', mul, 2}, {'/', div_, 3}};

  20. void menu(void) {
  21.     puts("*********");
  22.     puts("请选择运算符:");
  23.     puts("加法运算,请按1;");
  24.     puts("减法运算,请按2;");
  25.     puts("乘法运算,请按3;");
  26.     puts("除法运算,请按4;");
  27.     puts("历史记录,请按5;");
  28.     puts("退出程序,请按0;");
  29.     puts("*********");
  30. }

  31. void history(const list_t *list) {
  32.     for(size_t i = 0; i < list_size(list); ++i) {
  33.         exp_t e; list_get(list, i, &e, sizeof(e));
  34.         printf("%d%c%d=%d", e.a, e.op.ch, e.b, e.c);
  35.         if(e.c == e.op.func(e.a, e.b)) puts("正确");
  36.         else puts("错误");
  37.     }
  38. }

  39. list_t *read_history(const char *filename) {
  40.     list_t *list = list_init();
  41.     FILE *fp = fopen(filename, "rb");
  42.     if(!fp) return list;
  43.     size_t size; fread(&size, 1, sizeof(size), fp);
  44.     for(size_t i = 0; i < size; ++i) {
  45.         exp_t e; fread(&e, 1, sizeof(e), fp);
  46.         e.op = op_table[e.op.index];
  47.         list_append(list, &e, sizeof(e));
  48.     }
  49.     fclose(fp);
  50.     return list;
  51. }

  52. void save_history(const list_t *list, const char *filename) {
  53.     FILE *fp = fopen(filename, "wb");
  54.     if(!fp) return;
  55.     size_t size = list_size(list);
  56.     fwrite(&size, 1, sizeof(size), fp);
  57.     for(size_t i = 0; i < size; ++i) {
  58.         exp_t e; list_get(list, i, &e, sizeof(e));
  59.         fwrite(&e, 1, sizeof(e), fp);
  60.     }
  61.     fclose(fp);
  62. }

  63. int main(void) {
  64.     srand(time(NULL));
  65.     //list_t *list = list_init();
  66.     list_t *list = read_history("history.dat");
  67.     while(1) {
  68.         menu();
  69.         int command; scanf("%d", &command);
  70.         if(command == 0) break;
  71.         if(command == 5) {history(list); continue;}
  72.         if(!(command >= 1 && command <= 4)) {puts("输入错误,请重新输入"); continue;}
  73.         exp_t e = {.a = rand() % 99 + 1, .b = rand() % 99 + 1, .op = op_table[command - 1]};
  74.         printf("%d%c%d=", e.a, e.op.ch, e.b);
  75.         scanf("%d", &e.c);
  76.         if(e.c == e.op.func(e.a, e.b)) puts("你很棒,加油!");
  77.         else puts("很遗憾!");
  78.         list_append(list, &e, sizeof(e));
  79.         //if(list_size(list) > 10) list_delete(list, 0);
  80. repeat: printf("是否继续答题?(Y/n) ");
  81.         char q; getchar(); scanf("%c", &q);
  82.         if(q == 'n' || q == 'N') break;
  83.         if(!(q == 'y' || q == 'Y')) {puts("输入错误,请重新输入"); goto repeat;}
  84.     }
  85.     save_history(list, "history.dat");
  86.     list_deinit(list);
  87.     return 0;
  88. }
  89. sh-5.1$ cat list.h
  90. #ifndef _LIST_H_
  91. #define _LIST_H_

  92. #include <stddef.h>
  93. #include <stdbool.h>

  94. struct list_node_tag {
  95.     void *data; size_t size;
  96.     struct list_node_tag *next;
  97. };

  98. typedef struct {
  99.     struct list_node_tag *head;
  100.     size_t size;
  101. } list_t;

  102. list_t *list_init(void);
  103. void list_deinit(list_t *list);
  104. bool list_clean(list_t *list);
  105. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  106. bool list_delete(list_t *list, size_t index);
  107. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  108. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  109. bool list_append(list_t *list, const void *data, size_t size);
  110. size_t list_size(const list_t *list);
  111. bool list_empty(const list_t *list);

  112. #endif
  113. sh-5.1$ cat list.c
  114. #include "list.h"
  115. #include <stdlib.h>
  116. #include <memory.h>

  117. list_t *list_init(void) {
  118.     list_t *list = malloc(sizeof(*list));
  119.     if(!list) return NULL;
  120.     list->head = NULL;
  121.     list->size = 0;
  122.     return list;
  123. }

  124. void list_deinit(list_t *list) {
  125.     if(!list) return;
  126.     list_clean(list);
  127.     free(list);
  128. }

  129. bool list_clean(list_t *list) {
  130.     if(!list) return false;
  131.     while(!list_empty(list)) if(!list_delete(list, 0)) return false;
  132.     return true;
  133. }

  134. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  135.     if(!list) return false;
  136.     if(list_size(list) < index) return false;
  137.     if(!data) return false;
  138.     struct list_node_tag **current = &list->head;
  139.     while(index--) current = &(*current)->next;
  140.     struct list_node_tag *node = malloc(sizeof(*node));
  141.     if(!node) return false;
  142.     node->data = malloc(size);
  143.     if(!node->data) {free(node); return false;}
  144.     memcpy(node->data, data, size);
  145.     node->size = size;
  146.     node->next = *current;
  147.     *current = node;
  148.     ++list->size;
  149.     return true;
  150. }

  151. bool list_delete(list_t *list, size_t index) {
  152.     if(!list) return false;
  153.     if(list_size(list) <= index) return false;
  154.     struct list_node_tag **current = &list->head;
  155.     while(index--) current = &(*current)->next;
  156.     struct list_node_tag *temp = *current;
  157.     *current = temp->next;
  158.     free(temp->data); free(temp);
  159.     --list->size;
  160.     return true;
  161. }

  162. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  163.     if(!list) return false;
  164.     if(list_size(list) <= index) return false;
  165.     if(!data) return false;
  166.     struct list_node_tag *const *current = &list->head;
  167.     while(index--) current = &(*current)->next;
  168.     struct list_node_tag *temp = *current;
  169.     if(temp->size > size) return false;
  170.     memcpy(data, temp->data, temp->size);
  171.     return true;
  172. }

  173. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  174.     bool res = list_delete(list, index);
  175.     return res ? list_insert(list, index, data, size) : res;
  176. }

  177. bool list_append(list_t *list, const void *data, size_t size) {
  178.     if(!list) return false;
  179.     return list_insert(list, list_size(list), data, size);
  180. }

  181. size_t list_size(const list_t *list) {
  182.     if(!list) return 0;
  183.     return list->size;
  184. }

  185. bool list_empty(const list_t *list) {
  186.     if(!list) return true;
  187.     return list_size(list) == 0;
  188. }
  189. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 17:03:38 From FishC Mobile | 显示全部楼层
大佬能不能写得再简单些啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 17:03:53 From FishC Mobile | 显示全部楼层
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-22 17:06:53 | 显示全部楼层
啊啊啊空空 发表于 2022-11-22 17:03
大佬能不能写得再简单些啊

这代码哪里复杂了,你哪里看不懂?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 04:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表