四则运算2的单元测试

  1 /*2013/3/12   李萌  20133079 */
  2 #include <iostream>
  3 #include <ctime>
  4 using namespace std;
  5 void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
  6 {
  7     while(int f=1)  
  8     {
  9         int Oper=rand()%19-9;
 10         if(Oper>0)             /*没有0*/
 11         {
 12             cout<<Oper;
 13             break;
 14         }
 15         if(Oper<0)
 16         {
 17             cout<<"("<<Oper<<")";
 18             break;
 19         }
 20     }
 21 }
 22 void Operands_2()/*-9到9  有0*/
 23 {
 24     while(int f=1)  /*产生操作数 (-9 -- 9)*/
 25     {
 26         int Oper=rand()%19-9;
 27         if(Oper>=0)/*有0*/
 28         {
 29             cout<<Oper;
 30             break;
 31         }
 32         else
 33         {
 34             cout<<"("<<Oper<<")";
 35             break;
 36         }
 37     }
 38 }
 39 void Operator_1()/*随机产生运算符   有乘除*/
 40 {
 41     int f=rand()%4;
 42     if(f==0) 
 43     {
 44         cout<<"+";
 45     }
 46     if(f==1)
 47     {
 48         cout<<"-";
 49     }
 50     if(f==2)
 51     {
 52         cout<<"*";
 53     }
 54     if(f==3)
 55     {
 56         cout<<"÷";
 57     }
 58 }
 59 void Operator_2()/*随机产生运算符   无乘除*/
 60 {
 61     int f=rand()%2;
 62     if(f==0) 
 63     {
 64         cout<<"+";
 65     }
 66     if(f==1)
 67     {
 68         cout<<"-";
 69     }
 70 }
 71 int Bracket_l(int left) /*随机产生左括号*/
 72 {
 73     int f=rand()%3;
 74     if(f<2)
 75     {
 76         return left;
 77     }
 78     if(f==2)
 79     {
 80         cout<<"(";
 81         return left+1;
 82     }
 83 }
 84 int Bracket_r(int right) /*随机产生右括号*/
 85 {
 86     int r=rand()%5;
 87     if(r==0)
 88     {
 89         return right;
 90     }
 91     if(r>0) /*产生右括号的概率大 */
 92     {
 93         cout<<")";
 94         return right+1;
 95     }
 96 }
 97 void Way_1() /*最多可支持10个数参与计算   有乘除 有括号 10以内 加减有负数 除法有余数*/
 98 {
 99     int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
100     int left=0,right=0,flag=0,left_1;
101     for(int i=0;i<length-1;i++)
102     {
103         left_1=left;
104         left=Bracket_l(left);
105         if(left_1!=left)     /*产生了左括号  flag=i*/
106         {
107             flag=i;
108         }
109         Opreands_1();
110         if(left>right&&flag!=i) /*左括号大于右括号的个数 and 产生左括号和右括号不在一个循环里  即不会产生“(随机数)” 这种无效情况*/
111         {
112             right=Bracket_r(right);
113         }
114         Operator_1();  /*有乘除*/
115     }
116     Opreands_1(); /*因为 一个操作数一个运算符   还缺少一个操作数 (-9 -- 9)*/
117     cout<<" = "<<endl;
118 }
119 
120 void main() /*主函数*/
121 {
122     srand((unsigned)time(0));
123     cout<<"--------------------------------------------------------------------------"<<endl;
124     cout<<"最多可支持10个数参与计算  有乘除 有括号 -9到9  加减有负数 除法有余数"<<endl;
125     for(int num=0;num<30;num++)
126     {
127         cout<<num+1<<"";
128         Way_1();
129     }
130 }

