zoukankan      html  css  js  c++  java
  • 四则运算2单元测试

    一、程序代码

    //HaoYing 2016.3.7 信1301-2班 20132919
    //课堂实验2--四则运算2
    #include<iostream>
    #include<stdlib.h>
    #include<time.h>
    #include<fstream>
    using namespace std;
    
    ofstream ofile("c:calculate.txt",ios::trunc);//若输出到文件,则输出到c:calculate.txt中
    
    void jiajian(int type,int choose)//输出加减符号的函数
    {
        if(choose==0)
        {
            if(type==1)
            {
                cout<<"+";
            }
            else
                ofile<<"+";
        }
        else
        {
            if(type==1)
            {
                cout<<"-";
            }
            else
                ofile<<"-";
        }
    }
    void chengchu(int type,int choose)//实现输出乘除符号的函数
    {
        if(choose==0)
        {
            if(type==1)
            {
                cout<<"*";
            }
            else
                ofile<<"*";
        }
        else
        {
            if(type==1)
            {
                cout<<"/";
            }
            else
                ofile<<"/";
        }
    }
    void kuohao1(int type)//实现输出左括号的函数
    {
        if(type==1)
        {
            cout<<"(";
        }
        else
            ofile<<"(";
    }
    void kuohao2(int type)//实现输出右括号的函数
    {
        if(type==1)
        {
            cout<<")";
        }
        else
            ofile<<")";
    }
    void main()
    {
    
        double n[10];//用来存放每道题的数字
        srand((int)time(NULL));//变换随机数
        int next,down,up,minus,remainder,bracket,have,edg,length,length1,length2,length3,output,choose,choose1=1,p2,p1;
        int i,j,k,k1,k2,m;//i,j,k用于循环计数器,k1,k2分别为左括号计数器和右括号计数器,m为输出选择控制器
        cout<<"请设置题数:";
        cin>>next;//实现可定制功能
        cout<<"请设置打印方式:1.屏幕显示  2.保存到文件夹  ";
        cin>>output;//实现打印方式功能
        cout<<"请设置是否有乘除法:1.是  2.否  ";
        cin>>have;//实现是否有乘除法功能
        cout<<"请设置是否有括号:1.是  2.否  ";
        cin>>bracket;//实现是否有括号功能
        cout<<"请设置数值范围,依次输入下限和上限:";
        cin>>down;
        cin>>up;
        if(have==1&&bracket==2)
        {
            cout<<"请设置除法是否有余数:1.是  2.否  ";
            cin>>remainder;
        }
        if(have==2&&bracket==2)
        {
            cout<<"请设置加减法是否有负数:1.是  2.否  ";
            cin>>minus;//实现加减法是否有负数的功能
        }
        if(output==2)
        {
            if(!ofile.is_open())
            {
                cout<<"打开文件失败!"<<endl;
            }
            cout<<"已经成功保存到了C盘的calculate.txt中!"<<endl;
        }
        double p[1000];
        int q[1000];
        for(j=0;j<next;j++)//四则运算的题数为next的值
        {
            length=2+(rand()%100)%8;
            q[j]=length;
            length1=length-2;//括号的对儿数最大值
            for(i=0;i<length;i++)
            {
                edg=(rand()%100)%2;
                if(remainder==2)//如果要求除法没有余数
                    n[i]=down+rand()%(up-down);
                else
                {
                    if(edg==0)
                        n[i]=down+rand()%(up-down);//随机整数范围【down,up】
                    else
                        n[i]=(down+rand()%((up-1)-down))+rand()%100/100.0;//随机数范围【down,up】的随机小数
                }
                p[j]=n[0];
            }
            for(k=0;k<j;k++)//避免题目重复
            {
                if(q[k]==q[j])
                {
                    if(edg==0)
                        p[j]=down+rand()%(up-down);//随机整数范围【down,up】
                    else
                        p[j]=(down+rand()%((up-1)-down))+rand()%100/100.0;//随机数范围【down,up】的随机小数
                }
                    break;
            }
            n[0]=p[j];
            if(have==2&&bracket==2)//如果只有加减法
            {
                if(output==1)
                {
                    cout<<n[0];
                }
                else
                    ofile<<n[0];
                for(i=1;i<length;i++)
                {
                    choose=(rand()%100)%2;
                    if(choose1==0)
                    {
                        jiajian(output,0);
                        choose1=1;
                    }
                    else
                        jiajian(output,choose);    
                    if(minus==2)//设置加减法没有负数
                    {
                        if(choose==1)
                        {
                            if(n[i]>n[i-1])
                            {
                                if(n[i-1]==down||(n[i-1]-down)<1)
                                    n[i]=down;
                                else
                                    n[i]=down+rand()%(int(n[i-1])-down);
                                choose1=0;
                            }
                        }
                    }
                    if(output==1)
                    {
                        cout<<n[i];
                    }
                    else
                        ofile<<n[i];
                }
                if(output==1)
                {
                    cout<<"="<<endl;
                }
                else
                    ofile<<"="<<endl;
            }
            else if(have==1&&bracket==2)//有乘除无括号
            {
                if(output==1)
                {
                    cout<<n[0];
                }
                else
                    ofile<<n[0];
                for(i=1;i<length;i++)
                {
                    if((rand()%100)%2==0)
                    {
                        choose=(rand()%100)%2;
                        jiajian(output,choose);
                    }
                    else
                    {
                        choose=(rand()%100)%2;
                        chengchu(output,choose);
                        if(n[i]==0)
                            n[i]=1+rand()%(up-1);
                        if(remainder==2)//如果设置除法没有余数,则令被除数为除数的整数倍即可
                        {
                            if(choose==1)
                            {
                                if(n[i-1]==1)
                                    n[i]=1;
                                else
                                {
                                    p2=int(n[i-1]);
                                    p1=1+rand()%(p2-1);
                                    while(p2%p1!=0)
                                    {
                                        p1=1+rand()%(p2-1);
                                    }
                                    n[i]=p1;
                                }
                            }
                        }
                    }
                    if(output==1)
                    {
                        cout<<n[i];
                    }
                    else
                        ofile<<n[i];
                }
                if(output==1)
                {
                    cout<<"="<<endl;
                }
                else
                    ofile<<"="<<endl;
            }
            else if(have==1&&bracket==1)//有乘除有括号
            {
                k1=0,k2=0;
                length2=1+rand()%(length-1);//左括号数
                length3=length2;//右括号数
                if((rand()%100)%2==0&&length!=2)
                {
                    if(output==1)
                    {
                        cout<<"("<<n[0];
                    }
                    else
                        ofile<<"("<<n[0];
                    k1++;
                }
                else
                    if(output==1)
                    {
                        cout<<n[0];
                    }
                    else
                        ofile<<n[0];
                   for(i=1;i<length;i++)
                {     
                    m=(rand()%100)%3;
                    if((rand()%100)%2==0)
                    {
                        choose=(rand()%100)%2;
                        jiajian(output,choose);
                    }
                    else
                    {
                        choose=(rand()%100)%2;
                        chengchu(output,choose);
                    }
                    if(m==0&&i!=length-1)
                    {
                        if(k1<=length2)
                        {
                            if(output==1)
                            {
                                cout<<"("<<n[i];
                            }
                            else
                                ofile<<"("<<n[i];
                            k1++;
                        }
                        else
                        {
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                        }
                    }
                    else if(m==1&&k1!=0)
                    {
                        if(k2<=length3)
                        {
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                            if(k1>k2)
                            {
                                for(k=0;k<k1-k2;k++)
                                {
                                    if(output==1)
                                    {
                                        cout<<")";
                                    }
                                    else
                                        ofile<<")";
                                }
                                k2=k2+k;
                            }
                        }
                        else
                        {
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                        }
                    }
                    else
                    {
                        if(output==1)
                        {
                            cout<<n[i];
                        }
                        else
                            ofile<<n[i];
                        if(k1>k2)
                        {
                            for(k=0;k<k1-k2;k++)
                            {
                                if(output==1)
                                {
                                    cout<<")";
                                }
                                else
                                    ofile<<")";
                            }
                            k2=k2+k;
                        }
                    }
                }
                if(output==1)
                {
                    cout<<"="<<endl;
                }
                else
                    ofile<<"="<<endl;
            }
            else//没有乘除有括号
            {
                k1=0,k2=0;//k1,k2分别为左括号计数器和右括号计数器
                length2=1+rand()%(length-1);//左括号数
                length3=length2;//右括号数
                if((rand()%100)%2==0&&length!=2)
                {
                    if(output==1)
                    {
                        cout<<"("<<n[0];
                    }
                    else
                        ofile<<"("<<n[0];
                    k1++;
                }
                else
                    if(output==1)
                    {
                        cout<<n[0];
                    }
                    else
                        ofile<<n[0];
                   for(i=1;i<length;i++)
                {     
                    m=(rand()%100)%3;
                    choose=(rand()%100)%2;
                    jiajian(output,choose); 
                    if(m==0&&i!=length-1)
                    {
                        if(k1<=length2)
                        {
                            if(output==1)
                            {
                                cout<<"("<<n[i];
                            }
                            else
                                ofile<<"("<<n[i];
                            k1++;
                        }
                        else
                        {
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                        }
                    }
                    else if(m==1&&k1!=0)
                    {
                        if(k2<=length3)
                        {
                            
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                            if(k1>k2)
                            {
                                for(k=0;k<k1-k2;k++)
                                {
                                    if(output==1)
                                    {
                                        cout<<")";
                                    }
                                    else
                                        ofile<<")";
                                 }
                                k2=k2+k;
                            }
                        }
                        else
                        {
                            if(output==1)
                            {
                                cout<<n[i];
                            }
                            else
                                ofile<<n[i];
                        }
                    }
                    else
                    {
                        if(output==1)
                        {
                            cout<<n[i];
                        }
                        else
                            ofile<<n[i];
                        if(k1>k2)
                        {
                            for(k=0;k<k1-k2;k++)
                            {
                                if(output==1)
                                {
                                    cout<<")";
                                }
                                else
                                    ofile<<")";
                            }
                            k2=k2+k;
                        }
                    }
                }
                if(output==1)
                {
                    cout<<"="<<endl;
                }
                else
                    ofile<<"="<<endl;
            }
       }
       ofile.close();
    }

    二、单元测试
    1.算式输出是否正确

    测试用例:选择输出10个式子,有加减和乘除运算,有括号,查看屏幕是否输出正确

    预期结果:屏幕上出现10个式子,并且有加减乘除符号,有括号的输出,数字和符号组合无误。

    实际结果:正确输出10个算式,每个式子都正确。

    2.检测输入的数值范围产生的数是否正确

    测试用例:选择输出40个式子,有加减和乘除运算,有括号,查看屏幕中的数值是否符合区间

    预期结果:屏幕上出现40个式子,有加减和乘除运算,有括号,数值符合区间,且随机组合

    实际结果:正确输出40个式子,有加减和乘除运算,有括号,数值符合区间

    3.括号是否匹配

    测试用例:选择输出20个式子,无乘除有括号,看左右括号是否相等,以及括号的位置是否正确

    预期结果:出现20个无乘除有括号的式子,左括号的数量等于右括号的数量。

    实际结果:正确输出20个式子,括号匹配正确。

    4.输出到文件时是否会出现乱码?

    测试用例:输入25个算式,无乘除有括号,看结果中是否有乱码

    预期结果:出现25个无乘除有括号的式子,没有乱码的出现

    实际结果:正确输出25个式子,没有乱码出现

    5.当数值范围里出现零值时,在除法中是否会出现除数为零而导致程序中断

    测试用例:输入50个式子,有乘除无括号,看是否会出现问题

    预期结果:出现50个无乘除有括号的式子,没有问题的出现

    实际结果:正确输出50个式子,没有问题出现

    6.检测余数是否可以为零

    检测用例:输入30个式子,有乘除无括号,看在指定无余数的情况下被除数是否能被除的尽

    预期结果:出现30个有乘除无括号的式子,被除数能被除的尽

    实际结果:正确出现30个式子,被除数能被除的尽

  • 相关阅读:
    extjs2.0
    抽象类和接口的选择
    获得汉字字符串的首字母
    快速找回桌面快捷方式
    vs2008破解90天限制
    SQL Server索引的使用和优化
    SQL Server 索引结构及其使用
    桥接模式(Bridge)体验
    vs2008 Working with jQuery
    利用索引来提高速度
  • 原文地址:https://www.cnblogs.com/haoying1994/p/5269411.html
Copyright © 2011-2022 走看看