1 作业头:
这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/CST2019-2/ |
---|---|
这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10261 |
这个作业的目标 | 使用rand()函数生成随机题目,并计算 |
作业正文 | 如下 |
其他参考文献 | cpp |
2.2 设计思路和遇到的问题
在第三次作业的基础上将rand()决定符号改成了决定计算,并将各种情况列举出来。
2.3 程序结果截图
2.4 程序代码
#include
#include
int help(int a) {
printf("欢迎使用口算生成器 :\n \n \n帮助信息\n您需要输入命令代号来进行操作, 且\n");
printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n\n");
printf("操作列表:\n1)一年级 2)二年级 3)三年级\n4)帮助 5)退出程序\n ");
return 0;
}
int fh1() {
int a = rand() % 9, b = rand() % 9, n = rand() % 2;
if (n == 1) printf("%d + %d = %d\n", a, b, a + b);
else printf("%d - %d = %d\n", a, b, a - b);
return 0;
}
int fh2() {
double a = rand() % 10, b = (rand() % 9) + 1, n = rand() % 2;
if (n == 1) printf("%g * %g = %g\n", a, b, a * b);
else printf("%g / %g = %g\n", a, b, a / b);
return 0;
}
int fh3() {
double a = rand() % 9, b = (rand() % 9) + 1;
int n = rand() % 3 + 1;
if (n == 1) {
double c = a * b, d = (rand() % 9) + 1; int i = rand() % 3 + 1;
if (i == 1) { double c = a * b, d = (rand() % 9) + 1; printf("%g / %g + %g = %g\n", a, b, d, c + d); }
if (i == 2) { double c = a * b, d = (rand() % 9) + 1; printf("%g / %g - %g = %g\n", a, b, d, c - d); }
if (i == 3) { double c = a * b, d = (rand() % 9) + 1; printf("%g / %g * %g = %g\n", a, b, d, c * d); }
if (i == 4) { double c = a * b, d = (rand() % 9) + 1; printf("%g / %g / %g = %g\n", a, b, d, c / d); }
}
if (n == 2) {
double c = a / b, d = (rand() % 9) + 1; int i = rand() % 3 + 1;
if (i == 1) { double c = a / b, d = (rand() % 9) + 1; printf("%g / %g + %g = %g\n", a, b, d, c + d); }
if (i == 2) { double c = a / b, d = (rand() % 9) + 1; printf("%g / %g - %g = %g\n", a, b, d, c - d); }
if (i == 3) { double c = a / b, d = (rand() % 9) + 1; printf("%g / %g * %g = %g\n", a, b, d, c * d); }
if (i == 4) { double c = a / b, d = (rand() % 9) + 1; printf("%g / %g / %g = %g\n", a, b, d, c / d); }
}
if (n == 3) {
int i = rand() % 3 + 1;
if (i == 1) { double c = a + b, d = (rand() % 9) + 1; printf("%g + %g + %g = %g\n", a, b, d, c + d); }
if (i == 2) { double c = a + b, d = (rand() % 9) + 1; printf("%g + %g - %g = %g\n", a, b, d, c - d); }
if (i == 3) { double d = (rand() % 9) + 1; double c = b * d; printf("%g + %g * %g = %g\n", a, b, d, a + c); }
if (i == 4) { double d = (rand() % 9) + 1; double c = b / d; printf("%g + %g / %g = %g\n", a, b, d, a + c); }
}
if (n == 4) {
int i = rand() % 3 + 1;
if (i == 1) { double c = a - b, d = (rand() % 9) + 1; printf("%g - %g + %g = %g\n", a, b, d, c + d); }
if (i == 2) { double c = a - b, d = (rand() % 9) + 1; printf("%g - %g - %g = %g\n", a, b, d, c - d); }
if (i == 3) { double d = (rand() % 9) + 1; double c = b * d; printf("%g - %g * %g = %g\n", a, b, d, a - c); }
if (i == 4) { double d = (rand() % 9) + 1; double c = b / d; printf("%g - %g / %g = %g\n", a, b, d, a - c); }
}
return 0;
}
int menu(int a) {
int i;
printf("请输入生成题目的个数:");
scanf_s("%d", &i);
if (a == 1) for (i; i--; fh1());
else
if (a == 2) for (int i = 5; i--; fh2());
else for (i; i--; fh3());
return 0;
}
int main()
{
printf("========== 口算生成器 ==========\n");
printf("欢迎使用口算生成器 :\n \n \n帮助信息\n您需要输入命令代号来进行操作, 且\n");
printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n\n");
printf("操作列表:\n1)一年级 2)二年级 3)三年级\n4)帮助 5)退出程序\n ");
sr:printf("请输入操作 >\n\n");
int a;
scanf_s("%d", &a);
printf("开始执行%d命令\n", a);
switch (a) {
case 1:case 2:case 3:menu(a); goto sr;
case 4:help(a); goto sr;
case 5:printf("程序结束, 欢迎下次使用\n任意键结束……"); return 0;
default:printf("Error!!!\n错误操作指令, 请重新输入"); goto sr;
}
}
https://gitee.com/zhuoyao_cham/czy20199256/tree/master/fuck