鱼C论坛

 找回密码
 立即注册
查看: 2366|回复: 8

[已解决]求大佬教教我第五题怎么做

[复制链接]
发表于 2022-10-31 23:52:55 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 啊啊啊空空 于 2022-11-3 12:40 编辑

题目二:先做一菜单,菜单格式如下:
*********
请选择运算符:
加法运算,请按1;
减法运算,请按2;
乘法运算,请按3;
除法运算,请按4;
*****
程序要求:先显示菜单,客户选择1-4中任意一个数字,就做相应的计算。然后要求客户输入两个数,运算出结果。比如客户选择了...
在题目二的基础上进行改进。程序要求:先显示菜单,客户选择了数字,就做相应的计算。然后系统随机产生两个整数,列出式子,要求客户写出答案,最后系统判断客户答题是否正确,正确显示“你很棒,加油!”,错误显示“很遗憾!”。比如客户选择了2,系统就随机产生一个式子(例23-12=),然后要求客户做减法。                                                    在题目三的基础上进行改进。客户做完一题后,询问客户是否继续答题(继续请输入Y,退出请输入N)。如客户选择继续答题,就重复显题目四,否则退出。
即用户可以循环做答,直到其输入N.
在题目四的基础上进行改进。客户做完多道题目后,可以查询最近十道题的答题情况(如没有做到十道题,就显示全部的题目)。查询显示结果的格式如下:
2*3=6正确
2*4=7错误
提示:要用到数组
最佳答案
2022-11-1 19:15:21
像这样,写一个容器来存储这些表达式

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>

  8. typedef struct {
  9.     int a, b;
  10.     char op;
  11.     int result;
  12. } exp_t;

  13. int calc(int a, char op, int b) {
  14.     switch(op) {
  15.         case '+': return a + b;
  16.         case '-': return a - b;
  17.         case '*': return a * b;
  18.         case '/': return a / b;
  19.     }
  20.     return 0;
  21. }

  22. void output(const list_t *list, size_t index) {
  23.     if(index >= list_size(list)) return;
  24.     output(list, index + 1);
  25.     exp_t e; list_get(list, index, &e, sizeof(e));
  26.     printf("%d %c %d = %d\n", e.a, e.op, e.b, e.result);
  27. }

  28. int main(void) {
  29.     srand(time(NULL));
  30.     list_t *list = list_init();
  31.     for(size_t i = 0; i < 10; ++i) {
  32.         exp_t e;
  33.         e.a = rand() % 100;
  34.         e.b = rand() % 100;
  35.         e.a = rand() % 2 ? e.a : -e.a;
  36.         e.b = rand() % 2 ? e.b : -e.b;
  37.         char op[] = {'+', '-', '*', '/'};
  38.         e.op = op[rand() % 4];
  39.         e.op = e.op == '/' && e.b == 0 ? op[0] : e.op;
  40.         e.result = calc(e.a, e.op, e.b);
  41.         list_append(list, &e, sizeof(e));
  42.     }
  43.     output(list, 0);
  44.     list_deinit(list);
  45.     return 0;
  46. }
  47. sh-5.1$ cat list.h
  48. #ifndef _LIST_H_
  49. #define _LIST_H_

  50. #include <stddef.h>
  51. #include <stdbool.h>

  52. struct list_node_tag {
  53.     void *data; size_t size;
  54.     struct list_node_tag *next;
  55. };

  56. typedef struct {
  57.     struct list_node_tag *head;
  58.     size_t size;
  59. } list_t;

  60. list_t *list_init(void);
  61. void list_deinit(list_t *list);
  62. bool list_clean(list_t *list);
  63. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  64. bool list_delete(list_t *list, size_t index);
  65. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  66. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  67. bool list_append(list_t *list, const void *data, size_t size);
  68. size_t list_size(const list_t *list);
  69. bool list_empty(const list_t *list);

  70. #endif
  71. sh-5.1$ cat list.c
  72. #include "list.h"
  73. #include <stdlib.h>
  74. #include <memory.h>

  75. list_t *list_init(void) {
  76.     list_t *list = malloc(sizeof(*list));
  77.     if(!list) return NULL;
  78.     list->head = NULL;
  79.     list->size = 0;
  80.     return list;
  81. }

  82. void list_deinit(list_t *list) {
  83.     if(!list) return;
  84.     list_clean(list);
  85.     free(list);
  86. }

  87. bool list_clean(list_t *list) {
  88.     if(!list) return false;
  89.     while(!list_empty(list)) list_delete(list, 0);
  90.     return true;
  91. }

  92. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  93.     if(!list) return false;
  94.     if(list_size(list) < index) return false;
  95.     if(!data) return false;
  96.     struct list_node_tag **current = &list->head;
  97.     while(index--) current = &(*current)->next;
  98.     struct list_node_tag *node = malloc(sizeof(*node));
  99.     if(!node) return false;
  100.     node->data = malloc(size);
  101.     if(!node->data) {free(node); return false;}
  102.     memcpy(node->data, data, size);
  103.     node->size = size;
  104.     node->next = *current;
  105.     *current = node;
  106.     ++list->size;
  107.     return true;
  108. }

  109. bool list_delete(list_t *list, size_t index) {
  110.     if(!list) return false;
  111.     if(list_size(list) <= index) return false;
  112.     struct list_node_tag **current = &list->head;
  113.     while(index--) current = &(*current)->next;
  114.     struct list_node_tag *temp = *current;
  115.     *current = temp->next;
  116.     free(temp->data); free(temp);
  117.     --list->size;
  118.     return true;
  119. }

  120. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  121.     if(!list) return false;
  122.     if(list_size(list) <= index) return false;
  123.     if(!data) return false;
  124.     struct list_node_tag *const *current = &list->head;
  125.     while(index--) current = &(*current)->next;
  126.     struct list_node_tag *temp = *current;
  127.     if(temp->size > size) return false;
  128.     memcpy(data, temp->data, temp->size);
  129.     return true;
  130. }

  131. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  132.     bool res = list_delete(list, index);
  133.     return res ? list_insert(list, index, data, size) : res;
  134. }

  135. bool list_append(list_t *list, const void *data, size_t size) {
  136.     if(!list) return false;
  137.     return list_insert(list, list_size(list), data, size);
  138. }

  139. size_t list_size(const list_t *list) {
  140.     if(!list) return 0;
  141.     return list->size;
  142. }

  143. bool list_empty(const list_t *list) {
  144.     if(!list) return true;
  145.     return list_size(list) == 0;
  146. }
  147. sh-5.1$ gcc -g -Wall -o main main.c list.c
  148. sh-5.1$ ls
  149. list.c        list.h        main  main.c
  150. sh-5.1$ ./main
  151. 23 - -77 = 100
  152. 37 * -58 = -2146
  153. 94 * 19 = 1786
  154. 7 - -24 = 31
  155. 51 * 99 = 5049
  156. 68 + -48 = 20
  157. -95 * -80 = 7600
  158. -5 + -33 = -38
  159. 15 * 18 = 270
  160. -93 - 95 = -188
  161. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-1 00:00:22 | 显示全部楼层
