软件工程个人作业01

设计思想:(1)用随机数0.1.2.3通过swicth语句来控制+-*/.

          (2)用while语句来处理真分数.

          (3)用try  catch语句保证分母不为0.

源代码:

#include<iostream>

#include<stdlib.h>

#include<conio.h>

using namespace std;

 

void DFenshu(int m,int a[][2])

{

for(int p=0;p<m;p++)

    {

        int i=(int)rand()%10;//取0-9之间的随机数

        int j=(int)rand()%10;

        while(j==0||i>=j)//使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()%4;

        switch(k)

        {

            case 0:

                cout<<"("<<i<<"/"<<j<<")"<<"+"<<"("<<x<<"/"<<y<<")"<<"=";

                

                break;

            case 1:

                cout<<"("<<i<<"/"<<j<<")"<<"-"<<"("<<x<<"/"<<y<<")"<<"=";

                

                break;

            case 2:

                cout<<"("<<i<<"/"<<j<<")"<<"*"<<"("<<x<<"/"<<y<<")"<<"=";

                

                break;

            case 3:

                

                cout<<"("<<i<<"/"<<j<<")"<<"/"<<"("<<x<<"/"<<y<<")"<<"=";

            }

            

            if(p%5==4)

            {

                cout<<endl;

            }

            else

            {

                cout<<'\t';

            }

    }

 

}

 

 void DInt(int m,int a[])

 {

 for(int p=0;p<m;p++)

 {

 int i=(int)rand()%100;

     int j=(int)rand()%100;

 

 

  int k=(int)rand()%4;

switch(k)

case 0:

a[p]=i+j;

cout<<i<<"+"<<j<<"=";

 

break;

case 1:

a[p]=i-j;

cout<<i<<"-"<<j<<"=";

 

break;

case 2: 

a[p]=i*j;

cout<<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 main()

 {

    int p;

    do

    {

        

        int a[1000],b[1000][2];//题数范围.

        int m,n,w;

        cout<<"请输入生成的四则运算题个数;

        cin>>m;

        cout<<endl;

        cout<<"请输入要生成的四则运算种类(输入1为整数,否则为真分数):";

        cin>>n;

        cout<<endl;

        if(n==1)                                                                                                                                                                                                                                                                                                            

        {

            DInt(m,a);

            cout<<endl;

        }

        else

        {

            DFenshu(m,b);

            cout<<endl;

        }

       

        cout<<"是否继续生成运算题(输入1则生成否则不生成)"<<endl;

        cin>>p;

        cout<<endl;

    }

while(p==1);

 

}

程序截图:

上课未按时完成的原因:请假。

posted @ 2016-03-10 15:14  蝈蝈gl  阅读(139)  评论(0编辑  收藏  举报