C++实现日期计算器

本文介绍了C++中一个名为Data的类,包含私有成员变量year、month和day,用于表示日期。类中定义了构造函数、获取月份天数的方法以及重载的运算符如+、-,用于日期加减天数和与另一个Date对象的比较。
摘要由CSDN通过智能技术生成

功能:

日期加天数

日期减天数

日期减日期

#pragma once
#include <iostream>
using namespace std;
class Data
{
private:
	int _year;
	int _month;
	int _day;
public:
	//全缺省构造函数
	Data(int year = 0, int month = 1, int day = 1);
	//获得对应年月的天数
	int getMonthDay(int year, int month);
	//构造函数
	Data(const Data& d);
	//重载运算符
	void operator=(const Data& d);
	bool operator<(const Data& d) const;
	bool operator==(const Data& d)const;
	bool operator<=(const Data& d)const;
	bool operator>(const Data& d)const;
	bool operator>=(const Data& d)const;
	//日期加减天数
	void operator+=(int day);
	Data operator+(int day)const;
	void operator-=(int day);
	Data operator-(int day)const;
	//日期减日期
	int operator-(const Data& d)const;
};

#include "Data.h"
Data::Data(int year , int month,  int day ){
	if (month <= 0 || month > 13 || day <= 0 || day > getMonthDay(year, month)) {
		printf("日期非法!\n");
	}
	else {
		_year = year;
		_month = month;
		_day = day;
	}
}
int Data::getMonthDay(int year, int month) {
	static int monthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if ((year % 4 == 0 && year % 100 != 0 || year % 400 == 0) && month == 2) {
		return 29;
	}
	return monthDay[month];
}
bool Data::operator<(const Data& d) const {
	if (_year < d._year) return true;
	if (_year == d._year && _month < d._month)return true;
	if (_year == d._year && _month == d._month && _day < d._day)return true;
	return false;
}
bool Data::operator==(const Data& d)const {
	return _year == d._year && _month == d._month && _day == d._day;
}
bool Data::operator<=(const Data& d)const {
	return (*this) < d || (*this) == d;
}
bool Data::operator>(const Data& d)const {
	return !(*this <= d);
}
bool Data::operator>=(const Data& d)const {
	return !(*this < d);
}
Data::Data(const Data& d) {
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
void Data::operator=(const Data& d) {
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

void  Data::operator+=(int day) {
	_day += day;
	while (_day > getMonthDay(_year, _month)) {
		_day -= getMonthDay(_year, _month);
		_month++;
		if (_month == 13) {
			_year++;
			_month = 1;
		}
	}
}
Data Data::operator+(int day)const {
	Data ret(*this);
	ret += day;
	return ret;
}
void Data::operator-=(int day) {
	_day -= day;
	while (_day <= 0) {
		--_month;
		if (_month == 0) {
			_year--;
			_month = 12;
		}
		_day += getMonthDay(_year, _month);
	}
}
Data Data::operator-(int day)const {
	Data ret(*this);
	ret -= day;
	return ret;
}
int Data::operator-(const Data& d)const {
	Data max = *this;
	Data min = d;
	int flag = 1;
	if (*this < d) {
		max = d;
		min = *this;
		flag = -1;
	}
	int cnt = 0;
	while (min < max) {
		cnt ++ ;
		min += 1;
	}
	return cnt*flag;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值