C++第四章上机作业2020-12-11
equal函数用于比较两个时间是否相等,lessthan则是比较两个时间的大小,set是设置三个指针的位置,Increment就是模拟时间逐秒增加行为。write就是输出三个指针重叠时的相关信息
#include <cstdio>
using namespace std;
class TimeType {
public:
void Set(int, int, int);
void Increment();
void Print() const;
bool isCoin() const;
bool operator!=(TimeType const&);
bool operator==(TimeType const&);
TimeType();
TimeType(int initHrs, int initMins, int initSecs);
TimeType(TimeType const&);
~TimeType();
private:
int hrs;
int mins;
int secs;
};
int main() {
const TimeType T(9, 23, 23);
TimeType MyClock = T;
int cnt = 0;
do {
MyClock.Increment();
if(MyClock.isCoin()) {
printf("第%d次重合,重合时间:", ++cnt);
MyClock.Print();
putchar('\n');
}
} while(MyClock != T);
}
void TimeType::Set(int hours, int minutes, int seconds) {
hrs = hours * 5 + minutes / 12;
mins = minutes;
secs = seconds;
}
void TimeType::Increment() {
++secs;
if(secs == 60) {
secs = 0;
++mins;
if(mins % 12 == 0) {
++hrs;
if(hrs == 60) hrs = 0;
if(mins == 60) {
mins = 0;
}
}
}
}
void TimeType::Print() const {
printf("%02d:%02d:%02d\n", hrs / 5, mins, secs);
}
bool TimeType::isCoin() const {
return hrs == mins && hrs == secs;
}
bool TimeType::operator!=(TimeType const& otherTime) {
return otherTime.hrs != hrs || otherTime.mins != mins || otherTime.secs != secs;
}
bool TimeType::operator==(TimeType const& otherTime) {
return otherTime.hrs == hrs && otherTime.mins == mins && otherTime.secs == secs;
}
TimeType::TimeType() : hrs(0), mins(0), secs(0) {}
TimeType::TimeType(int initHrs, int initMins, int initSecs) : hrs(initHrs * 5 + initMins / 12), mins(initMins), secs(initSecs) {}
TimeType::TimeType(TimeType const&) = default;
TimeType::~TimeType() = default;