日期类
日期类有助于理解类与对象、构造函数、析构函数、运算符重载等
目录
前言
测试文件随便写的:
#include <iostream>
using namespace std;
#include "Date.h"
int main()
{
/*Date d1(2021, 3, 1);
d1.Print();*/
/*d1 += 50;
d1.Print();
Date ret = d1 + 50;
ret.Print();
d1 -= 50;
d1.Print();
ret = d1 - 50;
ret.Print();*/
/*d1.operator++();
d1.Print();
Date ret = d1++;
ret.Print();
d1.operator--();
d1.Print();
Date ret = d1--;
ret.Print();*/
Date d1(2021, 3, 1);
d1.Print();
Date d2(2021, 3, 2);
bool x = d1 < d2;
bool y = d1 >= d2;
bool z = d1 <= d2;
cout << x << endl;
cout << y << endl;
cout << z << endl;
}
一、头文件
pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
#pragma once
#include <iostream>
using namespace std;
class Date
{
public:
int GetMonthDay(int year, int month);
void Print();
Date(int year = 1900, int month = 1, int day = 1);
// d1 += 10
Date& operator+=(int day);
// d1 + 10
Date operator+(int day);
// d1 -= 10
Date& operator-=(int day);
// d1 - 10
Date operator-(int day);
Date& operator++();
// 为了跟前置++区别,迫不得已,这里给后置++,
// 加了一个用以区别的形参,这个参数不使用,仅仅是为了跟前置++构成函数重载区分开来
// d1++
// d1.operator++(&d1, 0);
Date operator++(int);
Date& operator--();
Date operator--(int);
// ==运算符重载
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);
// d1 - d2
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
二、源文件
1.获得月份相对的天数
代码如下:
int Date::GetMonthDay(int year,int month)
{
static int daysArray[13] = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = daysArray[month];
if (month == 2 &&
((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
days = 29;
}
return days;
}
2.日期的合法性
代码如下:
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
if (!(year >= 0 &&
month > 0 && month < 13 &&
day>0 && day < GetMonthDay(year, month)))
{
cout << "日期非法" << endl;
}
}
3.打印日期
代码如下:
void Date::Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
4.+= day的值变了
代码如下:
// d1 += 10
Date& Date::operator+=(int day)
{
/*if (day < 0)
{
return *this -= -day;
}*/
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
5.+ day本身不变
代码如下:
// d1 + 10
Date Date::operator+(int day)
{
Date ret(*this);
ret += day;
return ret;
}
6.-= day的值变了
代码如下:
扫描二维码关注公众号,回复:
13139046 查看本文章

Date& Date::operator-=(int day)
{
/*if (day < 0)
{
return *this += -day;
}*/
_day -= day;//_day=_day-day; 20-80
while (_day <= 0)
{
if (_month == 1)
{
_year--;
_month = 12;
}
_month--;
_day += GetMonthDay(_year, _month);
}
return *this;
}
7.- day本身不变
代码如下:
Date Date::operator-(int day)
{
Date ret(*this);
ret -= day;
return ret;
}
8.两种++
代码如下:
Date& Date:: operator++()
{
*this += 1;
return *this;
}
Date Date:: operator++(int)
{
Date ret(*this);
ret += 1;
return ret;
}
9.两种–
代码如下:
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date ret(*this);
ret -= 1;
return ret;
}
10.判断相等
代码如下:
bool Date::operator==(const Date& d)
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
11.判断不等
代码如下:
bool Date::operator != (const Date& d)
{
return !(*this == d);
}
12.判断大于
代码如下:
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if (_month == d._month)
{
if (_day > d._day)
{
return true;
}
}
}
return false;
}
13.判断小于
代码如下:
bool Date::operator < (const Date& d)
{
return !((*this > d) || (*this == d));
}
13.其他不等式
代码如下:
// >=运算符重载
bool Date::operator >= (const Date& d)
{
return !(*this < d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{
return !(*this > d);
}
14.两个日期差
代码如下:
// d1 - d2
int Date::operator-(const Date& d)
{
Date max = *this, min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++n;
++min;
}
return flag * n;
}