判断日期是否合法
Date.h
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
//频繁调用后被重复定义需要开很多空间,但加了static放到静态区就可以不用重复被定义
static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 闰年的2月是29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
return monthDays[month];
}
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
// 判断日期是否合法
if (_year < 0
|| _month <= 0 || _month >= 13
|| _day <= 0 || _day > GetMonthDay(_year, _month))
{
cout << _year << "/" << _month << "/" << _day << "->";
cout << "非法日期" << endl;
}
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
test.c
#include<iostream>
using namespace std;
#include "Date.h"
int main()
{
Date d1(2021, 10, 11);
d1.Print();
Date d2(2021, 2, 29);
Date d3(2020, 2, 29);
Date d4(2020, 13, 29);
d2.Print();
d3.Print();
d4.Print();
return 0;
}
日期和天数进行+=、+、-、-=运算,日期自己++,–运算----->输出合法的日期
Date.cpp
#include "Date.h"
// d1 + 100--不改变d1
Date Date::operator+(int day)
{
// //Date ret = *this;
// Date ret(*this);//对d1拷贝构造得到的ret就是d1
// ret._day += day;
// while (ret._day > GetMonthDay(ret._year, ret._month))
// {
// ret._day -= GetMonthDay(ret._year, ret._month);
// ++ret._month;
// if (ret._month == 13)
// {
// ++ret._year;
// ret._month = 1;
// }
// }
// return ret;//局部变量出函数会销毁了,但也不能返回它的别名,因为别人去访问会导致越界--没办法的时候就不用写,自动多一次拷贝构造好过出错
Date ret = *this;//先拷贝构造
ret += day;//再赋用加等
return ret;
}
// d1 += 100--值赋给d1即改变d1
Date& Date::operator+=(int day)
{
if (day < 0)//加了一个小于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
Date Date::operator-(int day) //const
{
Date ret(*this);//先拷贝构造
ret -= day;//再赋用减等
return ret;
}
// d1 -= 100
Date& Date::operator-=(int day)
{
if (day < 0)//如果减了一个负的天数的时候,调用加等
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// ++d1 -> d1.operator++(&d1)
Date& Date::operator++()//出了作用域还在,加引用可以减少拷贝
{
*this += 1;//赋用加等
return *this;
}
// d1++ -> d1.operator++(&d1, 0)
// 按正常运算符重载规则,无法区分前置++和后置++
// 为了区分,这里就做一个特殊处理,给后置++增加一下int参数,
// 这个参数仅仅是为了区分,这样他们俩就构成函数重载
Date Date::operator++(int)
{
Date tmp(*this);//拷贝构造--保存++之前的值
*this += 1;//赋用加等
return tmp;//出了作用域就没有了不能用引用
}
// --d1 -> d1.operator--(&d1)
Date& Date::operator--()
{
*this -= 1;
return *this;
}
// d1-- -> d1.operator--(&d1, 0)
Date Date::operator--(int)
{
Date tmp(*this);//拷贝构造--保存--之前的值
*this -= 1;//赋用减等
return tmp;
}
Date.h
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 闰年2月是29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
return monthDays[month];
}
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
// 判断日期是否合法
if (_year < 0
|| _month <= 0 || _month >= 13
|| _day <= 0 || _day > GetMonthDay(_year, _month))
{
cout << _year << "/" << _month << "/" << _day << "->";
cout << "非法日期" << endl;
}
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
// d1 + 100
Date operator+(int day) const;
Date& operator+=(int day);
Date operator-(int day) const;
Date& operator-=(int day);
// ++d1 -> d1.operator++(&d1)
Date& operator++();
// d1++ -> d1.operator++(&d1, 0)
// 按正常运算符重载规则,无法区分前置++和后置++
// 为了区分,这里就做一个特殊处理,给后置++增加一下int参数,
// 这个参数仅仅是为了区分,这样他们俩就构成函数重载
Date operator++(int);
// --d1 -> d1.operator--(&d1)
Date& operator--();
// d1-- -> d1.operator--(&d1, 0)
// 按正常运算符重载规则,无法区分前置++和后置++
// 为了区分,这里就做一个特殊处理,给后置++增加一下int参数,
// 这个参数仅仅是为了区分,这样他们俩就构成函数重载
Date operator--(int);
private:
int _year;
int _month;
int _day;
};
Test.cpp
#include<iostream>
using namespace std;
#include"Date.h"
void TestDate1()
{
Date d1(2021, 10, 13);
Date d2 = d1 + 100;
d1 + 100;
//d1 = d1 + 100;
d2.Print();
d1.Print();
}
void TestDate2()
{
Date d1(2021, 10, 13);
Date d2 = d1 + 100;
d1 += 100;
d2.Print();
d1.Print();
//Date d3 = d2 - 100;
//d2 -= 100;
//d3.Print();
//d2.Print();
}
void TestDate3()
{
Date d1(2021, 10, 13);
Date d2 = d1 - 100;
d1 - 100;
d2.Print();
d1.Print();
}
void TestDate4()
{
Date d1(2021, 10, 13);
Date d2 = d1 - 100;
d1 -= 100;
d2.Print();
d1.Print();
}
void TestDate5()
{
Date d1(2021, 10, 13);
Date d3 = ++d1;
d1.Print();
d3.Print();
}
void TestDate6()
{
Date d1(2021, 10, 13);
Date d4 = d1++;
d1.Print();
d4.Print();
}
void TestDate7()
{
Date d1(2021, 10, 13);
Date d3 = --d1;
d1.Print();
d3.Print();
}
void TestDate8()
{
Date d1(2021, 10, 13);
Date d4 = d1--;
d1.Print();
d4.Print();
}
int main()
{
printf("+:\n");
TestDate1();
printf("\n");
printf("+=:\n");
TestDate2();
printf("\n");
printf("-:\n");
TestDate3();
printf("\n");
printf("-=\n");
TestDate4();
printf("\n");
printf("前置++\n");
TestDate5();
printf("\n");
printf("后置++\n");
TestDate6();
printf("\n");
printf("前置--\n");
TestDate7();
printf("\n");
printf("后置--\n");
TestDate8();
return 0;
}
日期-日期
Date.cpp
#include "Date.h"
// d1 += 100
Date& Date::operator+=(int day)
{
if (day < 0)//加了一个小于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
Date& Date::operator-=(int day)
{
if (day < 0)//如果减了一个负的天数的时候,调用加等
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
--_year;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
//++d1 -> d1.operator++(&d1)
Date& Date::operator++()//出了作用域还在,加引用可以减少拷贝
{
*this += 1;//赋用加等
return *this;
}
//d1 - d2
int Date::operator-(const Date& d)
{
Date max = *this, min = d;//max=d1,min=d2
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n*flag;
}
Date.h
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// 闰年2月是29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
{
return 29;
}
return monthDays[month];
}
Date(int year = 0, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
// 判断日期是否合法
if (_year < 0
|| _month <= 0 || _month >= 13
|| _day <= 0 || _day > GetMonthDay(_year, _month))
{
cout << _year << "/" << _month << "/" << _day << "->";
cout << "非法日期" << endl;
}
}
void Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
//d1>d2
bool 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 operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
// 赋用>和==
bool operator>=(const Date& d) const
{
return *this > d || *this == d;
}
bool operator<(const Date& d) const
{
return !(*this >= d);
}
//大于取反
bool operator<=(const Date& d) const
{
return !(*this > d);
}
//等于取反
bool operator!=(const Date& d) const
{
return !(*this == d);
}
Date& operator+=(int day);
Date& operator-=(int day);
Date& operator++();
d1 - d2;
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
Test.cpp
#include<iostream>
using namespace std;
#include"Date.h"
void TestDate9()
{
Date d1(2021, 10, 13);
Date d2(2022, 3, 1);
cout << d2 - d1 << endl;
}
int main()
{
printf("d1-d2:");
TestDate9();
return 0;
}