指针重合

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;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值