就是按照题目要求做么
先做一个菜单,菜单会做不?先把菜单做出来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-1 07:51:19 From FishC Mobile | 显示全部楼层
啊这,我说第五题那个,不是前面的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-1 08:11:58 | 显示全部楼层
啊啊啊空空 发表于 2022-11-1 07:51
啊这,我说第五题那个,不是前面的

可以通过一个队列储存
队列的使用:
https://www.cnblogs.com/xzxl/p/7266370.html
https://blog.csdn.net/zichen_ziqi/article/details/80819939
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-1 09:20:41 From FishC Mobile | 显示全部楼层
要去是用c语言来做
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-1 09:48:04 | 显示全部楼层

那就自己写一个容器,其实链表就可以,队列也行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-1 19:15:21 | 显示全部楼层    本楼为最佳答案   
像这样,写一个容器来存储这些表达式

  1. sh-5.1$ ls
  2. list.c        list.h        main.c
  3. sh-5.1$ cat main.c
  4. #include "list.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>

  8. typedef struct {
  9.     int a, b;
  10.     char op;
  11.     int result;
  12. } exp_t;

  13. int calc(int a, char op, int b) {
  14.     switch(op) {
  15.         case '+': return a + b;
  16.         case '-': return a - b;
  17.         case '*': return a * b;
  18.         case '/': return a / b;
  19.     }
  20.     return 0;
  21. }

  22. void output(const list_t *list, size_t index) {
  23.     if(index >= list_size(list)) return;
  24.     output(list, index + 1);
  25.     exp_t e; list_get(list, index, &e, sizeof(e));
  26.     printf("%d %c %d = %d\n", e.a, e.op, e.b, e.result);
  27. }

  28. int main(void) {
  29.     srand(time(NULL));
  30.     list_t *list = list_init();
  31.     for(size_t i = 0; i < 10; ++i) {
  32.         exp_t e;
  33.         e.a = rand() % 100;
  34.         e.b = rand() % 100;
  35.         e.a = rand() % 2 ? e.a : -e.a;
  36.         e.b = rand() % 2 ? e.b : -e.b;
  37.         char op[] = {'+', '-', '*', '/'};
  38.         e.op = op[rand() % 4];
  39.         e.op = e.op == '/' && e.b == 0 ? op[0] : e.op;
  40.         e.result = calc(e.a, e.op, e.b);
  41.         list_append(list, &e, sizeof(e));
  42.     }
  43.     output(list, 0);
  44.     list_deinit(list);
  45.     return 0;
  46. }
  47. sh-5.1$ cat list.h
  48. #ifndef _LIST_H_
  49. #define _LIST_H_

  50. #include <stddef.h>
  51. #include <stdbool.h>

  52. struct list_node_tag {
  53.     void *data; size_t size;
  54.     struct list_node_tag *next;
  55. };

  56. typedef struct {
  57.     struct list_node_tag *head;
  58.     size_t size;
  59. } list_t;

  60. list_t *list_init(void);
  61. void list_deinit(list_t *list);
  62. bool list_clean(list_t *list);
  63. bool list_insert(list_t *list, size_t index, const void *data, size_t size);
  64. bool list_delete(list_t *list, size_t index);
  65. bool list_get(const list_t *list, size_t index, void *data, size_t size);
  66. bool list_set(list_t *list, size_t index, const void *data, size_t size);
  67. bool list_append(list_t *list, const void *data, size_t size);
  68. size_t list_size(const list_t *list);
  69. bool list_empty(const list_t *list);

  70. #endif
  71. sh-5.1$ cat list.c
  72. #include "list.h"
  73. #include <stdlib.h>
  74. #include <memory.h>

  75. list_t *list_init(void) {
  76.     list_t *list = malloc(sizeof(*list));
  77.     if(!list) return NULL;
  78.     list->head = NULL;
  79.     list->size = 0;
  80.     return list;
  81. }

  82. void list_deinit(list_t *list) {
  83.     if(!list) return;
  84.     list_clean(list);
  85.     free(list);
  86. }

  87. bool list_clean(list_t *list) {
  88.     if(!list) return false;
  89.     while(!list_empty(list)) list_delete(list, 0);
  90.     return true;
  91. }

  92. bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
  93.     if(!list) return false;
  94.     if(list_size(list) < index) return false;
  95.     if(!data) return false;
  96.     struct list_node_tag **current = &list->head;
  97.     while(index--) current = &(*current)->next;
  98.     struct list_node_tag *node = malloc(sizeof(*node));
  99.     if(!node) return false;
  100.     node->data = malloc(size);
  101.     if(!node->data) {free(node); return false;}
  102.     memcpy(node->data, data, size);
  103.     node->size = size;
  104.     node->next = *current;
  105.     *current = node;
  106.     ++list->size;
  107.     return true;
  108. }

  109. bool list_delete(list_t *list, size_t index) {
  110.     if(!list) return false;
  111.     if(list_size(list) <= index) return false;
  112.     struct list_node_tag **current = &list->head;
  113.     while(index--) current = &(*current)->next;
  114.     struct list_node_tag *temp = *current;
  115.     *current = temp->next;
  116.     free(temp->data); free(temp);
  117.     --list->size;
  118.     return true;
  119. }

  120. bool list_get(const list_t *list, size_t index, void *data, size_t size) {
  121.     if(!list) return false;
  122.     if(list_size(list) <= index) return false;
  123.     if(!data) return false;
  124.     struct list_node_tag *const *current = &list->head;
  125.     while(index--) current = &(*current)->next;
  126.     struct list_node_tag *temp = *current;
  127.     if(temp->size > size) return false;
  128.     memcpy(data, temp->data, temp->size);
  129.     return true;
  130. }

  131. bool list_set(list_t *list, size_t index, const void *data, size_t size) {
  132.     bool res = list_delete(list, index);
  133.     return res ? list_insert(list, index, data, size) : res;
  134. }

  135. bool list_append(list_t *list, const void *data, size_t size) {
  136.     if(!list) return false;
  137.     return list_insert(list, list_size(list), data, size);
  138. }

  139. size_t list_size(const list_t *list) {
  140.     if(!list) return 0;
  141.     return list->size;
  142. }

  143. bool list_empty(const list_t *list) {
  144.     if(!list) return true;
  145.     return list_size(list) == 0;
  146. }
  147. sh-5.1$ gcc -g -Wall -o main main.c list.c
  148. sh-5.1$ ls
  149. list.c        list.h        main  main.c
  150. sh-5.1$ ./main
  151. 23 - -77 = 100
  152. 37 * -58 = -2146
  153. 94 * 19 = 1786
  154. 7 - -24 = 31
  155. 51 * 99 = 5049
  156. 68 + -48 = 20
  157. -95 * -80 = 7600
  158. -5 + -33 = -38
  159. 15 * 18 = 270
  160. -93 - 95 = -188
  161. sh-5.1$
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-2 20:26:36 From FishC Mobile | 显示全部楼层
可是要用到数组来解决唉,求大佬教教我
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-2 20:28:27 From FishC Mobile | 显示全部楼层
能不能写一个简化一点的,用C语言的数组完成,求大佬写一下,感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 05:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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