首页 > 技术文章 > 作业三(2)

GGGGGG7 2016-03-23 19:45 原文

2. 代码复审10分)

每位同学复审其结对小伙伴的个人项目(第二次作业中的四则运算编程作业),并在博客中记录复审结果。

 这次我复查的是段天旭在第二次作业中的四则运算编程

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>

void DealFenshu(int m, int a[][2])
{
    for(int p=0;p<m;p++)
    {
        int i=(int)rand()%10;
        int j=(int)rand()%10;
        while(j==0||i>=j)
        {
            i=(int)rand()%10;
            j=(int)rand()%10;
        }
        int x=(int)rand()%10;
        int y=(int)rand()%10;
        while(y==0||x>=y)
        {
            x=(int)rand()%10;
            y=(int)rand()%10;
        }
        int k=(int)rand()%100/25;
        switch(k)
        {
            case 0:
                cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*y+x*j;
                a[p][1]=j*y;
                break;
            case 1:
                cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*y-x*j;
                a[p][1]=j*y;
                break;
            case 2:
                cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";
                a[p][0]=i*x;
                a[p][1]=j*y;
                break;
            case 3:
                a[p][0]=i*y;
                a[p][1]=j*x;
                cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";
            }
            
            if(p%5==4)
            {
                cout<<endl;
            }
            else
            {
                cout<<‘\t‘;
            }
    }

}
void DisplayFenshu(int a[][2],int w,int m)
{
    if(w==1)
    {
        for(int q=0;q<m;q++)
        {
            if(a[q][0]==0)
                cout<<"0"<<‘\t‘;
            else
                cout<<a[q][0]<<"/"<<a[q][1]<<‘\t‘;
            if(q%5==4)
            {
                cout<<endl;
            }
        }
    }
    else
    {};
}
void DealInt(int m,int a[])
{
        
    for(int p=0;p<m;p++)
    {
    int i=(int)rand()%10;
    int j=(int)rand()%10;
    int k=(int)rand()%100/25;
    switch(k)
    {
    case 0:
        cout<<i<<"+"<<j<<"=";
            a[p]=i+j;
        break;
    case 1:
        cout<<i<<"-"<<j<<"=";
        a[p]=i-j;
        break;
    case 2:
        cout<<i<<"*"<<j<<"=";
        a[p]=i*j;
        break;
    case 3:
        try
        {
        a[p]=i/j;
        cout<<i<<"/"<<j<<"=";
        }
        catch(...)
        {
            p--;
        }
        

    }
        
        if(p%5==4)
        {
            cout<<endl;
        }
        else
        {
            cout<<‘\t‘;
        }
    }
}
void DisplayInt(int a[],int w,int m)
{
    if(w==1)
    {
        for(int q=0;q<m;q++)
        {
            cout<<a[q]<<‘\t‘;
            if(q%5==4)
            {
                cout<<endl;
            }
        }
    }
    else
    {};
}
void main()
{
    int p;
    do
    {
        system("cls");
        int a[1000],b[1000][2];
        int m,n,w;
        cout<<"请输入生成的四则运算题个数:";
        cin>>m;
        cout<<endl;
        cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";
        cin>>n;
        cout<<endl;
        if(n==1)
        {
            DealInt(m,a);
            cout<<endl;
        }
        else
        {
            DealFenshu(m,b);
            cout<<endl;
        }
        cout<<"是否输出答案(输入1则输出答案否则不输出答案)"<<endl;
        cin>>w;
        if(n==1)
        {
            DisplayInt(a,w,m);        
        }
        else
        {
            DisplayFenshu(b,w,m);
        }
        cout<<endl;
        cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;
        cin>>p;
        cout<<endl;
    }while(1==p);

}

 

复审代码总结:

       这个代码总体上编写的很不错。我根据教材进行了逐步复审,通过VS2015进行了运行,程序可以运行,通过测试四则运算程序也没有发生乱码或者闪退等bug。代码符合需求和规格说明;代码设计考虑的也相当周全,例如,在处理整数操作部分时,使用try/throw语句,避免除法分母为0的情况;代码也不是很难理解。他的代码对于我来说有很多值得学习的地方,我可以通过复审他的代码来提升我对于我自己的四则运算的一个提高。这次复审进行的也是相当愉快,颇有感悟。

 

 

 

推荐阅读