📄 随机时间.cpp
字号:
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
const double PI=4*atan(1);
const double INIT_SEED=0.123456789;
void showPosInt(int nVal,char cTrail)
{
//exit if nVal is not positive or zero
if(nVal<0)
return;
if(nVal<10)
cout << '0' << nVal;
else
cout << nVal;
if(cTrail !='\0')
cout << cTrail;
else
cout << endl;
}
void showTime(int nHour,int nMinute,int nSecond)
{
showPosInt(nHour,':');
showPosInt(nMinute,':');
showPosInt(nSecond,'\0');
}
class RandomTime1
{
public:
RandomTime1()
{m_fSeed=INIT_SEED;}
void getTime(int &nHour,int &nMinute,int &nSecond);
protected:
double m_fSeed;
virtual int random(int nMax);
double sqr(double x)
{return x * x;}
double frac(double x)
{return x-long(x);}
};
class RandomTime2:public RandomTime1
{
public:
RandomTime2():RandomTime1(){}
protected:
virtual int random(int nMax);
double cube(double x)
{return x * x * x;}
};
class RandomTime3:public RandomTime2
{
public:
RandomTime3():RandomTime2(){}
protected:
virtual int random(int nMax);
double fourth(double x)
{return sqr(x) * sqr(x);}
};
void RandomTime1::getTime(int &nHour,int &nMinute,int &nSecond)
{
nHour=random(24);
nMinute=random(60);
nSecond=random(60);
}
int RandomTime1::random(int nMax)
{
m_fSeed=nMax * frac(sqr(PI + m_fSeed));
return int(m_fSeed);
}
int RandomTime2::random(int nMax)
{
m_fSeed=nMax * frac(cube(PI + m_fSeed));
return int(m_fSeed);
}
int RandomTime3::random(int nMax)
{
m_fSeed=nMax * frac(fourth(PI + m_fSeed));
return int(m_fSeed);
}
main()
{
const int MAX=5;
int nHour,nMinute,nSecond;
RandomTime1 RT1;
RandomTime2 RT2;
RandomTime3 RT3;
cout << " Sequence of time generated by "
<< " instance of class RandomTime1 is: " << endl;
for(int i=0;i<MAX;i++)
{
RT1.getTime(nHour,nMinute,nSecond);
showTime(nHour,nMinute,nSecond);
}
cout << endl;
cout << " Sequence of time generated bu "
<< " instance of class RandomTime2 is: " << endl;
for(i=0;i<MAX;i++)
{
RT2.getTime(nHour,nMinute,nSecond);
showTime(nHour,nMinute,nSecond);
}
cout << endl;
cout << " Sequence of time generated bu "
<< " instance of class RandomTime3 is: " << endl;
for(i=0;i<MAX;i++)
{
RT3.getTime(nHour,nMinute,nSecond);
showTime(nHour,nMinute,nSecond);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -