2.2.1作业头
问题 | 答案 |
---|---|
这个作业属于那个课程 | C语言程序设计 |
这个作业1要求在哪里 | https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10279 |
这个作业目标 | 使用函数、Switch语句来完成菜单程序 |
作业正文 | https://www.cnblogs.com/galen123/p/12320451.html |
其他参考文献 | https://baijiahao.baidu.com/s?id=1616367741584583839&wfr=spider&for=pc |
2.2.2 设计思路和遇到的问题
请写上你的心路历程和程序设计思路,并在其中写下你遇到的问题
- 1.srand((unsigned)time(NULL)),srand函数是随机数发生器的初始化函数。原型:void srand(unsigned seed);
- 2.字符型变量如何在表达式中显示,以及算式计算出精确结果并表达出来;
- 3.rand函数的意思C 库函数 int rand(void) 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。
4.用switch case语句配合RAND函数随机选择符号,确保每一种随机算式情况不遗漏;
2.3 程序结果截图
2.2.4 程序代码
#include
#include
#include
void project1(int opt){
int i, n, s1, s2, s3;
char op[] = { '+','-' };
srand((unsigned)time(NULL));
printf("现在是一年级题目:\n请输入生成个数>\n");
scanf_s("%d", &n);
for (i = 1; i <= n; i++) {
s1 = rand() % 10;
s2 = rand() % 10;
switch (op[rand() % 2]) {
case '+': printf("%d + %d = %d\n",s1,s2,s1+s2); break;
case '-': printf("%d - %d = %d\n", s1,s2,s1-s2); break;
}
}
}
void project2(int opt){
int i, n, s1, s2;
char op[] = { '*','/' };
srand((unsigned)time(NULL));
printf("现在是二年级题目:\n请输入生成个数>/\n");
scanf_s("%d", &n);
for (i = 1; i <= n; i++) {
s1 = rand() % 10;
s2 = rand() % 9+1;
switch (op[rand() % 2])
{
case '*': printf("%d * %d = %d\n", s1, s2, s1 * s2); break;
case '/': printf("%d / %d = %g\n", s1, s2, (double)s1/ s2); break;
}
}
}
void project3(int opt) {
int i, n, s1, s2, s3;
double s4;
char op[] = { '*','/','+','-' };
srand((unsigned)time(NULL));
printf("现在是三年级题目:\n请输入生成个数>\n");
scanf_s("%d", &n);
for (i = 1; i <= n; i++) {
s1 = rand() % 101;
s2 = rand() % 100+1;
s3 = rand() % 100+1;
switch (rand() % 16 + 1)
{
case 1: printf("%d + %d + %d = %d\n", s1, s2, s3, s1 + s2 + s3); break;
case 2: printf("%d + %d - %d = %d\n", s1, s2, s3,s1 + s2 - s3); break;
case 3: printf("%d + %d * %d = %d\n", s1, s2, s3, s1 + s2 * s3); break;
case 4: printf("%d + %d / %d = %g\n", s1, s2, s3, (double)s1 + s2 / s3); break;
case 5: printf("%d - %d + %d = %d\n", s1, s2, s3, s1 - s2 + s3); break;
case 6: printf("%d - %d - %d = %d\n", s1, s2, s3, s1 - s2 - s3); break;
case 7: printf("%d - %d * %d = %d\n", s1, s2, s3, s1 - s2 * s3); break;
case 8: printf("%d - %d / %d = %g\n", s1, s2, s3, (double)s1 - s2 / s3); break;
case 9: printf("%d * %d + %d = %d\n", s1, s2, s3, s1 * s2 + s3); break;
case 10: printf("%d * %d - %d = %d\n", s1, s2, s3, s1 * s2 - s3); break;
case 11: printf("%d * %d * %d = %d\n", s1, s2, s3, s1 * s2* s3); break;
case 12: printf("%d * %d / %d = %g\n", s1, s2, s3, (double)s1 * s2 / s3); break;
case 13: printf("%d / %d + %d = %g\n", s1, s2, s3, (double)s1 / s2 + s3); break;
case 14: printf("%d / %d - %d = %g\n", s1, s2, s3, (double)s1 / s2 - s3); break;
case 15: printf("%d / %d * %d = %g\n", s1, s2, s3, (double)s1 / s2 * s3); break;
case 16: printf("%d / %d / %d = %g\n", s1,s2,s3, (double)s1/ s2 / s3); 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:project1(opt); break;
case 2:project2(opt); break;
case 3:project3(opt); break;
case 4:help(opt); break;
case 5:end(opt); break;
default:error(opt); break;
}
}
return 0;
}