C初阶四日期类实现
目录
C++(初阶)(四)——日期类实现
日期类实现
获取月份天数
//函数会被频繁调用,直接定义在类中
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//将month == 2这个简单的判断条件写在前面,更好一些
if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
return 29;
}
else
{
return monthDayArray[month];
}
}
日期±天数
加等
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;
}
但是这里有一个问题,此时我们想要得到d1+100 的日给到d2,但是d1也会被修改。需要拷贝一份。
Date d1(2025,3,9);
d1.print();
Date d2 = d1 + 100;
d1.print();
d2.print();
Date& Date::operator+=(int day)
{
Date tmp(* this)
if (day < 0)
{
return *this -= -day;
}
tmp._day += day;
while (tmp._day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
tmp._month++;
if (_month == 13)
{
tmp._year++;
tmp._month = 1;
}
}
return tmp;
}
加
上面我们实现了+=,所以在实现+时,直接复用+=就行了。
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
}
那假设我们先实现的是+功能,那么怎么实现+=?
因为我们知道实现+=时,是不改变*this的,此处要使用赋值就可以
*this = *this + _day;
return *this;
减等
Date& Date::operator-=(int day)
{
_day -= day;
while(_day <= 0)
{
_day += GetMonthDay(_year, _month);
_month--;
if (_month == 0)
{
--_year;
_month = 12;
}
}
return *this;
}
减
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
前置++和后置++
都需要++,但是前者返回++之后的值,后者返回++之前的值
前置++
Date& Date::operator++()
{
(*this) += 1;
return *this;
}
后置++
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
比较日期大小
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
等于
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
小于
年小就小,相等比较月,再相等比较天,其他为false
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
return _day < d._day;
}
}
//剩余情况为d1>d2
return false;
}
<=,>,>=,!=
首先我们最容易想到的就是,直接c,v上面的小于,但是更好的有;
先实现==函数,而正好我们又有<函数,直接复用,然后对二者取反更简单,而且一旦某处出现错误,能
更好的修改。
bool Date::operator<=(const Date& d) const
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
日期减日期
日期加日期是没有什么实际意义的,所以此处只实现日期减日期。
思路:
1,小的月和天,和大的对齐,算差多少天,年直接减,再处理闰年2月。
2,直接复用前面的,小的日期不断++,当和大的相等时,++多少次,就是相差多少天
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if(*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while(min != max)
{
++min;
++n;
}
return n*flag;
}
源代码
.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
//获取月份天数,频繁调用,使用内联函数
int GetMonthDay(int year, int month)
{
static int monthDayArray[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
//闰年二月加一天
if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
{
return 29;
}
return monthDayArray[month];
}
Date(int year = 2000, int month = 1, int day = 1);
void print() const;
//~Date()
//{
// cout << "~Date" << endl;
// _year = _month = _day = 0;
//}
//检查日期是否合法
bool Check();
bool operator==(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator!=(const Date& d) const;
//改变自己
Date& operator+=(int day);
//不改变自己
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
Date& operator++();
Date operator++(int);
Date& operator--();
Date operator--(int);
//日期减日期
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"Date.h"
bool Date::Check()
{
if (_year < 0 || _month < 0 || _month > 13 || _day<0 || _day > GetMonthDay(_year,_month))
{
return false;
}
return true;
}
//声明全缺省,定义就不能加
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//检查输入是否合法
if (!Check())
{
cout << "日期不合法" << endl;
}
}
void Date::print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month < d._month)
{
return true;
}
else if (_month == d._month)
{
return _day < d._day;
}
}
//剩余情况为d1>d2
return false;
}
bool Date::operator<=(const Date& d) const
{
return *this < d || *this == d;
}
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date tmp(*this);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
_day -= day;
while(_day <= 0)
{
_day += GetMonthDay(_year, _month);
_month--;
if (_month == 0)
{
--_year;
_month = 12;
}
}
return *this;
}
Date Date::operator-(int day) const
{
Date tmp = *this;
tmp -= day;
return tmp;
}
Date& Date::operator++()
{
(*this) += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date::operator--()
{
(*this) -= 1;
return *this;
}
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
int Date::operator-(const Date& d) const
{
//*this,d
//比较两个日期大小,小的一直++直到与大的相等,记录++次数
Date max = *this;
Date min = d;
//假设d1较大
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag;
}