C语言寒假大作战03

作业头

问题 答案
这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/CST2019-2
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10271
这个作业的目标 使用上次作业的菜单框架,在其基础上增加各年级题目操作函数
作业正文 https://www.cnblogs.com/abcd123456/p/12256867.html
其他参考文献 随机数rand,C语言程序设计

主要内容

使用上次作业的菜单框架,在其基础上增加各年级题目操作函数

1.菜单程序函数调用图如下:

C语言寒假大作战03

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

  • 设计思路同作业2,本次作业只是加了rand随机函数。
    遇到的问题:在主函数while语句循环那里,没有注意后面花括号打的位置,导致一个死循环,后将花括号放到了switch-case语句后面,才能够得到正确答案 。

3.程序结果截图

C语言寒假大作战03

C语言寒假大作战03

C语言寒假大作战03

4.程序代码

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int help(int opt)
{
    printf("
操作列表:
    1)一年级    2)二年级    3)三年级
     4)帮助     5)退出程序
");
    printf("请输入操作> ");
    return 0;
}

void operation1(int opt)
{
   int i,op;
   printf("< 执行操作 :)

现在是一年级题目:
请输入生成数> 10
< 执行操作 :)

");
   for(i=0;i<10;i++)
   {
       int divisor1=rand()%10;
       int divisor2=rand()%10;
       char ops[2]={'+','-'};
       op=rand()%2;
        
       printf("%d %c %d=__
",divisor1,ops[op],divisor2);
    }
}

void operation2(int opt)
{
    int i,op;
    printf("< 执行操作 :)

现在是二年级题目:
请输入生成数> 10
< 执行操作 :)

");
    for(i=0;i<10;i++)
    {
       int divisor1=rand()%101;
       int divisor2=rand()%101;
       char ops[2]={'*','/'};
       op=rand()%2;

       if(divisor2==0)printf("%2d * %2d=__
",divisor1,divisor2);
       else printf("%2d %c %2d=__
",divisor1,ops[op],divisor2);
    }  
}

void operation3(int opt)
{
    int i,op1,op2,op3;
    printf("< 执行操作 :)

现在是三年级题目:
请输入生成数> 10
< 执行操作 :)

");
    for(i=0;i<10;i++)
    {
       int divisor1=rand()%101;
       int divisor2=rand()%101;
       int divisor3=rand()%101;
       char operator1[4]={'+','-','*','/'};
       char operator2[3]={'+','-','*'};
       op1=rand()%4;
       op2=rand()%3;
       op3=rand()%4;
       
       if((divisor2==0)&&(divisor3!=0))printf("%2d %c %2d %c %2d=__
",divisor1,operator2[op2],divisor2,operator1[op1],divisor3);
       else if((divisor2!=0)&&(divisor3==0))printf("%2d %c %2d %c %2d=__
",divisor1,operator1[op1],divisor2,operator2[op2],divisor3);
       else printf("%2d %c %2d %c %2d=__
",divisor1,operator1[op1],divisor2,operator1[op3],divisor3);
    }
}

int error(int opt)
{
    printf("Error!!!
错误操作指令, 请重新输入");
    return 0;
}

int main() 
{   
    printf("========== 口算生成器 ==========
");
    printf("欢迎使用口算生成器 :
 
 
帮助信息
您需要输入命令代号来进行操作, 且
");
    printf("一年级题目为不超过十位的加减法;
二年级题目为不超过百位的乘除法;
三年级题目为不超过百位的加减乘除混合题目.


");
    
    int opt=-1;
    
    while(opt!=0)
    {
       help(opt);
       scanf("%d",&opt);
       
    switch(opt)
    {
        case 1:operation1(opt);break;
        case 2:operation2(opt);break;
        case 3:operation3(opt);break;
        default :error(opt);break;
    }
    }
 return 0;
} 

5.Gittee上传截图与链接

截图:
C语言寒假大作战03

C语言寒假大作战03

链接:
https://gitee.com/ning_pei