有乘除、有括号、-9到9 加减有负数  除法有余数     结果如图

  1 /*2013/3/12   李萌  20133079 */
  2 #include <iostream>
  3 #include <ctime>
  4 using namespace std;
  5 void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
  6 {
  7     while(int f=1)  
  8     {
  9         int Oper=rand()%19-9;
 10         if(Oper>0)             /*没有0*/
 11         {
 12             cout<<Oper;
 13             break;
 14         }
 15         if(Oper<0)
 16         {
 17             cout<<"("<<Oper<<")";
 18             break;
 19         }
 20     }
 21 }
 22 void Operands_2()/*-9到9  有0*/
 23 {
 24     while(int f=1)  /*产生操作数 (-9 -- 9)*/
 25     {
 26         int Oper=rand()%19-9;
 27         if(Oper>=0)/*有0*/
 28         {
 29             cout<<Oper;
 30             break;
 31         }
 32         else
 33         {
 34             cout<<"("<<Oper<<")";
 35             break;
 36         }
 37     }
 38 }
 39 void Operator_1()/*随机产生运算符   有乘除*/
 40 {
 41     int f=rand()%4;
 42     if(f==0) 
 43     {
 44         cout<<"+";
 45     }
 46     if(f==1)
 47     {
 48         cout<<"-";
 49     }
 50     if(f==2)
 51     {
 52         cout<<"*";
 53     }
 54     if(f==3)
 55     {
 56         cout<<"÷";
 57     }
 58 }
 59 void Operator_2()/*随机产生运算符   无乘除*/
 60 {
 61     int f=rand()%2;
 62     if(f==0) 
 63     {
 64         cout<<"+";
 65     }
 66     if(f==1)
 67     {
 68         cout<<"-";
 69     }
 70 }
 71 int Bracket_l(int left) /*随机产生左括号*/
 72 {
 73     int f=rand()%3;
 74     if(f<2)
 75     {
 76         return left;
 77     }
 78     if(f==2)
 79     {
 80         cout<<"(";
 81         return left+1;
 82     }
 83 }
 84 int Bracket_r(int right) /*随机产生右括号*/
 85 {
 86     int r=rand()%5;
 87     if(r==0)
 88     {
 89         return right;
 90     }
 91     if(r>0) /*产生右括号的概率大 */
 92     {
 93         cout<<")";
 94         return right+1;
 95     }
 96 }
 97 void Way_2()/*最多可支持10个数参与计算   无乘除 有括号 10以内 加减有负数 除法有余数*/
 98 {
 99     int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
100     int left=0,right=0,flag=0,left_1;
101     for(int i=0;i<length-1;i++)
102     {
103         left_1=left;
104         left=Bracket_l(left);
105         if(left_1!=left)     /*产生了左括号  flag=i*/
106         {
107             flag=i;
108         }
109         Operands_2();  /*产生操作数 (-9 -- 9)*/
110         if(left>right&&flag!=i) /*左括号大于右括号的个数 and 产生左括号和右括号不在一个循环里  即不会产生“(随机数)” 这种无效情况*/
111         {
112             right=Bracket_r(right);
113         }
114         Operator_2();/*无乘除*/
115     }
116     Operands_2();  /*因为 一个操作数一个运算符   还缺少一个操作数 (-9 -- 9)*/
117     for(int i=0;i<left-right;i++)
118     {
119         cout<<")";
120     }
121     cout<<" = "<<endl;
122 }
123 void main() /*主函数*/
124 {
125     srand((unsigned)time(0));
126     cout<<"--------------------------------------------------------------------------"<<endl;
127     cout<<"最多可支持10个数参与计算  无乘除 有括号 -9到9  加减有负数 除法有余数"<<endl;
128     for(int num=0;num<30;num++)
129     {
130         cout<<num+1<<"";
131         Way_2();
132     }
133 }
  1 /*2013/3/12   李萌  20133079 */
  2 #include <iostream>
  3 #include <ctime>
  4 using namespace std;
  5 void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
  6 {
  7     while(int f=1)  
  8     {
  9         int Oper=rand()%19-9;
 10         if(Oper>0)             /*没有0*/
 11         {
 12             cout<<Oper;
 13             break;
 14         }
 15         if(Oper<0)
 16         {
 17             cout<<"("<<Oper<<")";
 18             break;
 19         }
 20     }
 21 }
 22 void Operands_2()/*-9到9  有0*/
 23 {
 24     while(int f=1)  /*产生操作数 (-9 -- 9)*/
 25     {
 26         int Oper=rand()%19-9;
 27         if(Oper>=0)/*有0*/
 28         {
 29             cout<<Oper;
 30             break;
 31         }
 32         else
 33         {
 34             cout<<"("<<Oper<<")";
 35             break;
 36         }
 37     }
 38 }
 39 void Operator_1()/*随机产生运算符   有乘除*/
 40 {
 41     int f=rand()%4;
 42     if(f==0) 
 43     {
 44         cout<<"+";
 45     }
 46     if(f==1)
 47     {
 48         cout<<"-";
 49     }
 50     if(f==2)
 51     {
 52         cout<<"*";
 53     }
 54     if(f==3)
 55     {
 56         cout<<"÷";
 57     }
 58 }
 59 void Operator_2()/*随机产生运算符   无乘除*/
 60 {
 61     int f=rand()%2;
 62     if(f==0) 
 63     {
 64         cout<<"+";
 65     }
 66     if(f==1)
 67     {
 68         cout<<"-";
 69     }
 70 }
 71 int Bracket_l(int left) /*随机产生左括号*/
 72 {
 73     int f=rand()%3;
 74     if(f<2)
 75     {
 76         return left;
 77     }
 78     if(f==2)
 79     {
 80         cout<<"(";
 81         return left+1;
 82     }
 83 }
 84 int Bracket_r(int right) /*随机产生右括号*/
 85 {
 86     int r=rand()%5;
 87     if(r==0)
 88     {
 89         return right;
 90     }
 91     if(r>0) /*产生右括号的概率大 */
 92     {
 93         cout<<")";
 94         return right+1;
 95     }
 96 }
 97 
 98 void Way_3() /*最多可支持10个数参与计算  有乘除 无括号(负数用括号括起来) -9到9  加减有负数 除法有余数*/
 99 {
100     int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
101     int left=0,right=0,flag=0,left_1;
102     for(int i=0;i<length-1;i++)
103     {
104         Opreands_1(); /*-9-9除0*/
105         Operator_1(); /*有乘除*/
106     }
107     Opreands_1();  /* 因为 一个操作数一个运算符   还缺少一个操作数 (-9 -- 9)*/
108     cout<<" = "<<endl;
109 }
110 void main() /*主函数*/
111 {
112     srand((unsigned)time(0));
113     
114     cout<<"--------------------------------------------------------------------------"<<endl;
115     cout<<"最多可支持10个数参与计算  有乘除 无括号(负数用括号括起来) -9到9  加减有负数 除法有余数"<<endl;
116     for(int num=0;num<30;num++)
117     {
118         cout<<num+1<<"";
119         Way_3();
120     }
121     
122 }

 

  1 /*2013/3/12   李萌  20133079 */
  2 #include <iostream>
  3 #include <ctime>
  4 using namespace std;
  5 void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
  6 {
  7     while(int f=1)  
  8     {
  9         int Oper=rand()%19-9;
 10         if(Oper>0)             /*没有0*/
 11         {
 12             cout<<Oper;
 13             break;
 14         }
 15         if(Oper<0)
 16         {
 17             cout<<"("<<Oper<<")";
 18             break;
 19         }
 20     }
 21 }
 22 void Operands_2()/*-9到9  有0*/
 23 {
 24     while(int f=1)  /*产生操作数 (-9 -- 9)*/
 25     {
 26         int Oper=rand()%19-9;
 27         if(Oper>=0)/*有0*/
 28         {
 29             cout<<Oper;
 30             break;
 31         }
 32         else
 33         {
 34             cout<<"("<<Oper<<")";
 35             break;
 36         }
 37     }
 38 }
 39 void Operator_1()/*随机产生运算符   有乘除*/
 40 {
 41     int f=rand()%4;
 42     if(f==0) 
 43     {
 44         cout<<"+";
 45     }
 46     if(f==1)
 47     {
 48         cout<<"-";
 49     }
 50     if(f==2)
 51     {
 52         cout<<"*";
 53     }
 54     if(f==3)
 55     {
 56         cout<<"÷";
 57     }
 58 }
 59 void Operator_2()/*随机产生运算符   无乘除*/
 60 {
 61     int f=rand()%2;
 62     if(f==0) 
 63     {
 64         cout<<"+";
 65     }
 66     if(f==1)
 67     {
 68         cout<<"-";
 69     }
 70 }
 71 int Bracket_l(int left) /*随机产生左括号*/
 72 {
 73     int f=rand()%3;
 74     if(f<2)
 75     {
 76         return left;
 77     }
 78     if(f==2)
 79     {
 80         cout<<"(";
 81         return left+1;
 82     }
 83 }
 84 int Bracket_r(int right) /*随机产生右括号*/
 85 {
 86     int r=rand()%5;
 87     if(r==0)
 88     {
 89         return right;
 90     }
 91     if(r>0) /*产生右括号的概率大 */
 92     {
 93         cout<<")";
 94         return right+1;
 95     }
 96 }
 97 
 98 
 99 void Way_4()  /*最多可支持10个数参与计算  无乘除 无括号(负数用括号括起来) -9到9  加减有负数 除法有余数*/
