C++----日期计算器

Day.h

#include <iostream>
using namespace std;
class Date {
public:
	void Print(Date& d);
	int GetmonthDay(int _year, int _day);
	int GetyearDays(int year);
	int GetMinus(int num);
	Date(int year = 2020, int month = 2, int day = 2);
	Date(const Date& d);
	Date& operator=(const Date& d);
	Date& operator+=(int day);
	Date operator+(int day);
	Date operator-(int day);
	Date& operator-=(int day);
	int operator-(const Date& b);
	Date& operator++();
	Date operator++(int);
	Date operator--(int);
	Date& operator--();
	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);
private:
	int _year;
	int _month;
	int _day;
};

Day.cpp

#include"Day.h"
void Date::Print(Date& d) {
	cout << d._year << "-" << d._month << "-" << d._day << endl;
}
int Date::GetmonthDay(int _year, int _day) {
	static int day[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int daynum = day[_day];
	if (_year % 4 == 0 && _year % 100 != 0 && _year % 400 == 0)
		daynum++;
	return daynum;
}
int Date::GetyearDays(int year) {
	if (GetmonthDay(year, 2) == 29)
		return 366;
	else
		return 365;
}
int Date::GetMinus(int num) {
	if (num < 0)
		num = -num;
	return num;
}
Date::Date(int year = 2020, int month = 2, int day = 2) {  //全缺省的构造函数
	if (year > 0 && month > 0 && month < 13 && day < GetmonthDay(year, month)) {
		_year = year;
		_month = month;
		_day = day;
	}
	else
		cout << "非法日期" << endl;
}
Date::Date(const Date& d) {  //拷贝构造函数
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
Date& Date::operator=(const Date& d) {  //赋值运算符重载
	if (this != &d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}
Date& Date::operator+=(int day) {  //日期+=天数
	if (day < 0) {
		*this -= day;
		return *this;
	}
	_day += day;
	while (_day > GetmonthDay(_year, _month)) {
		_month++;
		_day -= GetmonthDay(_year, _month);
		if (_month == 13) {
			_month = 1;
			_year++;
		}
	}
	return *this;
}
Date Date::operator+(int day) {  //日期+天数
	Date ret(*this);
	ret += day;
	return ret;
}
Date Date::operator-(int day) {  //日期-天数
	Date ret(*this);
	ret -= day;
	return ret;
}
Date& Date::operator-=(int day) {  //日期-=天数
	if (day < 0) {
		*this += day;
		return *this;
	}
	_day -= day;
	while (_day < 1) {
		_month--;
		if (_month < 1) {
			_year--;
			_month = 12;
		}
		_day += GetmonthDay(_year, _month);
	}
	return *this;
}
int Date::operator-(const Date& b) {  //日期-日期 返回天数
	int D_value = 0;
	Date d(b);
	Date e(*this);
	if ((d._year > e._year) || ((e._year == d._year) && (e._month < d._month))
		|| ((e._year == d._year) && (e._month == d._month) && (e._day < d._day))) {
		cout << "非法操作" << endl;
		return -1;
	}
	if (e._year == d._year) {
		if (e._month == d._month)
			return e._day - d._day;
		else {
			while (d._month != e._month) {
				D_value += GetmonthDay(e._year, d._month);
				d._month++;
			}
			return D_value + GetMinus(e._day - d._day);
		}
	}
	else {
		while (e._year - d._year != 1) {
			if (GetmonthDay(d._year, 2) == 28)
				D_value += 365;
			else
				D_value += 366;
			d._year++;
		}
		while ((e._year != d._year) || (e._month >= d._month)) {
			D_value += GetmonthDay(d._year, d._month);
			d += GetmonthDay(d._year, d._month);
		}
		if (e._day > d._day)
			return D_value + e._day - d._day;
		else
			return D_value + d._day - e._day;
	}
}
Date& Date::operator++() {  //前置++
	*this += 1;
	return *this;
}
Date Date::operator++(int) {  //后置++
	Date ret(*this);
	++(*this);
	return ret;
}
Date Date::operator--(int) {  //后置--
	Date ret(*this);
	*this -= 1;
	return ret;
}
Date& Date::operator--() {  //前置--
	*this -= 1;;
	return *this;
}
bool Date::operator>(const Date& d) {  //>运算符重载
	if ((_year > d._year) || (_year == d._year && _month > d._month)\
		|| (_year == d._year && _month == d._month && _day > d._day))
		return true;
	return false;
}
bool Date::operator==(const Date& d) {  //==运算符重载
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator >= (const Date& d) {  //>=运算符重载
	if ((_year > d._year) || (_year == d._year) && (_month < d._month)\
		|| (_year == d._year) && (_month == d._month) && (_day >= d._day))
		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._day))
		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._day))
		return true;
	return false;
}
bool Date::operator != (const Date& d) {  //!=运算符重载
	return !(_year == d._year && _month == d._month && _day == d._day);
}

main.cpp

#include"Day.h"
using namespace std;
int main() {
	int i = 0;
	Date A(2019, 7, 9);
	Date B(2014, 2, 14);
	i = A - B;
	cout << i << " days" << endl;
	return 0;  //其余测例不一一举例
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1) 测试日期类成员函数,在主函数中列出菜单选项,可以完成日期的加减比较等测试功能。 (2) 完善程序功能,在日期相加的菜单选项中增加日期加天数,结果为新日期日期家月份,结果为新日期,要考虑闰年情况。 (3) 完善程序功能,在日期相减的菜单选项中增加日期减天数,结果为新日期日期减月份,结果为新日期,要考虑闰年情况。 (4) 显示日期时增加显示星期及英文形式的月份的功能。 (5) 增加输入的甄别功能,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。 (1) 仿照日期类编写时间类CTime_t,可以完成时间的设置、运算、比较等功能。 (2) 增加时间的输入功能,既可以选择输入格式,可以输入hh:mm:ss格式的信息。 (3) 增加时间的输出格式,可以输出12小时的时间格式。 (4) 编写时间和日期的派生类CDati,完成日期与时间的联合设置、运算、比较等功能,要求该派生类可以完成:日期时间加天数或时间等于新的日期时间,日期时间减天数或等于新的日期时间,两个日期时间相减等于天数或时间等工作,在程序中考虑闰年等具体情况,并重载各种运算符。 (5) 增加输入的甄别功能,即输入非法数据,即输入非法数据(如负数、日期超过31天、时间超过24小时等情况)的识别显示功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值