1 作业头:
这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/CST2019-2 |
---|---|
这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10279 |
这个作业的目标 | 使用函数、Switch语句来完成菜单程序 |
作业正文 | https://i-beta.cnblogs.com/posts/edit |
参考文献 | https://www.runoob.com/cprogramming/c-function-rand.html https://zhidao.baidu.com/question/271899014.html |
2 设计思路和遇到的问题
1.在三年级的题中一直想不到什么好方法,就把可能的情况全部printf了。
2.在除法中0/会出现不等于0的情况,所以加上(double)a/b。
3 程序结果截图
4 程序代码
#include
#include
#include
void first(int opt) {
int i,n,a,b;
char op[] = { '+','-' };
srand(time(NULL));
printf("/*----- 一年级 -----*/\n");
printf("<请输入生成个数> ");
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
a = rand() % 10;
b = rand() % 10;
switch (op[rand() % 2])
{
case '+': printf("%d + %d = %d\n", a, b, a + b); break;
case '-': printf("%d - %d = %d\n", a, b, a - b); break;
}
}
}
void second(int opt) {
int i,n,a,b;
char op[] = { '*','/' };
srand(time(NULL));
printf("/*----- 二年级 -----*/\n");
printf("<请输入生成个数> ");
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
a = rand() % 10;
b = rand() % 9 + 1;
switch (op[rand() % 2])
{
case '*': printf("%d * %d = %d\n", a, b, a * b); break;
case '/': printf("%d / %d = %g\n", a, b, (double)a / b); break;
}
}
}
void third(int opt) {
int i,n,a,b,c;
char op[] = { '*','/','+','-' };
srand(time(NULL));
printf("/*----- 三年级 -----*/\n");
printf("<请输入生成个数> ");
scanf_s("%d", &n);
for (i = 1; i <= n; i++)
{
a = rand() % 100;
b = rand() % 9 + 1;
c = rand() % 9 + 2;
switch (rand() % 16 + 1) //16种情况/
{
case 1: printf("%d + %d + %d = %d\n", a, b, c, a + b + c); break;
case 2: printf("%d + %d - %d = %d\n", a, b, c, a + b - c); break;
case 3: printf("%d + %d * %d = %d\n", a, b, c, a + b * c); break;
case 4: printf("%d + %d / %d = %g\n", a, b, c, (double)a + b / c); break;
case 5: printf("%d - %d + %d = %d\n", a, b, c, a - b + c); break;
case 6: printf("%d - %d - %d = %d\n", a, b, c, a - b - c); break;
case 7: printf("%d - %d * %d = %d\n", a, b, c, a - b * c); break;
case 8: printf("%d - %d / %d = %g\n", a, b, c, (double)a - b / c); break;
case 9: printf("%d * %d + %d = %d\n", a, b, c, a * b + c); break;
case 10: printf("%d * %d - %d = %d\n", a, b, c, a * b - c); break;
case 11: printf("%d * %d * %d = %d\n", a, b, c, a * b * c); break;
case 12: printf("%d * %d / %d = %g\n", a, b, c, (double)a * b / c); break;
case 13: printf("%d / %d + %d = %g\n", a, b, c, (double)a / b + c); break;
case 14: printf("%d / %d - %d = %g\n", a, b, c, (double)a / b - c); break;
case 15: printf("%d / %d * %d = %g\n", a, b, c, (double)a / b * c); break;
case 16: printf("%d / %d / %d = %g\n", a, b, c, (double)a / b / c); break;
}
}
}
void help(int opt) {
printf("帮助信息\n您需要输入命令代号来进行操作, 且\n");
printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n\n");
}
void end(int opt) {
printf("程序结束, 欢迎下次使用,任意键结束......\n\n");
}
void error(int opt) {
printf("Error!!!\n错误操作指令, 请重新输入\n\n\n");
}
int main()
{
int opt = -1;
printf("========== 口算生成器 ==========\n欢迎使用口算生成器 :\n\n");
printf("帮助信息\n您需要输入命令代号来进行操作, 且\n");
printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n\n");
while (opt != 5)
{
printf("操作列表:\n1)一年级 2)二年级 3)三年级\n4)帮助 5)退出程序\n ");
printf("请输入操作 >\n\n");
scanf_s("%d", &opt);
switch (opt) {
case 1:first(opt); break;
case 2:second(opt); break;
case 3:third(opt); break;
case 4:help(opt); break;
case 5:end(opt); break;
default:error(opt); break;
}
}
}
5 Gitee上传截图与链接
6 主页地址
https://gitee.com/feng_yue_long