一、菜单程序函数调用图
二、设计思路和遇到的问题
- 设计思路:这次设计需要给原先的三种口算题加上结果,所以使用switch语句,分别列出产生的随机数使用不同算术运算符时的情况就可以了。
- 遇到的问题:在处理一年级和二年级题目时,还比较简单,但三年级题目有四种不同算术运算符的组合,有点复杂,查了一些资料,想了一些办法后选择了自己最容易理解的方法来解决问题。
三、 程序结果截图
四、程序代码
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
void menu() {
printf("\n操作列表:\n");
printf("1)一年级 2)二年级 3)三年级 \n4)帮助 5)退出程序\n");
printf("请输入操作>");
}
void help() {
printf("帮助信息:\n您需要输入命令代号来进行操作,且\n");
printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n");
}
void error() {
printf("Error!!!\n错误操作指令,请重新输入\n\n");
}
void Operation1() {
int i,j;
printf("现在是一年级题目:\n");
printf("请输入生成个数>");
scanf("%d",&j);
printf("<执行操作:\n\n");
for(i=1; i<=j; i++) {
int num1 = rand() % 11;
int num2 = rand() % 11;
int a = rand() % 2+1;
switch (a) {
case 1:
printf(" %d + %d = %2d \n",num1,num2,num1+num2);
break;
case 2:
printf(" %d - %d = %2d \n",num1,num2,num1-num2);
break;
}
}
}
void Operation2() {
int i,j;
printf("现在是二年级题目:\n");
printf("请输入生成个数>");
scanf("%d",&j);
printf("<执行操作:\n\n");
for(i=1; i<=j; i++) {
int num1 = rand() % 101;
int num2 = rand() % 101;
int num3 = rand() % 100+1;
int a = rand() % 2 + 1;
switch (a) {
case 1:
printf(" %d * %d = %2d \n",num1,num2,num1*num2);
break;
case 2:
printf(" %d / %d = %g \n",num1,num3,1.0*num1/num3);
break;
}
}
}
void Operation3() {
int i,j;
printf("现在是三年级题目:\n");
printf("请输入生成个数>");
scanf("%d",&j);
printf("<执行操作:\n\n");
for(i=1; i<=j; i++) {
int num1 = rand() % 101;
int num2 = rand() % 101;
int num3 = rand() % 101;
int num4 = rand() % 100+1;
int num5 = rand() % 100+1;
int a = rand() % 16+1;
switch(a) {
case 1:
printf(" %2d + %2d + %2d = %d\n ",num1,num2,num3,num1+num2+num3);
break;
case 2:
printf(" %2d - %2d - %2d = %d\n ",num1,num2,num3,num1-num2-num3);
break;
case 3:
printf(" %2d * %2d * %2d = %d\n ",num1,num2,num3,num1*num2*num3);
break;
case 4:
printf(" %2d / %2d / %2d = %g\n ",num1,num4,num5,1.0*num1/num4/num5);
break;
case 5:
break;
printf(" %2d + %2d - %2d = %d\n ",num1,num2,num3,num1+num2-num3);
break;
case 6:
printf(" %2d - %2d + %2d = %d\n ",num1,num2,num3,num1-num2+num3);
break;
case 7:
break;
printf(" %2d * %2d / %2d = %g\n ",num1,num2,num4,num1*num2/1.0*num4);
break;
case 8:
printf(" %2d / %2d * %2d = %g\n ",num1,num4,num3,num1/1.0*num4*num3);
break;
case 9:
break;
printf(" %2d + %2d * %2d = %d\n ",num1,num2,num3,num1+num2*num3);
break;
case 10:
break;
printf(" %2d * %2d + %2d = %d\n ",num1,num2,num3,num1*num2+num3);
break;
case 11:
break;
printf(" %2d + %2d / %2d = %g\n ",num1,num2,num4,num1+1.0*num2/num4);
break;
case 12:
break;
printf(" %2d / %2d + %2d = %g\n ",num1,num4,num3,1.0*num1/num4+num3);
break;
case 13:
break;
printf(" %2d - %2d * %2d = %d\n ",num1,num2,num3,num1-num2*num3);
break;
case 14:
break;
printf(" %2d * %2d - %2d = %d\n ",num1,num2,num3,num1*num2-num3);
break;
case 15:
printf(" %2d - %2d / %2d = %g\n ",num1,num2,num4,num1-1.0*num2/num4);
break;
case 16:
printf(" %2d / %2d - %2d = %g\n ",num1,num4,num3,1.0*num1/num4-num3);
break;
}
}
}
int main() {
srand((int)time(0));
int Grade;
printf("========口算生成器========\n欢迎使用口算生成器:\n\n");
help();
menu();
while(Grade!=5) {
scanf("%d",&Grade);
printf("<执行操作:\n\n");
switch(Grade) {
case 1:
Operation1();
break;
case 2:
Operation2();
break;
case 3:
Operation3();
break;
case 4:
help();
break;
case 5:
printf("程序结束,欢迎下次使用\n任意键结束......\n");
break;
default:
error();
break;
}
if(Grade!=5) menu();
}
}
五、Gitee上传截图与链接
七、主页链接
https://gitee.com/shenxinglou