C语言寒假大作战04

1.作业头

问题 答案
这个作业属于那个课程 https://edu.cnblogs.com/campus/zswxy/CST2019-2
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10279
这个作业的目标 给上次作业的随机口算题提供答案
作业正文 https://www.cnblogs.com/508775262-qq/p/12321566.html
参考文献 cpp6 百度

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

整体框架并无太大改动,因为考虑到是小学的问题,为了防止出现负数的情况,对一些特殊情况做了说明,并限制了一些数的范围。

3.程序结果截图

C语言寒假大作战04_第1张图片
C语言寒假大作战04_第2张图片
C语言寒假大作战04_第3张图片

4.程序代码

#include
#include 
#include 
int menu()
{
   printf("操作列表:\n    1)一年级    2)二年级    3)三年级\n     4)帮助     5)退出程序\n");
   printf("请输入操作:> ");
   return 0;
}
int help(){
     printf("帮助信息\n您需要输入命令代号来进行操作, 且\n一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n");
     return 0;
}
int error()
 {
printf("\n\n");
    printf("Error!!!\n错误操作指令, 请重新输入\n\n\n");
    return 0;
 }
 void operation_1(){
 srand(time(NULL));
 int sum1,sum2,i,n;
 char op[2]={'+','-'};
 printf("现在是一年级题目:\n请输入生成题目的个数:");
 scanf("%d",&n);
 for(i=n;i>0;i--){
       sum1=rand()%11;
       sum2=rand()%11;
    switch(op[rand()%2])
       {
         case '+': printf("%d + %d = %d\n",sum1,sum2,sum1+sum2); break;
         case '-': if(sum10;i--){
    sum1=rand()%101;
    sum2=rand()%101;
    c=op[rand()%2];
    switch(op[rand()%2])
    {
      case'*':if(sum1==0||sum2==0)
               printf("0 \n");
               else
               printf("%d * %d = %d\n",sum1,sum2,sum1*sum2);break;
      case'/':if(c=='/'&&sum2==0)
              sum2=rand()%100+1;
              else if(sum1==0)
              printf("0 \n");
              else printf("%d / %d = %g\n",sum1,sum2,(double)sum1/sum2);
    }   
    }
    printf("\n\n");
}
void operation_3(){
srand(time(NULL));
int sum1,sum2,sum3,i,n;
char op[4]={'*','/','+','-'},op1,op2;
printf("现在是三年级题目:\n请输入生成题目的个数:");
scanf("%d",&n);
for(i=n;i>0;i--){
    sum1=rand()%101;
    op1=op[rand()%4];
    sum2=rand()%11;
    if(op1=='/'&&sum2==0)    
    sum2=rand()%10+1;
    op2=op[rand()%4];
    sum3=rand()%11;
    if(op2=='/'&&sum3==0)    
    sum3=rand()%10+1;
    switch(rand()%12)
      {
        case 1: printf("%d + %d - %d = %d\n",sum1,sum2,sum3,sum1+sum2-sum3); break;
        case 2: printf("%d + %d * %d = %d\n",sum1,sum2,sum3,sum1+sum2*sum3); break;
        case 3: printf("%d + %d / %d = %g\n",sum1,sum2,sum3,(double)sum1+sum2/sum3); break;
        case 4: printf("%d - %d + %d = %d\n",sum1,sum2,sum3,sum1-sum2+sum3); break;
        case 5: printf("%d - %d * %d = %d\n",sum1,sum2,sum3,sum1-sum2*sum3); break;
        case 6: printf("%d - %d / %d = %g\n",sum1,sum2,sum3,(double)sum1-sum2/sum3); break;
        case 7: printf("%d * %d + %d = %d\n",sum1,sum2,sum3,sum1*sum2+sum3); break;
        case 8: printf("%d * %d - %d = %d\n",sum1,sum2,sum3,sum1*sum2-sum3); break;
        case 9: printf("%d * %d / %d = %g\n",sum1,sum2,sum3,(double)sum1*sum2/sum3);break;
        case 10: printf("%d / %d + %d = %g\n",sum1,sum2,sum3,(double)sum1/sum2+sum3); break;
        case 11: printf("%d / %d - %d = %g\n",sum1,sum2,sum3,(double)sum1/sum2-sum3); break;
        case 12: printf("%d / %d * %d = %g\n",sum1,sum2,sum3,(double)sum1/sum2*sum3); break;
      }
   }
    printf("\n\n");
}
int main() 
{   
  int Q;
  printf("========== 口算生成器 ==========\n");
  printf("欢迎使用口算生成器 :\n \n \n帮助信息\n您需要输入命令代号来进行操作, 且\n");
  printf("一年级题目为不超过十位的加减法;\n二年级题目为不超过百位的乘除法;\n三年级题目为不超过百位的加减乘除混合题目.\n\n\n");
  do{
      menu();
      scanf("%d",&Q);
      printf("< 执行操作 >\n\n");
      switch (Q) {        
          case 1:operation_1();break;
          case 2:operation_2();break;
          case 3:operation_3();break;
          case 4:help();break;
          case 5:printf("程序结束,欢迎下次使用任意键结束……\n");return 0;
          default:error();break;
      }
   }while(Q!=5);
   return 0;
 }

5. Gitee上传截图与链接

C语言寒假大作战04_第4张图片
C语言寒假大作战04_第5张图片

链接:https://gitee.com/liu_qing_yu0103/lqy20199237

你可能感兴趣的:(C语言寒假大作战04)