日期类的实现(C++)

1、构造函数 
2、拷贝构造函数 
3、常见运算符的重载 
4、函数的复用 实现的功能: 
1)、在当前基础下加一天,减一天后的日期 
2)、在当前基础下加多天,减多天后的日期 
3)、当前日期与要计算的日期相差多少天

Date.h

#define _CRT_SECURE_NO_WARNING 1

#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

class Date 
{ 
public: 
	Date(int year = 2018, int month = 6, int day = 15) 
	: _year(year) 
	, _month(month) 
	, _day(day) 
	{
		if(month<1 || month>12 || day<0 || day>GetMonthDay(_year,_month))
			assert(false);
	} 

	Date(const Date& d) 
	: _year(d._year) 
	, _month(d._month) 
	, _day(d._day) 
	{} 

	// 当前日期days天后是什么日期? 
Date operator+(int days);
int GetMonthDay(int year,int month);
bool isLeapYear(int year);

	// 当前日期days天前是什么日期? 
Date operator-(int days); 

	// 两个日期之间差了多少天? 
Date operator-(const Date& d); 

	// 日期比大小 
bool operator>(const Date& d);

bool operator<(const Date& d); 

bool operator<=(const Date& d);

bool operator>=(const Date& d);

bool operator==(const Date& d);

bool operator!=(const Date& d); 

Date& operator=(const Date& d); 

	// 重载取地址符号 
Date* operator&(); 

	// 前置++ 
Date& operator++(); 

	// 后置++ 
Date operator++(int); 

	// 前置-- 
Date& operator--(); 

	// 后置++ 
Date operator--(int);

void show();
private: 
	int _year; 
	int _month; 
	int _day; 
}; 

Date.cpp

#define _CRT_SECURE_NO_WARNING 1

#include "Date.h"


bool Date::isLeapYear(int year)
{
	if( (year % 4 == 0) && (year % 100 != 0) ||(year % 400 == 0) )
		return true;
	return false;
}

int Date::GetMonthDay(int year,int month)
{
	int MonthDay[13] = {0,31,28,31,30,31,30,31,30,30,31,30,31};
	if(month == 2&&isLeapYear(year))
	{
		MonthDay[2] = 29;
	}
	int day = MonthDay[month];
	return day;
}

	// 当前日期days天后是什么日期? 
Date Date::operator+(int days)
{
	if(days<0)
		return *this-(days);
	Date ret(*this);
	ret._day += days;
	while(ret._day > GetMonthDay(ret._year,ret._month))
	{
		ret._day -= GetMonthDay(ret._year,ret._month);
		ret._month++;
		if(ret._month == 13)
		{
			ret._year++;
			ret._month = 1;
		}
	}
	return ret;
}

	// 当前日期days天前是什么日期? 
Date Date::operator-(int days)
{
	if(days<0)
		return *this+(-days);//当前日期加上该天数
	
	Date ret(*this);
	ret._day -= days;
	while(ret._day <= 0)
	{
		ret._month--;
		if(ret._month == 0)
		{
			ret._year--;
			ret._month = 12;
		}
		ret._day += GetMonthDay(ret._year,ret._month);
	}
	return ret;
}

	// 两个日期之间差了多少天? 
Date Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;//标记当前日期和另一个日期相差的正负

	if( (*this) < d )
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int day = 0;//标记相差的天数
	while(min <= max)
	{
		min++;
		day++;
	}
	return day*flag;//正,表示当前日期在后,负,表示当前日期在前
}

	// 日期比大小 
bool Date::operator>(const Date& d)
{
	if(_year > d._year ||
		_year == d._year && _month > d._month ||
		_year == d._year && _month == d._month && _day > d._year)
	{
		return true;
	}

	return false;
}

bool Date::operator<(const Date& d)
{
	if(_year < d._year ||
		_year == d._year && _month < d._month ||
		_year == d._year && _month == d._month && _day < d._year)
	{
		return true;
	}

	return false;
}

bool Date::operator==(const Date& d)
{
	if( (_year = d._year) &&
	    (_month = d._month) &&
        (_day = d._day) )
	{
		return true;
	}
	return false;
}

bool Date::operator!=(const Date& d)
{
	return !(*this == d);
}

bool Date::operator>=(const Date& d)
{
	return !(*this<d);
}

bool Date::operator<=(const Date& d)
{
	return (*this == d || *this < d);
}

//赋值运算符的重载
Date& Date::operator=(const Date& d)
{
	if( (*this) != d)
	{
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._year;
	}
	return (*this);
}

	// 重载取地址符号 
