一、作业要求

| 所在班级 | 软件工程 |
| ---- | ---- | ---- |
| 作业要求 | 个人作业-四则运算题目生成程序 |
| 作业目标 | 学会使用Markdown算法 |
| 学号|3180701328 |

二.题目要求

写一个能自动生成小学四则运算题目的程序,然后在此基础上扩展:

1)除了整数以外,还要支持真分数的四则运算,例如:1/6+1/8=7/24

2)程序要求能处理用户的输入,判断对错,累积分数

3)程序支持可以由用户自行选择加、减、乘、除运算

4)使用-n参数控制生成题目的个数,例如Myapp.exe -n 10,将生成10个题目

三.代码提交
程序说明:
1)允许用户选择一种类型的算术问题来学习,输入1表示加法,2表示减法,3表示乘法,4表示除法;
2)由于程序代码中反复无穷递归,所以该程序的运算会不断进行下去,退出请自动关闭程序;

#include<iostream>`
#include<cstdlib>
#include<ctime>
using namespace std;
int Mul(int,int);
int Plus(int,int);
int Sub(int,int);
float Div(float,float);
int main()
{
    int a=0,b=0;
    int i;
      srand(time(0));
    cout<<"What do you want to study?(1-Plus,2-Sub,3-Mul,4-division,5-all the above)"<<endl;
    cin>>i
    switch(i)
    {
    case 1:Plus(a,b);break;
    case 2:Sub(a,b);break;
    case 3:Mul(a,b);break;
    case 4:Div(a,b);break;
    case 5:switch(1+rand()%4)
           {
    case 1:Plus(a,b);break;
    case 2:Sub(a,b);break;
    case 3:Mul(a,b);break;
    case 4:Div(a,b);break;
    default:break;
           }break;

    default:break;
    }
    return 0;
}
float Div(float x,float y)
{
    float m1, m;
    int k;
    k=1+rand()%4;
    int a1;
    cout<<"Please choose the level of this game(1 or 2):"<<endl;
    cin>>a1;
    srand(time(0));
    if(a1==1)
    {
         x=1+rand()%9;
        y=1+rand()%9;
    }
    if(a1==2)
    {
        x=1+rand()%99;
        y=1+rand()%99;
    }
        m=x/y;
    cout<<x<<"/"<<y<<"=?"<<endl;
    cin>>m1;
    if(m1==m)
    {
        switch(k)
        {
        case 1:cout<<"Very good!"<<endl;break;
        case 2:cout<<"Excellent!"<<endl;break;
        case 3:cout<<"Nice work!"<<endl;break;
        case 4:cout<<"Keep up the good work!"<<endl;break;
        default:break;
        }
        Div(x,y);
    }
    else
    {
        switch(k)
        {
        case 1:cout<<"Sorry,your answer is wrong!"<<endl;break;
        case 2:cout<<"Wrong.Try once again!"<<endl;break;
        case 3:cout<<"Don't give up!"<<endl;break;
        case 4:cout<<"No.Keep trying."<<endl;break;
        default:break;
        }
        Div(x,y);
    }
    return 0;
}


int Sub(int x,int y)
{
        int m1, m,k;
    k=1+rand()%4;
    int a1;
    cout<<"Please choose the level of this game(1 or 2):"<<endl;
    cin>>a1;
    srand(time(0));
    if(a1==1)
    {
         x=1+rand()%9;
        y=1+rand()%9;
    }
    if(a1==2)
    {
        x=1+rand()%99;
        y=1+rand()%99;
    }
        m=x-y;
    cout<<x<<"-"<<y<<"=?"<<endl;
    cin>>m1;
    if(m1==m)
    {
        switch(k)
        {
        case 1:cout<<"Very good!"<<endl;break;
        case 2:cout<<"Excellent!"<<endl;break;
        case 3:cout<<"Nice work!"<<endl;break;
        case 4:cout<<"Keep up the good work!"<<endl;break;
        default:break;
        }
        Sub(x,y);
    }
    else
    {
        switch(k)
        {
        case 1:cout<<"Sorry,your answer is wrong!"<<endl;break;
        case 2:cout<<"Wrong.Try once again!"<<endl;break;
        case 3:cout<<"Don't give up!"<<endl;break;
        case 4:cout<<"No.Keep trying."<<endl;break;
        default:break;
        }
        Sub(x,y);
    }
    return 0;
}

