【学习笔记】C++之类的继承实例:日期操作

定义日期类Date,实现:

  1. 通过加减天数操作获得新的日期;
  2. 通过两个日期相减获得相隔天数。

定义WDate类,继承Date类,且:

  1. 包含星期几信息;
  2. 对显示日期的成员函数进行修改。
//==================================
//Date.cpp
//==================================

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//----------------------------------

class Date {
protected:
	int  year, month, day;
	void init();
public:
	Date(const string& s);
	Date(int y = 2018, int m = 10, int d=20);
	bool isLeapYear()const;
	void datePlusOne();
	void dateMinusOne();
	void addDay(int d);
	friend ostream& operator<<(ostream& o, const Date& d);
	friend int dayInterval(const Date& c, const Date& e);
};//--------------------------------

class WDate : public Date {
	int week;
public:
	WDate(const string& s);
	WDate(int y = 2018, int m = 10, int d = 20);
	friend ostream& operator<<(ostream& o, const WDate& d);
};//---------------------------------

void Date::init() {
	int dayOfMonth[2][13] = { { 0, 31,28,31,30,31,30,31,31,30,31,30,31 },
							  { 0, 31,29,31,30,31,30,31,31,30,31,30,31 } };
	int leap = 0;
	if (isLeapYear())	 leap = 1;
	if (month < 1 || month>12 || day < 1 || day>dayOfMonth[leap][month]) {
		cerr << "Input illegal.\n";
		exit(1);
	}
}//---------------------------------

Date::Date(const string& s) {
	year  = atoi(s.substr(0,4).c_str());
	month = atoi(s.substr(5, 2).c_str());
	day   = atoi(s.substr(8, 2).c_str());
	init();
}//---------------------------------

Date::Date(int y,int m,int d) {
	year  = y;
	month = m;
	day   = d;
	init();
}//----------------------------------

ostream& operator<<(ostream& o, const Date& d) {
	return o << setfill('0') << setw(4) << d.year << '-' << setw(2) << d.month << '-' << setw(2) << d.day << '\n';
}//----------------------------------

bool Date::isLeapYear()const {
	return (year%4==0&&year%100!=0) || year % 400 == 0;
}//----------------------------------

void Date::datePlusOne() {
	int dayOfMonth[2][13] = { { 0, 31,28,31,30,31,30,31,31,30,31,30,31 },
							  { 0, 31,29,31,30,31,30,31,31,30,31,30,31 } };
	int leap = 0;
	if (isLeapYear())	 leap = 1;
	if (31 == day && 12 == month) {
		year ++;
		month = 1;
		day = 1;
	}
	else {
		if (day ==dayOfMonth[leap][month]) {
			month ++;
			day = 1;
		}
		else {
			day ++;
		}
	}
}//-----------------------------------

void Date::dateMinusOne(){ 
	int dayOfMonth[2][13] = { { 0, 31,28,31,30,31,30,31,31,30,31,30,31 },
						    { 0, 31,29,31,30,31,30,31,31,30,31,30,31 } };
	int leap = 0;
	if (isLeapYear())	 leap = 1;
	if (1 == day && 1 == month) {
		year--;
		month = 12;
		day = 31;
	}
	else {
		if (day==1) {
			day = dayOfMonth[leap][month - 1];
			month--;
		}
		else {
			day--;
		}
	}
}//-----------------------------------

void Date::addDay(int d) {
	if (d >= 0) {
		for (int i = 0; i < d; i++)
			datePlusOne();
	}
	else {
		for (int i = 0; i > d; i--)
			dateMinusOne();
	}
}//-----------------------------------

int dayInterval(const Date& c, const Date& e) {
	int days = 0;
	Date d(c);
	if (d.year<e.year||d.year==e.year&&d.month<e.month||d.year==e.year&&d.month==e.month&&d.day<=e.day) {
		while (!(d.year==e.year&&d.month==e.month&&d.day==e.day)) {
			d.datePlusOne();
			days++;
		}
	}
	else {
		while (!(d.year == e.year&&d.month == e.month&&d.day == e.day)) {
			d.dateMinusOne();
			days--;
		}
	}
	return days;
}//-----------------------------------

WDate::WDate(const string& s) :Date(s){
	Date date1("2018-10-21");
	Date date2(s);
	int days = dayInterval(date1, date2);
	if(days>=0)		week = days % 7;
	else			week= days % 7+7;
}//-----------------------------------

WDate::WDate(int y, int m, int d):Date(y,m,d) {
	Date date1(2018, 10, 21);
	Date date2(y, m, d);
	int days = dayInterval(date1,date2);
	if (days >= 0)	week = days % 7;
	else			week = days % 7 + 7;
}//-----------------------------------

ostream& operator<<(ostream& o, const WDate& d) {
	string weekString[7] = {"星期日","星期一","星期二","星期三","星期四","星期五", "星期六"};
	return o << setfill('0') << setw(4) << d.year << '-' << setw(2) << d.month << '-' << setw(2) << d.day << " "<<weekString[d.week]<<'\n';
}//----------------------------------

int main() {
	Date date1("2018-10-21");
	Date date2(2018, 11, 1);
	cout << date1<<date2<<dayInterval(date1,date2)<<endl;
	WDate date3(2018, 11, 1);
	cout << date3;
	return 0;
}//===================================

运行结果如下:

猜你喜欢

转载自blog.csdn.net/Gao_Shan2016/article/details/83217236