鱼C论坛

 找回密码
 立即注册
查看: 3334|回复: 3

c语言期中大作业

[复制链接]
发表于 2021-4-22 22:52:19 | 显示全部楼层
本帖最后由 yuxijian2020 于 2021-4-22 23:00 编辑

我来了   我就是工具人
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <math.h>

  6. typedef int                    BOOL;
  7. #define TRUE                    1
  8. #define FALSE                   0

  9. #define MAX_LEN                 50
  10. #define MAX_INT                 2147483647

  11. //运算符数组
  12. const char ops[4] = { '+', '-', '*', '/' };
  13. //提示数组
  14. const char* correct[4] = { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!" };
  15. const char* wrong[4] = { "No. Please try again.", "Wrong. Try once more.", "No. Don’t give up!", "Not correct. Keep trying." };

  16. typedef struct _Formula
  17. {
  18.     int first;                  //第一个数
  19.     char operator;              //操作符
  20.     int second;                 //第二个数
  21.     int result;                 //结果
  22.     double fResult;             //除法结果
  23.     char formula[MAX_LEN];      //整个公式字符串

  24. } Formula;

  25. //生成运算公式,并计算结果保存在结构体中
  26. Formula* CreateFormula()
  27. {
  28.     //种子
  29.     static unsigned int seed = 1;
  30.     int i = 0;
  31.     Formula* result = (Formula*)malloc(sizeof(Formula));
  32.     if (result == NULL)
  33.         printf_s("生成运算失败...\n");

  34.     memset(result, 0, sizeof(Formula));

  35.     //设置随机数种子
  36.     srand(seed + time(0));
  37.     //种子变化
  38.     seed++;

  39.     result->first = rand() % 10;
  40.     result->second = rand() % 10;
  41.     i = rand() % 4;
  42.     result->operator = ops[i];
  43.     switch (i)
  44.     {
  45.     case 0:
  46.         result->result = result->first + result->second;
  47.         break;
  48.     case 1:
  49.         result->result = result->first - result->second;
  50.         break;
  51.     case 2:
  52.         result->result = result->first * result->second;
  53.         break;
  54.     case 3:
  55.         //除法时,确保除数不为0
  56.         while (result->second == 0)
  57.             result->second = rand() % 10;

  58.         result->fResult = (double)result->first / (double)result->second;
  59.         break;
  60.     default:
  61.         break;
  62.     }

  63.     sprintf_s(result->formula, MAX_LEN, "%d %c %d = ", result->first, result->operator, result->second);

  64.     return result;
  65. }
  66. //任务一
  67. void TaskOne()
  68. {
  69.     Formula* task = CreateFormula();
  70.     double result = MAX_INT;
  71.     printf_s("请输入下列公式的结果:\n");

  72.     while (result != task->result)
  73.     {
  74.         printf_s("%s ", task->formula);
  75.         scanf_s("%lf%*c", &result);

  76.         if (task->operator != '/' && result != task->result)
  77.             printf_s("答案错误请重新输入!\n");
  78.         else if (task->operator == '/' && fabs(result - task->fResult) > 1e-4)//除法计算出4位小数即可
  79.             printf_s("答案错误请重新输入!\n");
  80.     }

  81.     printf_s("答案正确!\n");
  82.     free(task);
  83. }
  84. //任务二
  85. void TaskTwo()
  86. {
  87.     Formula* task = CreateFormula();
  88.     double result = MAX_INT;
  89.     int count = 3;
  90.     printf_s("请输入下列公式的结果:\n");
  91.     printf_s("你有3次尝试的机会,请好好把握!\n");

  92.     while (count > 0 && result != task->result)
  93.     {
  94.         printf_s("%s ", task->formula);
  95.         scanf_s("%lf%*c", &result);

  96.         count--;

  97.         if (task->operator != '/' && result != task->result && count > 0)
  98.             printf_s("答案错误请重新输入!\n");
  99.         else if(task->operator == '/' && fabs(result - task->fResult) > 1e-4 && count > 0)
  100.             printf_s("答案错误请重新输入!\n");
  101.     }

  102.     if (result != task->result)
  103.         printf_s("很遗憾,3次尝试都失败了!游戏结束!\n");
  104.     else
  105.         printf_s("回答正确!\n");

  106.     free(task);
  107. }
  108. //任务三
  109. void TaskThree()
  110. {
  111.     Formula* task[10];
  112.     double result;
  113.     for (int i = 0; i < 10; i++)
  114.         task[i] = CreateFormula();

  115.     int rightNum = 0;

  116.     printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");

  117.     for (int i = 0; i < 10; i++)
  118.     {
  119.         printf_s("(%d) %s", i + 1, task[i]->formula);
  120.         scanf_s("%lf%*c", &result);
  121.         if (task[i]->operator != '/' && result == task[i]->result)
  122.             rightNum += 1;
  123.         else if(task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
  124.             rightNum += 1;
  125.     }
  126.     //10题  每题10分
  127.     printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
  128.     //释放空间
  129.     for (int i = 0; i < 10; i++)
  130.         free(task[i]);
  131. }
  132. //任务四
  133. int TaskFour()
  134. {
  135.     Formula* task[10];
  136.     double result;
  137.     for (int i = 0; i < 10; i++)
  138.         task[i] = CreateFormula();

  139.     int rightNum = 0;

  140.     printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");

  141.     for (int i = 0; i < 10; i++)
  142.     {
  143.         printf_s("(%d) %s", i + 1, task[i]->formula);
  144.         scanf_s("%lf%*c", &result);
  145.         if (task[i]->operator != '/' && result == task[i]->result)
  146.         {
  147.             printf_s("答案正确!\n");
  148.             rightNum += 1;
  149.         }
  150.         else if (task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
  151.         {
  152.             printf_s("答案正确!\n");
  153.             rightNum += 1;
  154.         }
  155.         else
  156.         {
  157.             printf_s("答案错误!\n");
  158.         }
  159.     }
  160.     //10题  每题10分
  161.     printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
  162.     //释放空间
  163.     for (int i = 0; i < 10; i++)
  164.         free(task[i]);

  165.     return rightNum * 10;
  166. }
  167. //任务五
  168. void TaskFive()
  169. {
  170.     while (TaskFour() < 75)
  171.     {
  172.         printf_s("正确率低于75%%, 请重新做题!\n");
  173.         printf_s("-------------------------------------\n");
  174.     }
  175. }
  176. //任务六
  177. //人机回复 参数为回答正确或错误
  178. void RobotReply(BOOL isCorrect)
  179. {
  180.     //种子
  181.     static unsigned int seed = 0;
  182.     //设置随机数种子
  183.     srand(seed + time(0));
  184.     int i = rand() % 4;
  185.     if (isCorrect)
  186.         printf_s("%s\n", correct[i]);
  187.     else
  188.         printf_s("%s\n", wrong[i]);

  189.     //种子变化
  190.     seed++;
  191. }
  192. //设置问题并要求用户输入回复 -- 基本同TaskFour  只是改了回复
  193. int MakeQuestion()
  194. {
  195.     Formula* task[10];
  196.     double result;
  197.     for (int i = 0; i < 10; i++)
  198.         task[i] = CreateFormula();

  199.     int rightNum = 0;

  200.     printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");

  201.     for (int i = 0; i < 10; i++)
  202.     {
  203.         printf_s("(%d) %s", i + 1, task[i]->formula);
  204.         scanf_s("%lf%*c", &result);
  205.         if (task[i]->operator != '/' && result == task[i]->result)
  206.         {
  207.             RobotReply(TRUE);
  208.             rightNum += 1;
  209.         }
  210.         else if (task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
  211.         {
  212.             RobotReply(TRUE);
  213.             rightNum += 1;
  214.         }
  215.         else
  216.         {
  217.             RobotReply(FALSE);
  218.         }
  219.     }
  220.     //10题  每题10分
  221.     printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
  222.     //释放空间
  223.     for (int i = 0; i < 10; i++)
  224.         free(task[i]);

  225.     return rightNum * 10;
  226. }

  227. void TaskSix()
  228. {
  229.     while (MakeQuestion() < 75)
  230.     {
  231.         printf_s("正确率低于75%%, 请重新做题!\n");
  232.         printf_s("-------------------------------------\n");
  233.     }
  234. }

  235. //自定义功能
  236. void TaskCustom()
  237. {
  238.     //自定义功能自己写吧...
  239. }

  240. int main()
  241. {
  242.     //TaskOne();
  243.     //TaskTwo();
  244.     //TaskThree();
  245.     //TaskFour();
  246.     //TaskFive();
  247.     TaskSix();

  248.     return 0;
  249. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-21 03:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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