四则运算2

设计思路:

将乘除法有无余数和加减有无负数封装成两个独立的函数,主函数进行调度。

截图:

 

程序代码:


#include<iostream>
#include<ctime>
using namespace std;
void sz()
{
    int f;//数的范围
    cout<<"请输入数的上限:例:(0~99)输入100"<<endl;
    cin>>f;   
    int x,y,z,t;   //x,y分别表示两个数,z控制加减乘除
    int e;//控制每行输出的道数
    srand(time(NULL));//避免重复
    int n;
    int b;//负数的处理
    int a;//行间距
    cout<<"请输入题的道数;"<<endl;
    cin>>n;     
    cout<<"加减有无负数(0有,1无)"<<endl;
    cin>>b;
    cout<<"请输入每行需要打印的题数:"<<endl;
    cin>>e;
    cout<<"请输入行间距:"<<endl;
    cin>>a;
   
    cout<<"********************************"<<endl;
    for(int i=0;i<n;i++)
    {
        x=rand()%f;
        y=rand()%f;
        z=rand()%4;
        switch(z)
        {
        case 0:
            cout<<i+1<<"、 "<<x<<"+"<<y<<"="<<"     ";
            break;
           
        case 1:
            if(b==1)
            {
            if(x<y)
            {
                t=x;
                x=y;
                y=t;
            }
            cout<<i+1<<"、 "<<x<<"-"<<y<<"="<<"     ";
            break;
            }
            else if(b==0)
            {
            cout<<i+1<<"、 "<<x<<"-"<<y<<"="<<"     ";
            }
        case 2:
            cout<<i+1<<"、 "<<x<<"*"<<y<<"="<<"     ";
            break;
        case 3:
            if(y!=0)
            {
            cout<<i+1<<"、 "<<x<<"/"<<y<<"="<<"     ";
            }
            else
            {
                i=i-1;
            }
            break;
        default:
            cout<<"超出测试范围"<<endl;
            break;
        }
        if((i+1)%e==0)
        {
            for(int c=0;c<=a;c++)
            {
                cout<<endl;
            }
        }
        if((i+1)==n)
        {
            cout<<endl;
        }
    }
}


void sz1()
{
      int f;//数的范围
    cout<<"请输入数的上限:例:(0~99)输入100"<<endl;
    cin>>f;   
    int x,y,z,t;   //x,y分别表示两个数,z控制加减乘除
    int e;//控制每行输出的道数
    srand(time(NULL));//避免重复
    int n;
    int b;//负数的处理
    int a;//行间距
    cout<<"请输入题的道数;"<<endl;
    cin>>n;     
    cout<<"加减有无负数(0有,1无)"<<endl;
    cin>>b;
    cout<<"请输入每行需要打印的题数:"<<endl;
    cin>>e;
    cout<<"请输入行间距:"<<endl;
    cin>>a;
    cout<<"********************************"<<endl;
    for(int i=0;i<n;i++)
    {
        x=rand()%f;
        y=rand()%f;
        z=rand()%2;
        switch(z)
        {
        case 0:
            cout<<i+1<<"、 "<<x<<"+"<<y<<"="<<"     ";
            break;
        case 1:
            if(b==1)
            {
            if(x<y)
            {
                t=x;
                x=y;
                y=t;
            }
            cout<<i+1<<"、 "<<x<<"-"<<y<<"="<<"     ";
            }
            else if(b==0)
            {
                cout<<i+1<<"、 "<<x<<"-"<<y<<"="<<"     ";
            }
            break;
        default:
            cout<<"超出测试范围"<<endl;
            break;
        }
        if((i+1)%e==0)
        {
            for(int c=0;c<=a;c++)
            {           
                cout<<endl;
            }
        }
        if((i+1)==n)
        {
            cout<<endl;
        }
    }
}



int main()
{


    char ch;
    cout<<"是否支持乘除:(Y/N)"<<endl;
    cin>>ch;
    if(ch=='Y'||ch=='y')
    {
        sz();   
        cout<<"********************************"<<endl;
    }
   
    else if(ch=='N'||ch=='n')
    {
        sz1();
        cout<<"********************************"<<endl;
    }
    else
    {
        cout<<"输入错误,无法为您出题!"<<endl;
    }
   
    return 0;
}

 

 代码行数:

171行

个人体会:

本次实验对于我是极大的挑战,除了乘除余数的问题、加减法有无负数、控制台输出控制(行,道数),避免重复,其他要求基本没实现,总的来说,是基础太差,编程量较少。对书本的知识掌握潦潦草草。

posted on 2016-03-12 18:12  gaga123456  阅读(234)  评论(1编辑  收藏  举报

导航