用C++实现日期类
在上学的时候,总是在计算还有多少天放假;在上班的时候,总是在计算还有多久发工资?我们一般通过日历得到结果,那自己能不能实现一些基本的功能呢?答案是可以的!
需要实现内容:
1. 日期加减天数
//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day)
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}
// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}
2. 日期减日期
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;int count = 0;if (*this < d){max = d;min = *this;flag = -1;}int day1 = max._day;int day2 = min._day;int n = max._year - min._year;if (max._year == min._year){n = 0;}// 2025 1 31while (max._month != 1 || max._day != 1){max._month--;if (max._month <= 0){max._month = 12;max._year--;break;}max._day = GetMonthDay(max._year, max._month);day1 += max._day;}//2024 1 1while (min._month != 1 || min._day != 1){min._month--;if (min._month <= 0){min._month = 12;min._year--;break;}min._day = GetMonthDay(min._year, min._month);day2 += min._day;}int x = min._year;while (x < max._year){count += Getleapyear(min._year + 1);x++;}return day1 - day2 + 365 * n + count+1;
}
第二种就是直接暴力计算,如下:
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}
3. 完整代码
Date.h 文件
#pragma once
#include <iostream>
using namespace std;
#include <assert.h>class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}int GetMonthDay(int year, int month){assert(month > 0 && month < 13);// 为了避免每次调用都开辟空间,可以加static存放到全局static int arr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}elsereturn arr[month];}int Getleapyear(int year){if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)){return 1;}elsereturn 0;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}void Print(){cout << _year << "年" << _month << "月" << _day << "日" << endl;}// 日期+=dayDate& operator+=(int day);Date operator+(int day)const;Date& operator-=(int day);Date operator-(int day)const;// ++d1Date& operator++();// d1++Date operator++(int);// --d1Date& operator--();// d1--Date operator--(int);int 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 operator<=(const Date& d)const;~Date(){}
private:int _year;int _month;int _day;
};
Date.cpp 文件
#include "Date.h"//d1+=50
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month > 12){_year++;_month = 1;//_day -= GetMonthDay(_year, _month); // error - _month已经发生改变}}return *this;
}//d1+50(不会改变d1)
Date Date::operator+(int day) const
{//创建临时对象,改变贫道不改变己身Date tmp(*this);tmp += day;return tmp;
}// d1-=50
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){_month--;if (_month <= 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);;}return *this;
}
//// d1-50
//Date Date::operator-(int day) const
//{
// Date tmp(*this);
// tmp._day -= day;
// while (tmp._day <= 0)
// {
// tmp._month--;
// if (tmp._month <= 0)
// {
// tmp._year--;
// tmp._month = 12;
// }
// tmp._day += GetMonthDay(tmp._year, tmp._month);//error -> 这里传的是const Date* this,权限放大
// }
// return tmp;
//}// d1-50
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// d2 = ++d1
Date& Date::operator++()
{*this += 1;return *this;
}
// d2 = d1++
Date Date::operator++(int x)
{Date tmp(*this);*this += 1;return tmp;
}// --d1
Date& Date::operator--()
{*this -= 1;return *this;
}
// d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}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;}elsereturn 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);
}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 (min != max){min++;count++;}return count * flag;
}//int Date::operator-(const Date& d)
//{
// Date max = *this;
// Date min = d;
// int flag = 1;
// int count = 0;
// if (*this < d)
// {
// max = d;
// min = *this;
// flag = -1;
// }
// int day1 = max._day;
// int day2 = min._day;
// int n = max._year - min._year;
// if (max._year == min._year)
// {
// n = 0;
// }
//
// // 2025 1 31
// while (max._month != 1 || max._day != 1)
// {
// max._month--;
// if (max._month <= 0)
// {
// max._month = 12;
// max._year--;
// break;
// }
// max._day = GetMonthDay(max._year, max._month);
// day1 += max._day;
// }
// //2024 1 1
// while (min._month != 1 || min._day != 1)
// {
// min._month--;
// if (min._month <= 0)
// {
// min._month = 12;
// min._year--;
// break;
// }
// min._day = GetMonthDay(min._year, min._month);
// day2 += min._day;
// }
// int x = min._year;
// while (x < max._year)
// {
// count += Getleapyear(min._year + 1);
// x++;
// }
//
// return day1 - day2 + 365 * n + count+1;
//}