天天看點

作業三(2)

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的情況;代碼也不是很難了解。他的代碼對于我來說有很多值得學習的地方,我可以通過複審他的代碼來提升我對于我自己的四則運算的一個提高。這次複審進行的也是相當愉快,頗有感悟。