|
发表于 2021-4-22 22:52:19
|
显示全部楼层
本帖最后由 yuxijian2020 于 2021-4-22 23:00 编辑
我来了 我就是工具人
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include <math.h>
- typedef int BOOL;
- #define TRUE 1
- #define FALSE 0
- #define MAX_LEN 50
- #define MAX_INT 2147483647
- //运算符数组
- const char ops[4] = { '+', '-', '*', '/' };
- //提示数组
- const char* correct[4] = { "Very good!", "Excellent!", "Nice work!", "Keep up the good work!" };
- const char* wrong[4] = { "No. Please try again.", "Wrong. Try once more.", "No. Don’t give up!", "Not correct. Keep trying." };
- typedef struct _Formula
- {
- int first; //第一个数
- char operator; //操作符
- int second; //第二个数
- int result; //结果
- double fResult; //除法结果
- char formula[MAX_LEN]; //整个公式字符串
- } Formula;
- //生成运算公式,并计算结果保存在结构体中
- Formula* CreateFormula()
- {
- //种子
- static unsigned int seed = 1;
- int i = 0;
- Formula* result = (Formula*)malloc(sizeof(Formula));
- if (result == NULL)
- printf_s("生成运算失败...\n");
- memset(result, 0, sizeof(Formula));
- //设置随机数种子
- srand(seed + time(0));
- //种子变化
- seed++;
- result->first = rand() % 10;
- result->second = rand() % 10;
- i = rand() % 4;
- result->operator = ops[i];
- switch (i)
- {
- case 0:
- result->result = result->first + result->second;
- break;
- case 1:
- result->result = result->first - result->second;
- break;
- case 2:
- result->result = result->first * result->second;
- break;
- case 3:
- //除法时,确保除数不为0
- while (result->second == 0)
- result->second = rand() % 10;
- result->fResult = (double)result->first / (double)result->second;
- break;
- default:
- break;
- }
- sprintf_s(result->formula, MAX_LEN, "%d %c %d = ", result->first, result->operator, result->second);
- return result;
- }
- //任务一
- void TaskOne()
- {
- Formula* task = CreateFormula();
- double result = MAX_INT;
- printf_s("请输入下列公式的结果:\n");
- while (result != task->result)
- {
- printf_s("%s ", task->formula);
- scanf_s("%lf%*c", &result);
- if (task->operator != '/' && result != task->result)
- printf_s("答案错误请重新输入!\n");
- else if (task->operator == '/' && fabs(result - task->fResult) > 1e-4)//除法计算出4位小数即可
- printf_s("答案错误请重新输入!\n");
- }
- printf_s("答案正确!\n");
- free(task);
- }
- //任务二
- void TaskTwo()
- {
- Formula* task = CreateFormula();
- double result = MAX_INT;
- int count = 3;
- printf_s("请输入下列公式的结果:\n");
- printf_s("你有3次尝试的机会,请好好把握!\n");
- while (count > 0 && result != task->result)
- {
- printf_s("%s ", task->formula);
- scanf_s("%lf%*c", &result);
- count--;
- if (task->operator != '/' && result != task->result && count > 0)
- printf_s("答案错误请重新输入!\n");
- else if(task->operator == '/' && fabs(result - task->fResult) > 1e-4 && count > 0)
- printf_s("答案错误请重新输入!\n");
- }
- if (result != task->result)
- printf_s("很遗憾,3次尝试都失败了!游戏结束!\n");
- else
- printf_s("回答正确!\n");
- free(task);
- }
- //任务三
- void TaskThree()
- {
- Formula* task[10];
- double result;
- for (int i = 0; i < 10; i++)
- task[i] = CreateFormula();
- int rightNum = 0;
- printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");
- for (int i = 0; i < 10; i++)
- {
- printf_s("(%d) %s", i + 1, task[i]->formula);
- scanf_s("%lf%*c", &result);
- if (task[i]->operator != '/' && result == task[i]->result)
- rightNum += 1;
- else if(task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
- rightNum += 1;
- }
- //10题 每题10分
- printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
- //释放空间
- for (int i = 0; i < 10; i++)
- free(task[i]);
- }
- //任务四
- int TaskFour()
- {
- Formula* task[10];
- double result;
- for (int i = 0; i < 10; i++)
- task[i] = CreateFormula();
- int rightNum = 0;
- printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");
- for (int i = 0; i < 10; i++)
- {
- printf_s("(%d) %s", i + 1, task[i]->formula);
- scanf_s("%lf%*c", &result);
- if (task[i]->operator != '/' && result == task[i]->result)
- {
- printf_s("答案正确!\n");
- rightNum += 1;
- }
- else if (task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
- {
- printf_s("答案正确!\n");
- rightNum += 1;
- }
- else
- {
- printf_s("答案错误!\n");
- }
- }
- //10题 每题10分
- printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
- //释放空间
- for (int i = 0; i < 10; i++)
- free(task[i]);
- return rightNum * 10;
- }
- //任务五
- void TaskFive()
- {
- while (TaskFour() < 75)
- {
- printf_s("正确率低于75%%, 请重新做题!\n");
- printf_s("-------------------------------------\n");
- }
- }
- //任务六
- //人机回复 参数为回答正确或错误
- void RobotReply(BOOL isCorrect)
- {
- //种子
- static unsigned int seed = 0;
- //设置随机数种子
- srand(seed + time(0));
- int i = rand() % 4;
- if (isCorrect)
- printf_s("%s\n", correct[i]);
- else
- printf_s("%s\n", wrong[i]);
- //种子变化
- seed++;
- }
- //设置问题并要求用户输入回复 -- 基本同TaskFour 只是改了回复
- int MakeQuestion()
- {
- Formula* task[10];
- double result;
- for (int i = 0; i < 10; i++)
- task[i] = CreateFormula();
- int rightNum = 0;
- printf_s("一共10道题目,只有一次机会!请谨慎填写!\n");
- for (int i = 0; i < 10; i++)
- {
- printf_s("(%d) %s", i + 1, task[i]->formula);
- scanf_s("%lf%*c", &result);
- if (task[i]->operator != '/' && result == task[i]->result)
- {
- RobotReply(TRUE);
- rightNum += 1;
- }
- else if (task[i]->operator == '/' && fabs(result - task[i]->fResult) < 1e-4)
- {
- RobotReply(TRUE);
- rightNum += 1;
- }
- else
- {
- RobotReply(FALSE);
- }
- }
- //10题 每题10分
- printf_s(" 你的总分为: %d\n 正确率为: %d%%", rightNum * 10, rightNum * 10);
- //释放空间
- for (int i = 0; i < 10; i++)
- free(task[i]);
- return rightNum * 10;
- }
- void TaskSix()
- {
- while (MakeQuestion() < 75)
- {
- printf_s("正确率低于75%%, 请重新做题!\n");
- printf_s("-------------------------------------\n");
- }
- }
- //自定义功能
- void TaskCustom()
- {
- //自定义功能自己写吧...
- }
- int main()
- {
- //TaskOne();
- //TaskTwo();
- //TaskThree();
- //TaskFour();
- //TaskFive();
- TaskSix();
- return 0;
- }
复制代码 |
|