#include<iostream>
#define uint unsigned int
using namespace std;
class Clock {
private:
uint hour;
uint minute;
uint second;
public:
Clock(uint h = 0, uint m = 0, uint s = 0);
void showTime(void);
void addSecond(uint s);
void interval(const Clock& c);
};
Clock::Clock(uint h, uint m, uint s) {
hour = h%24;
minute = m%60;
second = s%60;
}
void Clock::showTime() {
cout << hour << ":" << minute << ":" << second << endl;
}
void Clock::addSecond(uint s) {
uint remainS = s + second + minute * 60 + hour * 3600;
hour = (remainS / 3600)%24;
minute = (remainS - (remainS / 3600) * 3600) / 60;
second = (remainS - (remainS / 3600) * 3600) % 60;
}
void Clock::interval(const Clock& c){
int remainS = second + minute * 60 + hour * 3600 - c.second - c.minute * 60 - c.hour * 3600;
int Hour = remainS / 3600;
int Minute = (remainS - Hour * 3600) / 60;
int Second = (remainS - Hour * 3600) % 60;
cout << Hour << ":" << Minute << ":" << Second << endl;
}
int main() {
Clock c1(12, 30, 1);
c1.showTime();
Clock c2(10, 1, 1);
c2.showTime();
c2.addSecond(86545);
c2.showTime();
c1.interval(c2);
return 0;
}
设计时间类
最新推荐文章于 2023-01-04 20:39:03 发布