100 {
101     int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
102     int left=0,right=0,flag=0,left_1;
103     for(int i=0;i<length-1;i++)
104     {
105         Operands_2();  /*产生操作数 (-9 -- 9)*/
106         Operator_2(); /*无乘除*/
107     }
108     Operands_2();  /* 因为 一个操作数一个运算符   还缺少一个操作数 (-9 -- 9)*/
109     cout<<" = "<<endl;
110 }
111 
112 void main() /*主函数*/
113 {
114     srand((unsigned)time(0));
115     
116     cout<<"--------------------------------------------------------------------------"<<endl;
117     cout<<"最多可支持10个数参与计算  无乘除 无括号(负数用括号括起来) -9到9  加减有负数 除法有余数"<<endl;
118     for(int num=0;num<30;num++)
119     {
120         cout<<num+1<<"";
121         Way_4();
122     }124 
125 }

 

 

 

 

 

 

 

/*2013/3/12   李萌  20133079 */
#include <iostream>
#include <ctime>
using namespace std;
void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
{
    while(int f=1)  
    {
        int Oper=rand()%19-9;
        if(Oper>0)             /*没有0*/
        {
            cout<<Oper;
            break;
        }
        if(Oper<0)
        {
            cout<<"("<<Oper<<")";
            break;
        }
    }
}
void Operands_2()/*-9到9  有0*/
{
    while(int f=1)  /*产生操作数 (-9 -- 9)*/
    {
        int Oper=rand()%19-9;
        if(Oper>=0)/*有0*/
        {
            cout<<Oper;
            break;
        }
        else
        {
            cout<<"("<<Oper<<")";
            break;
        }
    }
}
void Operator_1()/*随机产生运算符   有乘除*/
{
    int f=rand()%4;
    if(f==0) 
    {
        cout<<"+";
    }
    if(f==1)
    {
        cout<<"-";
    }
    if(f==2)
    {
        cout<<"*";
    }
    if(f==3)
    {
        cout<<"÷";
    }
}
void Operator_2()/*随机产生运算符   无乘除*/
{
    int f=rand()%2;
    if(f==0) 
    {
        cout<<"+";
    }
    if(f==1)
    {
        cout<<"-";
    }
}
int Bracket_l(int left) /*随机产生左括号*/
{
    int f=rand()%3;
    if(f<2)
    {
        return left;
    }
    if(f==2)
    {
        cout<<"(";
        return left+1;
    }
}
int Bracket_r(int right) /*随机产生右括号*/
{
    int r=rand()%5;
    if(r==0)
    {
        return right;
    }
    if(r>0) /*产生右括号的概率大 */
    {
        cout<<")";
        return right+1;
    }
}

