定义一个时间类,提供设定时间、显示时间和秒数增加1的功能,其中设定时间的方法需要校验数据的正确性

/*
  1、【基本题】定义一个时间类,提供设定时间、显示时间和秒数增加1的功能,其中设定时间的方法需要校验数据的正确性,
  并在main函数中验证。
*/
#include<iostream>
using namespace std;
class Clock
{
public:
 void settime(int h,int m,int s);
 
 void showtime();
 void add();
 
 Clock(int h=0,int m=0,int s=0);
 Clock(const Clock &c);
 
private:
 int hour;
 int minute;
 int second;
 int state;
};
Clock::Clock(int h,int m,int s):hour(h),minute(m),second(s),state(0)
{
}
Clock::Clock(const Clock &c):hour(c.hour),minute(c.minute),second(c.second)
{
}
void Clock::settime(int h,int m,int s)
{
 this->hour=h;
 this->minute=m;
 this->second=s;
 if(h>0&&h<24)
 {
  state=state|0x4;   
 }
 if(m>0&&m<60)
 {
  state=state|0x2;
 }
 if(s>0&&s<60)
 {
  state=state|0x1;
 }
 
}
void Clock::add()
{ 
 if(this->hour==24)
 {
  this->hour++;
  this->hour=0;
 }
 if(this->minute==60)
 {
  this->minute++;
  this->minute=0;
 }
 if(this->second==60)
 {
  this->second++;
  this->second=0;
 }
}
void Clock::showtime()
{
 if((state&0x4)==0)
 {
  cout<<"小时错了"<<endl;
 }
 if((state&0x2)==0)
 {
  cout<<"分钟错了"<<endl;
 }
 if((state&0x1)==0)
 {
  cout<<"秒错了"<<endl;
 }
 cout<<this->hour<<":"<<this->minute<<":"<<this->second<<endl;
}
void main()
{
 Clock clock;
 clock.settime(21,78,90);
 clock.showtime();
 
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值