【C++】基础类之日期类
- 1. DateClass.h
- 2. DateClass.cpp
- 3. Text.cpp
1. DateClass.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:Date(int year, int month, int day);Date(const Date& date){_year = date._year;_month = date._month;_day = date._day;}void Print(){cout << "year:" << _year << "month:" << _month << "day:" << _day << endl;}int GetMonthDay(int year, int month);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+=(const int day);Date operator+(const int day)const;Date& operator-=(const int day);Date operator-(const int day)const;Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);int operator-(const Date& date)const;Date& operator=(const Date& date);friend ostream& operator<<(ostream& out, const Date& date);friend istream& operator>>(istream& in, Date& date);private:int _year = 0;int _month = 0;int _day = 0;
};
ostream& operator<<(ostream& out, const Date& date);
istream& operator>>(istream& in, Date& date);
2. DateClass.cpp
#include"DateClass.h"
int Date::GetMonthDay(int year, int month)
{static int daysArr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30 };if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){return 29;}elsereturn daysArr[month];
}
Date::Date(int year, int month, int day)
{if (month > 0 && month < 13 && day < GetMonthDay(year, month)){_year = year;_month = month;_day = day;}else{cout << "非法日期" << endl;assert(false);}
}
bool Date::operator>(const Date& d)const
{if (_year > d._year){return true;}if (_year == d._year && _month > d._month){return true;}if (_year == d._year && _month == d._month && _day > d._day){return true;}return false;
}
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 || *this == d);
}
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);
}
Date& Date::operator+=(const 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;
}
Date Date::operator+(const int day)const
{Date tmp = *this;tmp += day;return tmp;
}
Date& Date::operator-=(const int day)
{if (day < 0){return *this += -day;}_day -= day;while(_day <= 0){_month--;if (_month == 0){_year--;_month = 0;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(const 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& date)const
{Date max = *this;Date min = date;int flag = 1;if (*this < date){max = date;min = *this;flag = -1;}int count = 0;while (min != max){min++;count++;}return count * flag;
}Date& Date::operator=(const Date& date)
{if (this != &date){_year = date._year;_month = date._month;_day = date._day;}return *this;
}ostream& operator<<(ostream& out, const Date& date)
{out << "year:" << date._year << "month:" << date._month << "day:" << date._day << endl;return out;
}
istream& operator>>(istream& in, Date& date)
{int year, month, day;in >> year >> month >> day;if (month > 0 && month < 13&& day > 0 && day <= date.GetMonthDay(year, month)){date._year = year;date._month = month;date._day = day;}else{perror("非法日期");assert(false);}return in;
}
3. Text.cpp
#include"DateClass.h"
void Text1()
{Date d1(2024, 6, 26);Date d2(2024, 6, 21);cout << (d1 < d2) << endl; cout << (d1 < d2) << endl;
}
void Text2()
{Date d1(2024, 6, 26);Date d2(2024, 6, 21);cout << (d1 += 1);cout << (d1 + 1);cout << (d1 -= 1);cout << d1++;cout << ++d1;int gap = (d2 - d1);cout << gap;
}
void Text3()
{Date d1(2024, 6, 26);Date d2(2024, 6, 21);d1 = d2;cout << d1;Date d3(d1);
}int main()
{Text3();return 0;
}