C++简单程序设计

来源:百度知道 编辑:UC知道 时间:2024/05/14 14:54:02
定义一个时间类,它能表示时、分、秒,并提供以下操作:
Time(int h,int m,int s) //构造函数
Set(int h,int m,int s) //调整时间
Increment( ) //时间增加1秒
Display() //显示时间值
Equal(Time &other_time) //比较是否与某个时间相等
Less_than(Time &other_time) //比较是否早于某个时间

class Time
{
public:
Time(int h,int m,int s); //构造函数
void Set(int h,int m,int s); //调整时间
void Increment(); //时间增加1秒
void Display(); //显示时间值
bool Equal(Time &other_time) const; //比较是否与某个时间相等
bool Less_than(Time &other_time) const; //比较是否早于某个时间

private:
int value;
};

Time::Time(int h,int m,int s)
{
Set(h,m,s);
}

void Time::Set(int h,int m,int s)
{
value = (h*3600+m*60+s);
}

void Time::Increment()
{
value++;
}
void Time::Display()
{
int s(value%60);
int m((value-s)/60%60);
int h((value-s-m*60)/3600);
printf("%02d:%02d:%02d\n", h, m, s);
}

bool Time::Equal(Time &other_time) const
{
return this->value == other_time.value;
}

bool Time::Less_than(Time &other_time) const
{
return this->value < other_time.value;
}

int ma