请问有没有懂C++的啊,帮个忙谢谢了。。(100分)

  • 主题发起人 主题发起人 liangjiny
  • 开始时间 开始时间
L

liangjiny

Unregistered / Unconfirmed
GUEST, unregistred user!
5、定义类time,数据成员有hour, minute和second 。以“时:分:秒”(24时制)的格式输出时间,还提供将时间增加一秒的成员函数nextime( ),要求能测试出如下的结果:
① 能够进入下一分钟。
② 能够进入下一小时。
 
这好象不难,就是逻辑判断有点烦琐
 
class time
{
private:
int _hour,_minute,_second;
public:
time(hour,minute,second)
{
_hour = hour;
_minute = minute;
_second = second;
}
time()
{
_hour = 0;
_minute = 0;
_second = 0;
}
void nexttime()
{
_second++;
if(_second == 60)
{
_second = 0;
_minute++;
if(_minute == 60)
{
_minute = 0;
_hour++;
if(_hour == 24)
_hour = 0;
}
}
}
void ToString(char *buffer)
{
wsprintf(buffer,"%2d:%2d:%2d",_hour,_minute,_second);
}
};
 
不知楼主喜不欢喜欢玩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&amp;
operator<<(ostream &amp;os,const Time&amp;t);
friend void print(Time* t,ostream&amp;
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&amp;
operator ++(){
if ((*_TimePolicy)(_count+1))
_count++;
else
{
_count=0;
if (_parent)
++(*_parent);
}
return *this;
}
};
ostream&amp;
operator<<(ostream &amp;os,const Time &amp;t)
{
os<<t._count;
return os;
};
void print(Time* t,ostream&amp;
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;
}
 
楼上的厉害
 
上面的代码又问题啊
for(i=0 ;i<70 ;i++)
这行吗
 
后退
顶部