C语言寒假大作战03

简易菜单代码的学习 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10269
作业链接 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10269
这个作业的目标 随机数的生成
参考文献 百度,http://c.biancheng.net/view/2043.html

1.设计思路和遇到的问题
设计思路:开始只有问题没有思路,并不知道随机数怎么生成,百度之后理解,就开始有了思路,开始不懂怎么随机取加减乘除,利用switch,rand()函数的取余方法来规定符号随机,比如
a=rand()%5 则会随机产生0-4 这样的5个数,及可使用switch语句随机产生5个符号。
(修改这个想法,之前看错题目了。无需这样设计,直接利用数组的形式随机获得符号,rand%x 可表示为你需要的字符数有多少。)

遇见的问题:随机的产生,通过百度解决。

2.程序截图

3.程序代码

#include <stdio.h>
#include<stdlib.h>
#include<time.h> 
void menu();
void help();
void error();
void operation_1();void one_1();
void operation_2();void two_1();
void operation_3();void three_1();

int main()
{
	int n;
	printf("========== 口算生成器 ==========\n欢迎使用口算生成器 :\n希望小学期中考试\n");
	printf("\n");
	help();
	while(1)
	{
		menu();
		scanf("%d",&n);
		switch(n)
		{
			case 1:operation_1();break;
			case 2:operation_2();break;
			case 3:operation_3();break;
			case 4:help();break;
		}
	    printf("\n");
		if(n==5) break;
	    if(n>5||n<1) error();
	}
	return 0;
}
void help()
{
	printf("帮助信息\n您需要输入命令代号来进行操作, 且\n");
	printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n");
	printf("三年级题目为不超过百位的加减乘除混合题目.\n");
	printf("\n");
}
void menu()
{
	printf("操作列表:\n1)一年级    2)二年级    3)三年级\n4)帮助      5)退出程序\n请输入代号:");
}
void error()
{
	printf("憨憨,请重新输入正确数值。");
	printf("\n");
	printf("\n");
 } 
 void operation_1()
 {
 	printf("请输入题目数量>");
 	one_1();
 }
  void operation_2()
 {
 	printf("请输入题目数量>");
 	two_1();
 }
  void operation_3()
 {
 	printf("请输入题目数量>");
 	three_1();
 }
  void one_1()
 {
    int n,a,b,c;
    scanf("%d",&n);
    printf("一年级题目如下:\n"); 
	srand((unsigned)time(NULL));
	for(int i=1;i<=n;i++)
	{
		a=rand()%10+1;
		b=rand()%10+1;
		c=rand()%2;
		if(c==0)
		printf("%2d + %2d = ___",a,b);
		else
		printf("%2d - %2d = ___",a,b);
		printf("\n");
	 } 
 } 
 void two_1()
 {
    int n,a,b,c;
    scanf("%d",&n);
	printf("二年级题目如下:\n"); 
	srand((unsigned)time(NULL));
	for(int i=1;i<=n;i++)
	{
		a=rand()%100+1;
		b=rand()%100+1;
		c=rand()%2;
		if(c==0)
		printf("%3d * %3d = ___",a,b);
		else
		printf("%3d / %3d = ___",a,b);
	    printf("\n");
	 } 
 }
 void three_1()
 {
 	int n,a,b,c,f,e;
 	char ch[4]={'+','-','*','/'} ;
 	scanf("%d",&n); 
	printf("三年级题目如下:\n"); 
	srand((unsigned)time(NULL));
 	for(int i=1;i<=n;i++)
 	{
 		a=rand()%100+1;
 		b=rand()%100+1;
 		e=rand()%100+1; 
 		c=rand()%4;
 		f=rand()%4; 
        printf("%3d %2c %3d %c %3d = ___",a,ch[c],b,ch[f],e);
		printf("\n");
	 }
 }

Gitee上传截图与链接

链接:https://gitee.com/zhou_wango/E-zuoye

增加内容:
嘻嘻,写完才反应过来有参考资料。但是我感觉我这一份比较好理解一点点啦。链接如下:
http://c.biancheng.net/view/2043.html

posted @ 2020-02-05 23:58  kame呦呦  阅读(169)  评论(0编辑  收藏  举报