void Way_5()  /*最多可支持10个数参与计算  有乘除 无括号(负数用括号括起来) 0-9  加减有负数 除法有余数*/
{
    int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
    int left=0,right=0,flag=0,left_1;
    for(int i=0;i<length-1;i++)
    {
        cout<<rand()%9+1; /*1-9除0*/
        Operator_1(); /*有乘除*/
    }
    cout<<rand()%9+1;  /* 因为 一个操作数一个运算符   还缺少一个操作数 (1 -- 9)*/
    cout<<" = "<<endl;
}

void main() /*主函数*/
{
    srand((unsigned)time(0));
    
    cout<<"最多可支持10个数参与计算  有乘除 无括号(负数用括号括起来) 0到9  加减有负数 除法有余数"<<endl;
    for(int num=0;num<30;num++)
    {
        cout<<num+1<<"";
        Way_5();
    }

}

 

/*2013/3/12   李萌  20133079 */
#include <iostream>
#include <ctime>
using namespace std;
void Opreands_1()  /*产生操作数 (-9 -- 9)除0*/
{
    while(int f=1)  
    {
        int Oper=rand()%19-9;
        if(Oper>0)             /*没有0*/
        {
            cout<<Oper;
            break;
        }
        if(Oper<0)
        {
            cout<<"("<<Oper<<")";
            break;
        }
    }
}
void Operands_2()/*-9到9  有0*/
{
    while(int f=1)  /*产生操作数 (-9 -- 9)*/
    {
        int Oper=rand()%19-9;
        if(Oper>=0)/*有0*/
        {
            cout<<Oper;
            break;
        }
        else
        {
            cout<<"("<<Oper<<")";
            break;
        }
    }
}
void Operator_1()/*随机产生运算符   有乘除*/
{
    int f=rand()%4;
    if(f==0) 
    {
        cout<<"+";
    }
    if(f==1)
    {
        cout<<"-";
    }
    if(f==2)
    {
        cout<<"*";
    }
    if(f==3)
    {
        cout<<"÷";
    }
}
void Operator_2()/*随机产生运算符   无乘除*/
{
    int f=rand()%2;
    if(f==0) 
    {
        cout<<"+";
    }
    if(f==1)
    {
        cout<<"-";
    }
}
int Bracket_l(int left) /*随机产生左括号*/
{
    int f=rand()%3;
    if(f<2)
    {
        return left;
    }
    if(f==2)
    {
        cout<<"(";
        return left+1;
    }
}
int Bracket_r(int right) /*随机产生右括号*/
{
    int r=rand()%5;
    if(r==0)
    {
        return right;
    }
    if(r>0) /*产生右括号的概率大 */
    {
        cout<<")";
        return right+1;
    }
}

void Way_6()  /*最多可支持10个数参与计算  无乘除 无括号(负数用括号括起来) 0到9  加减有负数 除法有余数*/
{
    int length=rand()%9+2; /*随机产生表达式中操作数的个数 2-10 个*/
    int left=0,right=0,flag=0,left_1;
    for(int i=0;i<length-1;i++)
    {
        cout<<rand()%10;  /*产生操作数 (0-- 9)*/
        Operator_2(); /*无乘除*/
    }
    cout<<rand()%10;  /* 因为 一个操作数一个运算符   还缺少一个操作数 (0-- 9)*/
    cout<<" = "<<endl;
}
void main() /*主函数*/
{
    srand((unsigned)time(0));
    
    cout<<"最多可支持10个数参与计算  无乘除 无括号(负数用括号括起来) 0到9  加减有负数 除法有余数"<<endl;
    for(int num=0;num<30;num++)
    {
        cout<<num+1<<"";
        Way_6();
    }
}

posted @ 2016-03-13 16:08  _小青年  阅读(169)  评论(2编辑  收藏  举报