zoukankan      html  css  js  c++  java
  • 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("========== 口算生成器 ==========
    欢迎使用口算生成器 :
    希望小学期中考试
    ");
    	printf("
    ");
    	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("
    ");
    		if(n==5) break;
    	    if(n>5||n<1) error();
    	}
    	return 0;
    }
    void help()
    {
    	printf("帮助信息
    您需要输入命令代号来进行操作, 且
    ");
    	printf("一年级题目为不超过十位的加减法;
    二年级题目为不超过百位的乘除法;
    ");
    	printf("三年级题目为不超过百位的加减乘除混合题目.
    ");
    	printf("
    ");
    }
    void menu()
    {
    	printf("操作列表:
    1)一年级    2)二年级    3)三年级
    4)帮助      5)退出程序
    请输入代号:");
    }
    void error()
    {
    	printf("憨憨,请重新输入正确数值。");
    	printf("
    ");
    	printf("
    ");
     } 
     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("一年级题目如下:
    "); 
    	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("
    ");
    	 } 
     } 
     void two_1()
     {
        int n,a,b,c;
        scanf("%d",&n);
    	printf("二年级题目如下:
    "); 
    	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("
    ");
    	 } 
     }
     void three_1()
     {
     	int n,a,b,c,f,e;
     	char ch[4]={'+','-','*','/'} ;
     	scanf("%d",&n); 
    	printf("三年级题目如下:
    "); 
    	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("
    ");
    	 }
     }
    

    Gitee上传截图与链接

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

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

  • 相关阅读:
    CSS之属性操作
    Python模块之sys
    Python模块之hashlib:提供hash算法
    Python模块之random:获取随机数
    Python模块之time:时间获取和转换
    Python模块之os:操作系统接口函数
    Python最牛逼内建函数之 filter:过滤
    Python最牛逼内建函数之 zip()
    Python最牛逼内建函数之 max/min()
    Python最牛逼内建函数之 map()
  • 原文地址:https://www.cnblogs.com/zhouwang/p/12267379.html
Copyright © 2011-2022 走看看