Date* Date::operator&()
{
	return this;
}

	// 前置++ 
Date& Date::operator++()
{
	*this = *this+1;
	return *this;
}

	// 后置++ 
Date Date::operator++(int)
{
	Date temp(*this);
	*this = *this+1;
	return temp;
}

	// 前置-- 
Date& Date::operator--()
{
	*this = *this-1;
	return *this;
}

	// 后置++ 
Date Date::operator--(int)
{
	Date temp(*this);
	*this = *this-1;
	return (*this);
}

void Date::show()
{
	cout<<_year<<"/"<<_month<<"/"<<_day<<endl;
}

test.cpp

#define _CRT_SECURE_NO_WARNING 1

#include "Date.h"

int main()
{
	Date d1(2018,6,10);
	d1.show();
	/*Date d2(2018,3,14);
	d2.show();
	Date d3(2018,3,2);
	d3.show();
	(d1+30).show();
	(d1+(-4)).show();

	(d1-5).show();

	(--d1).show();
	(++d1).show();
	(d1++).show();
	(d1--).show();

	d1=d2=d3;*/
	

	Date d2(2018,3,10);
	cout<< (d1==d2) <<endl;
	cout<< (d1>=d2) <<endl;
	cout<< (d1<=d2) <<endl;
	return 0;	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <iostream> using namespace std; #include "MyDate.h" #include <iomanip> bool MYDATE::dowFlag = false; void MYDATE::Input() { int year, month, day; char c1, c2; while(!(cin >> year >> c1 >> month >> c2 >> day) || ! IsValid(year, month, day) || c1 != '-' || c2 != '-') { cout << "不正确的日期! 请重新输入: "; if(! cin.good()) cin.clear(); while(cin.get() != '\n') ; } this->year = year; this->month = month; this->day = day; } void MYDATE::Output() { char *adow[] = {"日", "一", "二", "三", "四", "五", "六"}; int w = Dow(); cout << setfill('0') << setw(2) << year << '-' << setw(2) << month << '-' << setw(2) << day << setfill(' '); if(dowFlag) { cout << "(星期" << adow[w] << ")"; } } void MYDATE::Set(int year, int month, int day) { if(IsValid(year, month, day)) { this->year = year; this->month = month; this->day = day; } else { cout << "不正确的时间,设置失败!\n"; } } void MYDATE::Get(int &year, int &month, int &day) { year = this->year; month = this->month; day = this->day; } int MYDATE::Year() { return year; } void MYDATE::Year(int year) { if(year > 0) { this->year = year; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Month() { return month; } void MYDATE::Month(int month) { if(month > 0 && month <= 12) { this->month = month; int n = Dom(year, month); if(day > n) day = n; } } int MYDATE::Day() { return day; } void MYDATE::Day(int day) { int n = Dom(year, month); if(day <= 0) day = 1; else if(day > n) day = n; this->day = day; } MYDATE MYDATE::Add(int x) { int y = 1,i = 1,m = 1; MYDATE c; double s = this->Tod() + x; if(s >= 0) { y = 1; while(s >= 365 + IsLeap(y)) { s = s - (365 + IsLeap(y++)); i ++; } c.year = i; while(s >= Dom(c.year,m)) { s = s - Dom(c.year,m); m ++; } c.month = m; if(s == 0) c.day = 1; else c.day = s; } else cout << "Error!\n"; return c; } MYDATE MYDATE::Sub(int x) { MYDATE c; c.Set(year, month, day); c = c.Add(-x); return c; } bool MYDATE::IsValid(int year, int month, int day) { return year > 0 && month > 0 && month <= 12 && day > 0 && day <= Dom(year, month); } int MYDATE::Dom(int year, int month) { int n = 0; const static char adom[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month > 0 && month <= 12) { n = adom[month - 1]; if(month == 2 && IsLeap(year)) n ++; } return n; } int MYDATE::Doy() { const static short adoy[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; int n = adoy[month - 1] + day; if(month > 2 && IsLeap(year)) n ++; return n; } bool MYDATE::IsLeap(int year) { return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; } int MYDATE::Nol(int year) { return year / 4 - year / 100 + year / 400; } int MYDATE::Tod() { int t = year - 1; return t * 365 + Nol(t) + Doy(); } int MYDATE::Dow() { return Tod() % 7; } int MYDATE::Sub(MYDATE &x) { return this->Tod() - x.Tod(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值