int Plus(int x,int y)
{
    int m1, m,k;
    k=1+rand()%4;
    int a1;
    cout<<"Please choose the level of this game(1 or 2):"<<endl;
    cin>>a1;
    srand(time(0));
    if(a1==1)
    {
         x=1+rand()%9;
        y=1+rand()%9;
    }
    if(a1==2)
    {
        x=1+rand()%99;
        y=1+rand()%99;
    }
        m=x+y;
    cout<<x<<"+"<<y<<"=?"<<endl;
    cin>>m1;
    if(m1==m)
    {
        switch(k)
        {
        case 1:cout<<"Very good!"<<endl;break;
        case 2:cout<<"Excellent!"<<endl;break;
        case 3:cout<<"Nice work!"<<endl;break;
        case 4:cout<<"Keep up the good work!"<<endl;break;
        default:break;
        }
        Plus(x,y);
    }
    else
    {
        switch(k)
        {
        case 1:cout<<"Sorry,your answer is wrong!"<<endl;break;
        case 2:cout<<"Wrong.Try once again!"<<endl;break;
        case 3:cout<<"Don't give up!"<<endl;break;
        case 4:cout<<"No.Keep trying."<<endl;break;
        default:break;
        }
        Plus(x,y);
    }
    return 0;
}


int Mul(int x,int y)
{

    int m1, m,k;
    k=1+rand()%4;
    int a1;
    cout<<"Please choose the level of this game(1 or 2):"<<endl;
    cin>>a1;
    srand(time(0));
    if(a1==1)
    {
         x=1+rand()%9;
        y=1+rand()%9;
    }
    if(a1==2)
    {
        x=1+rand()%99;
        y=1+rand()%99;
    }
        m=x*y;
    cout<<x<<"*"<<y<<"=?"<<endl;
    cin>>m1;
    if(m1==m)
    {
        switch(k)
        {
        case 1:cout<<"Very good!"<<endl;break;
        case 2:cout<<"Excellent!"<<endl;break;
        case 3:cout<<"Nice work!"<<endl;break;
        case 4:cout<<"Keep up the good work!"<<endl;break;
        default:break;
        }
        Mul(x,y);
    }
    else
    {
        switch(k)
        {
        case 1:cout<<"Sorry,your answer is wrong!"<<endl;break;
        case 2:cout<<"Wrong.Try once again!"<<endl;break;
        case 3:cout<<"Don't give up!"<<endl;break;
        case 4:cout<<"No.Keep trying."<<endl;break;
        default:break;
        }
        Mul(x,y);
    }
    
    return 0;
}

四、运行截图

五、个人小结
知识总结:
Fraction()-->完成分数的表示和计算,并且用分数表示出来。
eval()-->可以自动进行四则混合运算(带括号),但是本次题目没有加入括号运算
代码测试-->可以发现程序隐藏的错误。
性能测试-->能够很方便地看到程序的运行时间及效率,对于以后的修改可以更加专业且有针对性。

个人感悟:
题目看起来很简单,但是分析起来会发现有特别多的情况,如果只是随机生成数字运算会比较简单,结果有要求又会变得麻烦一些,加上括号可能更复杂,个人练习比较少,写个不是很复杂的代码也花很长时间,又加上对各种函数不熟悉,做起来更慢,需要多练习才行。以后有机会再将这个四则运算功能写更完善一些。

PSP:

PSP2.1 任务内容 预计时间(分钟) 实际耗时(分钟)
Planning 计划 20 40
Estimate 估算开发时间 290 500
Development 开发 80 100
Analysis 需求分析 20 20
Design Spec 生成设计文档 10 20
Design Review 设计复审 10 10
Coding Standard 代码规范 10 10
Design 具体设计 20 20
Coding 具体编码 20 20
Code Review 代码复审 20 20
Test 测试 30 20
Reporting 报告 20 30
Test Report 测试报告 20 30
Size Measurement 计算工作量 20 20
Postmorten & Process Improvement 事后总结,并提出过程改进计划 20 20
Size Measurement 合计 300 350
posted on 2020-11-07 16:31  夹击妹抖  阅读(120)  评论(0编辑  收藏  举报