不知楼主喜不欢喜欢玩C++
#include <ostream.h>
#include <strstream.h>
class TimePolicy
{
private:
int Max_Count;
public:
TimePolicy(int max_count):Max_Count(max_count){};
bool operator()(const int count)const{
if (count>=Max_Count)
return false;
else
return true;
};
};
class Time{
friend ostream&
operator<<(ostream &os,const Time&t);
friend void print(Time* t,ostream&
os);
private:
int _count;
Time* _parent;
TimePolicy* _TimePolicy;
public:
Time(TimePolicy* timePolicy,Time* parent=NULL):_count(0),_TimePolicy(timePolicy),_parent(parent){};
~Time(){
delete _parent;
delete _TimePolicy;
};
Time&
operator ++(){
if ((*_TimePolicy)(_count+1))
_count++;
else
{
_count=0;
if (_parent)
++(*_parent);
}
return *this;
}
};
ostream&
operator<<(ostream &os,const Time &t)
{
os<<t._count;
return os;
};
void print(Time* t,ostream&
os)
{
if (!t)
return ;
string s;
do
{
ostrstream out ;
out<<(*t);
s=out.str() + s;
s=":"+s;
}
while(t=t->_parent);
os<<s<<endl;
}
int main(int argc, char* argv[])
{
int i;
TimePolicy *hourPolicy = new TimePolicy(24),
*minutePolicy = new TimePolicy(60),
*secondPolicy = new TimePolicy(60);
Time *hour = new Time(hourPolicy),
*minute = new Time(minutePolicy,hour),
*second = new Time(secondPolicy, minute);
for(i=0;i<70;i++)
{
++(*second);
print(second,cout);
}
for(i=0 ;i<70 ;i++)
{
(*minute)++;
print(second,cout);
}
for(i=0 ;i<25 ;i++)
{
(*hour)++;
print(second,cout);
}
cin>>i;
delete second;
return 0;
}