C语言寒假大作战04

这个作业属于哪个课程 C语言作业计科四班
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277
这个作业的目标 学习用上次作业代码编写出答案
作业正文 看下面内容
其他参考文献 https://m.wenda.so.com/q/1378547425075728

2.2 设计思路和遇到的问题

设计思路:其实利用上次代码进行加工,在百度一下%g和rand()如何结合运用以及他们各自的用法,在自己琢磨一下。
遇到的问题:其实运用上次代码可以快速编写出一二年级的答案,但是三年级是怎么也是错误的,不知道哪里出现了问题,所以花费了很多时间。

2.2.3 程序结果截图

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

2.2.4 程序代码

#include 
#include
#include 
#include 
void menu();
void help();
void error();
void operation_1(); void one_1();
void operation_2(); void two_2();
void operation_3(); void three_3();
int main()
{
    int n;
    printf("========== 口算生成器 ==========\n");
    printf("欢迎使用口算生成器 :\n");
    printf("\n");
    printf("\n");
    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("\n");
        if(n==5) break;
        if(n>5||n<1) error();
    }
    return 0;
}
void help()
{
    printf("帮助信息\n");
    printf("您需要输入命令代号来进行操作\n");
    printf("一年级题目为不超过十位的加减法\n");
    printf("二年级题目为不超过百位的乘除法\n");
    printf("三年级题目为不超过百位的加减乘除混合题目\n");
    printf("\n");
}
void menu()
{
    printf("操作列表:\n1)一年级    2)二年级    3)三年级\n");
    printf("4)帮助      5)退出程序\n请输入代号:");
    printf("\n");
}
void error()
{
    printf("操作错误 请重新输入 ");
    printf("\n");
    printf("\n");
 }
 void operation_1()
 {
    printf("请输入题目数量>");
    one_1();
 }
  void operation_2()
 {
    printf("请输入题目数量>");
    two_2();
 }
  void operation_3()
 {
    printf("请输入题目数量>");
    three_3();
}
 void one_1()
 {
    int n,a,b,c,x;
    time_t t;
    scanf("%d",&n);
    printf("一年级题目如下:\n"); 
    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)
        {
            x=a+b;
            printf("%d + %d = %d\n",a,b,x);
        }
        else
        {
            x=a-b; 
            printf("%d - %d = %d\n",a,b,x);
            printf("\n");
        } 
     } 
    
 }
  void two_2()
 {
    int n,d,e,f;
    float z;
    time_t t;
    scanf("%d",&n);
    printf("二年级题目如下:\n"); 
    srand((unsigned)time(NULL));
    for(int i=1;i<=n;i++)
    {
        d=rand()%100;
        e=rand()%100;
        f=rand()%2;
        if(f==0)
        {
            z=d*e;
            printf("%d * %d = %g\n",d,e,z);
        }
        else
        {
            while(e==0)
           {
              e=rand() % 100;
            } 
           z=d/(e*1.0);
           printf("%d / %d = %g\n",d,e,z);
           printf("\n");
        }
     } 
 }
  void three_3()
 {
    int n,h,k,j;
    float s;
    char fh1[2],fh2[2];
    time_t t;
    scanf("%d",&n);
    printf("三年级题目如下:\n"); 
    srand((unsigned)time(NULL));
    char fh[4][6] = {"*","/","+","-"};
    for(int i=1;i<=n;i++)
    {
        h=rand()%100;
        k=rand()%100;
        j=rand()%100;
         while (h==0||k==0||j==0)
        {
            h=rand() % 100;k=rand() % 100;j=rand() % 100; 
        }
        strcpy(fh1,fh[rand() % 3]);
        strcpy(fh2,fh[rand() % 3]);
        if(strcmp(fh1,"*")==0&&strcmp(fh2,"*")==0)
        {
            s = h * k * j;
        printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"/")==0)
        {
            s = h * k / (j*1.0);
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"+")==0)
        {
            s = h * k + j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"-")==0)
        {
            s = h * k - j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"+")==0)
        {
            s = h / (k*1.0) + j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"-")==0)
        {
            s = h / (k*1.0) - j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"/")==0)
        {
            s = h / (k*1.0) / (j*1.0);
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"-")==0)
        {
            s = h + k - j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"+")==0)
        {
            s = h + k + j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"-")==0)
        {
            s = h - k - j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"*")==0)
        {
            s = h / (k*1.0) * j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"/")==0)
        {
            s = h + k / (j*1.0);
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"*")==0)
        {
            s = h + k * j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"/")==0)
        {
            s = h - k / (j*1.0);
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"+")==0)
        {
            s = h - k + j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"*")==0)
        {
            s = h - k * j;
            printf("%2d %s %2d %s %2d = %g\n",h,fh1,k,fh2,j,s);
        }
    }
}

2.2.5 Gitee上传截图与链接

C语言寒假大作战04_第6张图片
C语言寒假大作战04_第7张图片

链接:

https://gitee.com/xie_xiang_yun/cxiexiangyun.git

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