zoukankan      html  css  js  c++  java
  • C语言寒假大作战04

    问题 答案
    这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277
    这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/CST2019-4/homework/10277
    这个作业的目标 学会使用%g以及会利用rand()函数随机生成口算题答案
    作业正文 使用上次作业的菜单框架,并在其基础上增添各个年级题目的答案函数
    其他参考文献 https://zhidao.baidu.com/question/271899014.html, 百度等

    2.2.2 设计思路和遇到的问题

    设计思路:

    一开始是直接想着就在rand()函数后面随机添加结果答案,但是想象归想象,我就去查了下百度,发现并没有和我想法类似的代码。然后就陷入了沉思中。最后就上网结果及问其他人在之前的代码上做了改变
    

    遇到的问题:

    (1)一直得不到结果
    (2)刚开始没有明白%g的意思,导致错误
    

    2.2.3 程序结果截图



    2.2.4 程序代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    void menu(); 
    void help();
    void one();void operation_1(); 
    void two();void operation_2();
    void three();void operation_3(); 
    void error();
    int main()
    {
    int opt=1,n;
    printf("==========口算生成器==========
    ");
    printf("欢迎使用口算生成器:
    ");
    printf("
    ");
    help();
    while(opt!=0)
    {
    menu();
    printf("请输入操作> ");
    scanf("%d",&opt);
    printf("<执行操作:)
    ");
    printf("
    ");
    switch(opt)
    {
    case 1:operation_1();break;
    case 2:operation_2();break;
    case 3:operation_3();break;
    case 4:help();break;
    case 5:printf("程序结束, 欢迎下次使用
    ");
    printf("任意键结束……");
    opt=0;
    default:error();break;
    }
    }
    return 0;
    }
    void menu()
    {
    printf("
    ");
    printf("操作列表:
    ");
    printf("1)一年级 2)二年级 3)三年级
    ");
    printf("4)帮助 5)退出程序
    ");
    }
    void help()
    {
    printf("
    ");
    printf("帮助信息
    ");
    printf("您需要输入命令代号来进行操作,且
    ");
    printf("一年级题目为不超过十位的加减法
    ");
    printf("二年级题目为不超过百位的乘除法
    ");
    printf("三年级题目为不超过百位的加减乘除混合题目.
    ");
    }
    void operation_1() 
    {
    printf("请输入生成个数>");
    one();
    }
    void operation_2()
    {
    printf("请输入生成个数>");
    two();
    }
    void operation_3()
    {
    printf("请输入生成个数>");
    three();
    }
    void one()
    {
    int n,a,b,c,d;
    time_t t;
    srand((unsigned) time(&t));
    printf("现在是一年级题目:
    ");
    scanf("%d",&n);
    printf("<执行操作:)
    ");
    for (int i=0;i<n;i++)
    {
        a=rand() % 10;
        b=rand() % 10;
        c=rand() % 2;
        if (c==0)
        {
        
           d=a+b;
           printf("%d + %d = %d
    ",a,b,d);
        }
        else
        {
           d=a-b;
           printf("%d - %d = %d
    ",a,b,d);
           }
    }
    
    }
    void two()
    {
    int n,a,b,c;
    float d;
    time_t t;
    srand((unsigned) time(&t));
    printf("现在是二年级题目:
    ");
    scanf("%d",&n);
    printf("<执行操作:)
    ");
    for (int i=0;i<n;i++)
    {
        a=rand() % 10;
        b=rand() % 10;
        c=rand() % 2;
        if (c==0)
        {
           d=a*b;
           printf("%d * %d = %g
    ",a,b,d);
           }
        else{
        while(b==0)
        {
            b=rand() % 10;
         } 
           d=a/(b*1.0);
           printf("%d / %d = %g
    ",a,b,d);
           }
    }
    
    }
    void three()
    {
     int n,m,i,a,b,c;
    float z;
    char fh1[2],fh2[2];
    time_t t;
    srand((unsigned) time(&t));
    printf("现在是三年级题目:
    ");
    scanf("%d",&n);
    printf("<执行操作:)
    ");
    char fh[4][6] = {"*","/","+","-"};
     for (int i=0; i<n; i++)
    {
        a=rand() % 100;
        b=rand() % 100;
        c=rand() % 100;
        while (a==0||b==0||c==0)
        {
            a=rand() % 100;b=rand() % 100;c=rand() % 100; 
        }
        strcpy(fh1,fh[rand() % 3]);
        strcpy(fh2,fh[rand() % 3]);
        if(strcmp(fh1,"*")==0&&strcmp(fh2,"*")==0)
        {
            z = a * b * c;
        printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"/")==0)
        {
            z = a * b / (c*1.0);
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"+")==0)
        {
            z = a * b + c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"*")==0&&strcmp(fh2,"-")==0)
        {
            z = a * b - c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"+")==0)
        {
            z = a / (b*1.0) + c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"-")==0)
        {
            z = a / (b*1.0) - c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"/")==0)
        {
            z = a / (b*1.0) / (c*1.0);
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"-")==0)
        {
            z = a + b - c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"+")==0)
        {
            z = a + b + c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"-")==0)
        {
            z = a - b - c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"/")==0&&strcmp(fh2,"*")==0)
        {
            z = a / (b*1.0) * c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"/")==0)
        {
            z = a + b / (c*1.0);
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"+")==0&&strcmp(fh2,"*")==0)
        {
            z = a + b * c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"/")==0)
        {
            z = a - b / (c*1.0);
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"+")==0)
        {
            z = a - b + c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
        else if(strcmp(fh1,"-")==0&&strcmp(fh2,"*")==0)
        {
            z = a - b * c;
            printf("%2d %s %2d %s %2d = %g
    ",a,fh1,b,fh2,c,z);
        }
    }
    
    }
    void error()
    {
    printf("Error!!!
    ");
    printf("错误操作指令, 请重新输入
    ");
    }
    

    2.2.5 Gitee上传截图与链接



    https://gitee.com/douqi123

  • 相关阅读:
    SQL分组统计
    实用DOS命令
    Shadertoy 教程 Part 6 使用光线步进算法创建3D场景
    浅谈web前端优化
    如何搭建一套前端监控系统
    with(this)中with的用法及其优缺点
    vue mvvm
    散列表(哈希表)(二)散列函数的构造方法
    作为程序员,你最常上的网站是什么
    散列表(哈希表)(一)散列表的概念
  • 原文地址:https://www.cnblogs.com/douqi/p/12303521.html
Copyright © 2011-2022 走看看