C语言寒假大作战04

问题 答案
这个作业属于那个课程 https://edu.cnblogs.com/campus/zswxy/CST2019-2
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/10279
这个作业的目标 在其基础上增加各年级题目的答案函数
作业正文 https://i-beta.cnblogs.com/posts/edit
参考文献 https://zhidao.baidu.com/question/271899014.html

2.2.2 设计思路和遇到的问题

1.在三年级的题中一直想不到什么好方法,就把可能的情况全部printf了。
2.在除法中0/会出现不等于0的情况,所以加上(double)a/b。

2.2.3 程序结果截图



2.2.4 程序代码

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
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(sum1<sum2&&sum2<10)
                    sum1=sum2+1;
            else printf("%d - %d = %d\n",sum1,sum2,sum1-sum2); break;
        }
   }
printf("\n\n");  
}
void operation_2(){
srand(time(NULL));
int sum1,sum2,i,n;
char op[2]={'*','/'},c;
printf("现在是二年级题目:\n请输入生成题目的个数:");
scanf("%d",&n);
for(i=n;i>0;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;
 }

2.2.5 Gitee上传截图与链接



https://gitee.com/peng_you_hao/projects

posted @ 2020-03-05 20:39  彭有濠  阅读(105)  评论(0编辑  收藏  举报