实现思路
日期类主要是通过重载实现的
功能实现
1.比较大小
实现 >,< ,=,!= 的重载
思路:这部分比较简单,首先比较年份,如果年份相等就比较月份,如果月份相等就比较日期
需要注意的是:写好一个以后,其他的可以进行复用,就比如 >,< ,写好大于以后,小于的实现就可以借助大于进行复用,减少代码量
2.计算日期往后n天的日期
相当于是实现日期的加法:
拿2022.3.3 举例:首先得判断出今年是平年还是闰年(2022是平年)
往后推迟10天 3.3 +10=3.13
往后推迟100天呢?思路如下
第一步:先将天数加到日期上
第二步:如果天数超出了该月份的天数,月份进1,天数减去上个月份的天数;如果没超过,直接就是结果。
第三步:如果月份满了(变成了13),年数进1,将月份重新置为1;
在这里首先实现一个取各个月份天数的函数,方便之后进行操作。
int Date::GetMonthDay(int year, int month)const
{
int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = MonthDayArray[month];
//判断闰年;
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
Date& Date::operator+=(int day)
{
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
3.计算日期往前n天的日期
相当于是实现日期的减法
拿2022.3.15 举例:首先得判断出今年是平年还是闰年(2022是平年)
往前10天,3.15-10=3.5
往前16天
往前100天
第一步:先拿该月份现有天数减去n天
第二步:如果天数是负数或者是0,将月份减1,天数+减1后月份对应的天数 ;如果天数不是负数,则就是最后结果。
第三步:如果月份满了(变成了1),年数减1,将月份重新置为12;
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
4.关于日期的前后置++,前后置--
后置++为了跟前置++进行区分,增加一个参数进行占位,跟前置++构成函数重载
后置--为了跟前置--进行区分,增加一个参数进行占位,跟前置++构成函数重载
实现思路:复用+,+=和-,-=。
5.计算某一时刻的日期是周几
首先实现一个日期减日期,也就是再实现一个-的重载
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 count = 0;
while (max!=min)
{
++min;
count++;
}
return count * flag;
}
然后根据1900,1,1是星期1,统计出某一个日期与该日期相差的天数。
void Date::PrintWeekDay()const
{
//方法一:
//Date start(1900, 1, 1); //星期一
//int count = *this - start;
//cout << "星期:" << count % 7 + 1 << endl; 0%7是0,所以要+1
//法二,用一个数组将星期存起来
const char* str[] = { "星期一","星期二" ,"星期三" ,"星期四","星期五","星期六","星期日" };
Date start(1900, 1, 1); //星期一
int count = *this - start;
cout << str[count%7]<< endl;
}
完整日期类展示:
Date.h 部分
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 0, int month = 1, int day = 1);
void Print()const;
int GetMonthDay(int year, int month)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 operator!=(const Date& d)const;
Date& operator+=(int day);
Date operator+(int day)const;
Date& operator-=(int day);
Date operator-(int day)const;
//++d1
Date& operator++();
//d1++,后置为了跟前置++进行区分,增加一个参数进行占位,跟前置++构成函数重载
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d)const; //两日期相差几天
void PrintWeekDay()const;
//类声明里也同样要加
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
int solution(Date& d); //计算某个日期对应他的年份过了多少天
private:
int _year;
int _month;
int _day;
};
Date.cpp 部分
#include"Date.h"
int Date::GetMonthDay(int year, int month)const
{
int MonthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int day = MonthDayArray[month];
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
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 << "非法日期:";
//this->print
Print();
}
}
void Date::Print()const
{
cout << _year << "-" << _month << "-" << _day << endl;
}
bool Date::operator==(const Date& d)const
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
{
return false;
}
}
bool Date::operator!=(const Date& d)const
{
if (*this == d)
{
return false;
}
else
{
return true;
}
}
bool Date::operator>(const Date& d)const
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year && _month > d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
else
{
return false;
}
}
bool Date::operator<(const Date& d)const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
else
{
return false;
}
}
bool Date::operator>=(const Date& d)const
{
/*if (_year >= d._year)
{
return true;
}
else if (_year == d._year && _month >= d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day >= d._day)
{
return true;
}
else
{
return false;
}*/
if (*this > d || *this == d)
{
return true;
}
else
{
return false;
}
}
bool Date::operator<=(const Date& d)const
{
/*if (_year <= d._year)
{
return true;
}
else if (_year == d._year && _month <= d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day <= d._day)
{
return true;
}
else
{
return false;
}*/
if (*this < d || *this == d)
{
return true;
}
else
{
return false;
}
}
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)
{
_month = 1;
_year++;
}
}
return *this;
}
Date Date::operator+(int day)const
{
Date ret(*this);
ret += day;
return ret;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_month = 12;
_year--;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day)const
{
Date ret = *this;
ret -= day;
return ret;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date ret(*this);
*this += 1;
return ret;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
Date Date::operator--(int)
{
Date ret(*this);
*this -= 1;
return ret;
}
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 count = 0;
while (max!=min)
{
++min;
count++;
}
return count * flag;
}
void Date::PrintWeekDay()const
{
//Date start(1900, 1, 1); //星期一
//int count = *this - start;
//cout << "星期:" << count % 7 + 1 << endl;
const char* str[] = { "星期一","星期二" ,"星期三" ,"星期四","星期五","星期六","星期日" };
Date start(1900, 1, 1); //星期一
int count = *this - start;
cout << str[count%7]<< endl;
}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "/" << d._month << "/" << d._day << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
cout << "请依次输入年月日,用空格隔开:" << endl;
in >> d._year >> d._month >> d._day;
return in;
}
int Date::solution(Date& d)
{
_year = d._year;
_month = 1;
_day = 1;
int count = d - *this;
return count;
}
测试用例部分
#include"Date.h"
void Test1()
{
Date d1;
Date d2(2022, 3, 5);
//d1.Print();
//d2.Print();
Date d4(2022, 2, 23);
Date d5(2022, 2, 23);
Date d6(2020, 2, 29);
Date d7(2022, 12, 31);
/*d7 + 100;
d7.Print();*/
cout << (d4 <= d5) << endl;
}
void Test2()
{
Date d1(2022, 1, 13);
d1 += 1000;
d1.Print();
Date d2(2022, 3, 3);
d2 + 100;
d2.Print();
Date d3 = d2 + 100;
d3.Print();
}
void Test3()
{
Date d1(2022, 5, 11);
d1 -= 200;
d1.Print();
Date d2(2022, 5, 12);
Date d3=d2 - -100;
d3.Print();
d2.Print();
}
void Test4()
{
Date d1(2022, 3, 3);
Date d2(2022, 3, 3);
Date ret1=d1++; //d1.operator++(&d1,0);
Date ret2=++d2; //d1.operator++(&d1);
}
void Test5()
{
Date Today(2022, 3, 6);
Date dd(2000, 3, 5);
cout << (Today - dd) << endl;
cout << (dd-Today) << endl;
Today.PrintWeekDay();
dd.PrintWeekDay();
}
int main()
{
//Date d;
//d.Print();
//const Date d2;
//d2.Print();
//cout << d2;
Date d9(1982, 3, 4);
Date d8(1982, 3, 4);
cout << d8.solution(d9) << endl;
}