个人作业1——四则运算题目生成程序

需求分析:

1.控制运算表达式的题目数量,根据键盘输入数字提供对应数量表达式

2.运算符个数不能超过三个

3.能够自动生成四则运算

4.随机生成分数运算或整数运算

5.程序判断对错并给出正确答案

6.答完题后给出正确率

实现步骤:

1.根据用户输入的参数n,自动生成n道分数计算题

2.通过用户输入的答案判断对错

3.给出用户答题的正确率

代码设计思路:自定义四个整数组合成两个分数,并通过简单的替换函数确保生成的分数为真分数。并通过判断对错统计正确率

代码:

 

#include<iostream>
#include<stdlib.h>
#include<time.h>
void boom()
{
    float x,y;
    int z,t;
    int first_num,second_num,third_num,fourth_num;
    srand(time(NULL));
    int n;
    float k;
    float d;
    float daan[100];
    int zq=0;
    int cw=0;
    std::cout<<"需要列出几道数学题?"<<std::endl;
    std::cin>>n;
    for(int i=0;i<n;i++)
    {
        z=rand()%4;
        first_num=rand()%10;
        second_num=rand()%10;
        third_num=rand()%10;
        fourth_num=rand()%10;
        if (second_num==1)
        {
            x=(float)first_num;
        }
        else if(first_num>second_num)
        {
            t=first_num;
            first_num=second_num;
            second_num=t;
        }
        x=(float)first_num/second_num;
        if (fourth_num==1)
        {
            y=(float)third_num;
        }
        else if(third_num>fourth_num)
        {
            t=third_num;
            third_num=fourth_num;
            fourth_num=t;
        }
        y=(float)third_num/fourth_num;
        z=rand()%4;
        switch(z)
        {
            case 0:
                std::cout<<i+1<<""<<first_num<<"/"<<second_num<<"+"<<third_num<<"/"<<fourth_num<<"="<<"     ";
                daan[i+1]=x+y;
                break;
                
            case 1:
                std::cout<<i+1<<""<<first_num<<"/"<<second_num<<"-"<<third_num<<"/"<<fourth_num<<"="<<"     ";
                daan[i+1]=x-y;
                break;
            case 2:
                std::cout<<i+1<<""<<first_num<<"/"<<second_num<<"*"<<third_num<<"/"<<fourth_num<<"="<<"     ";
                daan[i+1]=x*y;
                break;
            case 3:
                if(y!=0)
                {
                    std::cout<<i+1<<""<<first_num<<"/"<<second_num<<"÷"<<third_num<<"/"<<fourth_num<<"="<<"     ";
                }
                else
                {
                    i=i-1;
                }
                daan[i+1]=(float)x/y;
                break;
        }
        if((i+1)%3==0)
        {
            for(int c=0;c<=1;c++)
            {
                std::cout<<std::endl;
            }
        }

    }
    for(int i=1;i<=n;i++)
    {
        std::cout<<"请作答:"<<std::endl;
        std::cout<<i<<"";
        std::cin>>d;
        if(d==daan[i])
        {
            std::cout<<"回答正确"<<std::endl;
            std::cout<<std::endl;
            zq=zq+1;
        }
        else
        {
            std::cout<<"回答错误,正确答案为:"<<"  "<<daan[i]<<std::endl;
            std::cout<<std::endl;
            cw=cw+1;
        }
    }
    k=(float)zq/n;
    std::cout<<"回答正确的数量为:"<<" "<<zq<<""<<std::endl;
    std::cout<<"回答错误的数量为:"<<" "<<cw<<""<<std::endl;
    std::cout<<"正确率为:"<<" "<<k<<std::endl;
}
int main()
{
    boom();
    int c;
    std::cout<<"要继续答题吗?(0:继续  1:退出)"<<std::endl;
    std::cin>>c;
    if(c==0)
    {
        std::cout<<std::endl;
        main();
    }
    else
    {
        std::cout<<std::endl;
        return 0;
    }
    
    return 0;
}

 

运行结果:

PSP:

Personal Software Process Stages

Time  Senior Student

Time

计划

1h

2h

估计这个任务需要多少时间

5d

7d

开发

3d

3d

需求分析 (包括学习新技术)

1h

0.5h

生成设计文档

0

0

设计复审

0

0

代码规范

1h

1h

具体设计

1d

1.5d

具体编码

3d

4d

代码复审

3h

1.5h

测试(自我测试,修改代码,提交修改)

3h

2h

报告

2h

1。5h

测试报告

0

0

计算工作量

0

0

并提出过程改进计划

0

0

小结:

  看了班级交上去的同学的博客之后,发现自己弄的简直就是小学生写出来的东西,借鉴了很多知识,以前学c语言的时候有做过类似四则运算的计算器,所以搬了那个时候的一些代码,其中随机数和四则运算式生成。主要遇到的问题是分数的表达和运算,计算和输入的时候没办法做到老师要求的。

  自己的基础太差,动手打代码的能力还需要大幅度提升,慢慢来吧。

 

posted @ 2017-09-23 13:33  赵嘉楠  阅读(309)  评论(2编辑  收藏  举报