指定类型数据随机生成方法源代码

#include
#include
#include
#include
using namespace std;
int m;
class Random
{
private:
int year,month,day;
public:
Data(int y=0,int m=0,int d=0);
~Random();
int Getyear();
int Getmonth();
int Getday();
double Randomdouble();
void Printdata();
void menu();
};
class Int
{
public:
void RandomInt();
void limit(); //产生有范围限制的随机数
void unlimit(); //产生无范围限制的随机数
void special(); //实现一定包含某数
void special1(); //实现一定不包含某数
void Print();
private:
char q,spe,unspe; //spe指的是特定的数据,unspec不包含某个特定的数
int *a;
int n,min,max;
};
void Int::RandomInt()
{
cout<<" **********选择的是int型!*********"<cout<<"请选择数据生成要求!\n";
cout<<"1.数据是否有范围(Y/N) ";
cin>>q;
cout<<"2.是否包含某特定数据(Y/N) ";
cin>>spe;
cout<<"3.是否不包含某特定数据(Y/N) ";
cin>>unspe;
if(q=='Y') limit();
else unlimit();
if(spe=='Y') special();
if(unspe=='Y') special1();
}
void Int::limit()
{
cout<<" 请输入数据的范围!\n";
cout<<" 要生成多少个数 ";
cin>>n;
do{
cout<<" 输入数据的下界 ";
cin>>min;
cout<<" 输入数据的上界 ";
cin>>max;
if(max-min+1cout<<"范围的随机数少于需要输出的个数!\n"<<"请重新输入!\n"; //随机数的个数应该大于范围内的数
}while(max-min+1srand(time(NULL));
a=new int [n];
int length=max-min+1;
int *b=new int [length];
for(int i=0;ib[i]=min+i; //将min到max之间所有值赋值给b[i]
for(i=1;i<=n;i++)
{
int l;
l=rand()%length+1;
a[i-1]=b[l-1]; //随机产生b[l-1]赋值给a[i-1]
for(int j=l;jb[j-1]=b[j]; //删去已产生的数b[l-1]
length--;
} //保证随机数在min到max之间
}
void Int::unlimit()
{
cout<<"要生成多少个数 ";
cin>>n;
srand(time(NULL));
a=new int [n];
for(int i=0;ia[i]=rand();
}
void Int::special() //包含某个数
{
cout<<"要包含的数是: ";
int x,i=0;
cin>>x;
while(xmax)
{
cout<<"数据不在范围之内,请重新输入! ";
cin>>x;
}
while(i{
if(a[i]==x) break; //原数据中有x,直接跳出
else i++;
}
if(i==n) a[i-1]=x; //如果没有,最后一个值赋为x
}
void Int::special1()
{
cout<<"不要包含的数是: ";
int x,i=0;
cin>>x;
while(i{
if(a[i]==x)
{
if(q=='Y') a[i]=(x+1)%(max-min)+min; //如果有范围限制,产生范围内的值
else a[i

]=x+1;
}
else i++;
}
}
void Int::Print()
{
cout<<"\n产生的随机数是:\n";
for(int i=0;icout<cout<}
double Random::Randomdouble()
{ double q;
int t =rand();
int m=rand();
q=(double)t/(double)m;
cout<
return 0;
}
Random::Data(int y,int m,int d)
{
srand((unsigned)time(NULL));
y=rand()%2500+1;
m=rand()%12+1;
if(m==2)
d=rand()%28+1;
if(m%2!=0)
d=rand()%31+1;
else
d=rand()%30+1;
if(year % 4 == 0 &&year % 100 != 0 || year % 400 == 0 )
{
if(month == 2)
{
day = rand() % 28 + 1;
}
}
year=y;
month=m;
day=d;

}

class String
{
public:
void RandomStr();
void limit();
void unlimit();
void special();
void special1();
void Print();
private:
int n,length;
char **p; //存放生成的n个字符串
char fwei,tedg1,tedg2;
};
void String::RandomStr()
{
cout<<"您选择的是string型!\n";
cout<<"请选择数据生成要求!\n";
cout<<"要生成多少个字符串: ";
cin>>n;
cout<<"1.字符串是否有长度限制(Y/N) ";
cin>>fwei;
cout<<"2.是否包含某特定字符串(Y/N) ";
cin>>tedg1;
cout<<"3.是否不包含某特定字符串(Y/N) ";
cin>>tedg2;
if(fwei=='Y') limit();
else unlimit();
if(tedg1=='Y') special();
if(tedg2=='Y') special1();
}
void String::limit()
{
cout<<"字符串的最大长度为: ";
cin>>length;
p=new char*[n];
srand(time(NULL));
for(int i=0;i{
int m=rand()%length+1;
p[i]=new char [m+1];
for(int j=0;j{
p[i][j]=rand()%74+48;
}
p[i][j]='\0';
}
}
void String::unlimit()
{
p=new char*[n];
srand(time(NULL));
for(int i=0;i{
int m=rand()%10+1;
p[i]=new char [m+1];
for(int j=0;jp[i][j]=rand()%74+48;
p[i][j]='\0';
}
}
void String::special()
{
char *x;
if(fwei=='Y') x=new char [length];
else
{
cout<<"要包含的字符串的长度为: ";
int l;
cin>>l;
x=new char [l];
}
cout<<"要包含的字符串是: ";
cin>>x;
int i=0;
while(i{
if(!strcmp(p[i],x)) break;
else i++;
}
if(i==n) p[i-1]=x;
}
void String::special1()
{
char *x;
cout<<"不包含的字符串的长度为: ";
int l;
cin>>l;
x=new char [l];
cout<<"不包含的字符串是: ";
cin>>x;
int i=0;
while(i{
if(!strcmp(p[i],x)) strncat(p[i],x,1);
else i++;
}
}
void String::Print()
{
cout<<"\n产生的字符串是:\n";
for(int i=0;icout<cout<}
Random::~Random()
{
cout<<"deconstructing..."<}
int Random::Getyear()
{
return year;
}

int Random::Getmonth()
{
return month;
}

int Random::Getday()
{
return day;
}

void Random::Printdata()
{
cout<}
void Random::menu()
{

cout<<" **************************************"<

cout<<" >>>>>>>>>> 1.调用Data型 <<<<<<<<<<"<cout<<" >>>>>>>>>> 2.调用 Int型 <<<<<<<<<<"<cout<<" >>>>>>>>>> 3.调用double型 <<<<<<<<<<"<cout<<" >>>>>>>>>> 4.调用str类 <<<<<<<<<<"<cout<<" **************************************"<cin>>m;
}
int main()
{ system("color 0A");
Random t;
Int p;
//Double q;
String s;
while(1){

t.menu();
switch(m)
{ case 1:
cout<<" *****************输出日期****************"<t.Data (2014,2,3);
t.Printdata();break;
case 2 :

// cout<<" *********输出小余1000的整数**********"<p.RandomInt();
p.Print();
break;
case 3 :

cout<<" *****输出double数*****"<t.Randomdouble();
//q.Print();

break;
case 4 :
cout<<" **输出字符串**"<s.RandomStr();
s.Print();
break;
}
}
return 0;
}

相关文档
最新文档