const myDateTime operator-(myDateTime &dt, const int s) //dt-s秒 { int dayUsed = 0, S, Min, H; S = dt.getSecond() - s; Min = dt.getMinute(); H = dt.getHour(); while (S < 0) { S += 60; Min -= 1; } while (Min < 0) { Min += 60; H -= 1; } while (H < 0) { H += 24; dayUsed++; } int Y, M, D; Y = dt.getYear(); M = dt.getMonth(); D = dt.getDay() - dayUsed; while (D <= 0) { M--; if (M < 1) { Y--; M = 12; } if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) { D = D + 31; } else if (M == 4 || M == 6 || M == 9 || M == 11) { D = D + 30; } else { if (dt.isLeap(Y) == true) { D = D + 29; } else { D = D + 28; } } } return myDateTime(Y, M, D, H, Min, S); }
仿照上述建立operator-的部分。
第九部分:函式設立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
myDateTime::myDateTime(const int y, const int m, const int d, const int h, const int min, const int s) : myDate(y, m, d) { hour = h; minute = m; second = s; } int myDateTime::getHour() { return hour; } int myDateTime::getMinute() { return minute; } int myDateTime::getSecond() { return second; }
ool myDateTime::setDateTime(const int y, const int m, const int d, const int h, const int min, const int s) { setDate(y, m, d); if (h < 0 || h > 23) { hour = 0; minute = 0; second = 0; } else { if (min < 0 || min > 59) { hour = 0; minute = 0; second = 0; } else { if (s < 0 || s > 59) { hour = 0; minute = 0; second = 0; } } } return true; }
int main() { int y, m, d,hr,min,sec, x; cin >> y >> m >> d >> hr >> min >> sec; myDateTime date(y, m, d,hr,min,sec), answer(1900, 1, 1,0,0,0); date.setDateTime(y, m, d,hr,min,sec); while (cin >> x) { if (x == 0) break; else if (x > 0) { answer = date + x; answer.output(); cout << endl; date = answer; } else { x = -x; answer = date - x; answer.output(); cout << endl; date = answer; } } }