力扣刷题(数组篇)

时间:2025-02-13 22:36:59
#pragma once #include <iostream> #include <assert.h> using namespace std; class Date { public: // 构造会频繁调用,所以直接放在类里面(类里面的成员函数默认为内联) Date(int year = 1, int month = 1, int day = 1)//构造 { _year = year; _month = month; _day = day; //if (!CheckDate()) //{ // Print(); // cout << "刚构造的日期非法" << endl; //} assert(CheckDate()); } void Print() const; // 打印 int GetMonthDay(int year, int month)// 获取某年某月的天数 { static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = days[month]; if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) { day += 1; } return day; } bool CheckDate()// 检查日期是否合法 { if (_year >= 1 && _month > 0 && _month < 13 && _day > 0 && _day <= GetMonthDay(_year, _month)) { return true; } else { return false; } } 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; // 特殊处理,使用重载区分,后置++重载增加一个int参数跟前置构成函数重载进行区分 Date& operator++(); // 前置 Date operator++(int); // 后置 Date& operator--();// 前置 Date operator--(int);// 后置 int operator-(const Date& d) const; //日期减日期 void PrintWeekDay() const; //返回*this是星期几 private: int _year; int _month; int _day; }; #include "Date.h" // void Date::Print(const Date* const this) 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) || (_year == d._year && _month > d._month) || (_year == d._year && _month == d._month && _day > d._day)) { return true; } else { return false; } } bool Date::operator!=(const Date& d) const { return !(*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+=(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+(int day) const { Date ret = *this; ret += day; return ret;// 出了作用域ret对象就不在了,所以不能用引用返回 } 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; } Date Date::operator-(int day) const { Date ret = *this; ret -= day;// ret.operator-=(day); return ret;// 和 + 一样,出了作用域ret对象就不在了,所以不能用引用返回 } Date& Date::operator++() // 前置 { return *this += 1; } Date Date::operator++(int) // 后置 { Date ret = *this; *this += 1; return ret; } Date& Date::operator--() // 前置 { return *this -= 1; } Date Date::operator--(int) // 后置 { Date ret = *this; *this -= 1; return ret; } int Date::operator-(const Date& d) const { int ret = 0; int flag = -1; Date min = *this;//默认第一个小,返回的时候乘上 -1 Date max = d; if (*this > d)//默认错误,把小和大重置,返回时乘上 1 { flag = 1; min = d; max = *this; } while (min != max) { ++min; ++ret; } return ret * flag; } void Date::PrintWeekDay() const //打印*this是星期几 { const char* Week[] = { "星期一","星期二" ,"星期三" , "星期四" ,"星期五" , "星期六" , "星期天" }; Date flag(1900, 1, 1); //1900年1月1日是星期一,自己减自己为0,对应下标0 cout << Week[(*this - flag) % 7] << endl; } #include "Date.h" void TestDate7() { Date d1(2023, 5, 5); Date d2(2023, 6, 7); d1.Print(); d1.PrintWeekDay(); d2.Print(); d2.PrintWeekDay(); Date d3(1900, 1, 7); Date d4(2050, 6, 7); d3.Print(); d3.PrintWeekDay(); d4.Print(); d4.PrintWeekDay(); } int main() { TestDate7(); return 0; }