一.问题描述:
定义一个时间类,小时和分钟是其两个私有成员数据。输入一个起始时间和一个结束时间(起始时间早于结束时间),通过运算符重载-(减号),计算这两个时间相隔多少分钟。说明:这两个时间在同一天之内,且采用24小时计时分式,即从00:00-23:59。
二.代码实现:
#include<iostream> using namespace std; class Time { private: int hour; int minute; public: Time(int h = 00, int m = 00) { hour = h; minute = m; } int friend operator- (Time p, Time q); }; int operator- (Time q, Time p) { int c = 0; int d = 0; if (p.minute < q.minute|| p.minute == q.minute) { d = q.hour - p.hour; c = q.minute - p.minute + 60 * d; return c; } else { d = q.hour - p.hour - 1; c = 60 + q.minute - p.minute + 60 * d; return c; } } int main() { int a, b, c, d; while (cin >> a >> b >> c >> d) { if (a == 00 && b == 00 && c == 00 && d == 00) break; int difference; Time p1(a, b); Time p2(c, d); difference = p2 - p1; cout << difference << endl; } return 0; }
标签:00,相减,hour,int,时间,计算,Time,minute From: https://www.cnblogs.com/lzh0416/p/17391960.html