C语言寒假大作战03

1 作业头:#

这个作业属于哪个课程|https://edu.cnblogs.com/campus/zswxy/CST2019-2
--|:--😐:--😐--:
这个作业要求在哪里|https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10271
这个作业的目标|使用函数、Switch语句来完成菜单程序
作业正文|https://www.cnblogs.com/3165984153zb/p/12295118.html
参考文献|https://www.runoob.com/cprogramming/c-function-rand.html

2 设计思路和遇到的问题#

问题:函数rand()的使用不会,然后寻找更多的资料进行解决。
随机数的范围的确定,查完资料后得到:int num = rand()%n+a,a是初始值,最大值n-1+a。

3 程序结果截图#



4 程序代码#

#include<stdio.h>
#include<stdlib.h>
void operation1(){
    int a,b,i;
    printf("现在是一年级题目:\n请输入生成个数>");
    scanf("%d",&a);
    for(i=0;i<a;i++){
        int shu1=rand()%11;
        int shu2=rand()%11;
        char fh[2]={'+','-'};
        b=rand()%2;
        printf("%2d %c %2d=___\n",shu1,fh[b],shu2);
    }
} 
void operation2(){
    int a,b,i;
    printf("现在是二年级题目:\n请输入生成个数>");
    scanf("%d",&a);
    for(i=0;i<a;i++){
        int shu1=rand()%101;
        int shu2=rand()%101;
        int shu3=rand()%100+1;
        char fh[2]={'*','/'};
        b=rand()%2;
        if(fh[b]=='/')printf("%2d %c %2d=___\n",shu1,fh[b],shu3);
        else printf("%2d %c %2d=___\n",shu1,fh[b],shu2);
    }
}
void operation3(){
    int a,b,i,b1;
    printf("现在是三年级题目:\n请输入生成个数>");
    scanf("%d",&a);
    for(i=0;i<a;i++){
        int shu1=rand()%101;
        int shu2=rand()%101;
        int shu3=rand()%101;
        int shu4=rand()%100+1;
        char fh[4]={'+','-','*','/'};
        b=rand()%4;
        b1=rand()%4;
        if(fh[b]=='/'&&shu2==0) shu2=shu4;
        if(fh[b1]=='/'&&shu3==0) shu3=shu4;
        printf("%2d %c %2d %c %2d=___\n",shu1,fh[b],shu2,fh[b1],shu3);
        
    }
}
void menu(){
    printf("操作列表:\n1)一年级    2)二年级    3)三年级\n4)帮助     5)退出程序\n请输入操作>");
} 
void help(){
    printf("帮助信息:\n您需要输入命令代号来进行操作,且\n一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n");
} 
void error(){
    printf("Error!!!\n错误操作指令,请重新输入\n\n"); 
}
int main(){
    int Grade=1;
    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");break;
            default:error();break;
        }if(Grade!=5) menu();
    }
    return 0;
}

5 Gitee上传截图与链接#



链接
https://gitee.com/zhaobozhaobo

posted @ 2020-02-11 15:00  揽星辰  阅读(91)  评论(0)    收藏  举报