👉前言👈
在前三篇的类和对象的博客中,本人已经讲解了类和对象的全部知识点。这些知识点都是通过我们比较熟悉的日期类
Date、栈Stack和队列MyQueue来讲解的,那么本篇文章就将日期类的实现总结一下。
👉Date.h👈
头文件Date.h里主要放着头文件的包含、类型的声明、函数的声明。日期类主要需要实现的函数有构造函数(注:析构函数和拷贝构造函数不需要自己写,编译器默认生成的就够用了)、运算符重载、计算日期是一年中的第几天以及将一年中的第几天转换成日期。
主要:在类中定义的成员函数会被当做内联函数处理,我们可以将被频繁调用的小函数在类中定义,不经常被调用的函数在类外定义。因为流插入<<和流提取>>的重载在类中定义,可读性不高,不符合使用习惯,所以我们利用友元将这两个函数重载在类外定义。
#pragma once #include <iostream> using namespace std; class Date { public: // 友元声明(类的任意位置) friend ostream& operator<<(ostream& out, const Date& d); friend istream& operator>>(istream& in, Date& d); int GetMonthDay(int year, int month) { static int monthDayArray[13] = { 0,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; } else { return monthDayArray[month]; } } Date(int year = 1, int month = 1, int day = 1) { _year = year; _month = month; _day = day; // 检查日期是否合法 if (!(year >= 1 && (month >= 1 && month <= 12) && (day >= 1 && day <= GetMonthDay(year, month)))) { cout << "非法日期" << endl; } } void Print() const { cout << _year << '/' << _month << '/' << _day << endl; } bool operator==(const Date& d) const; // d1 > d2 bool operator>(const Date& d) const; // d1 >= d2 bool operator>=(const Date& d) const; bool operator<=(const Date& d) const; bool operator<(const Date& d) const; bool operator!=(const Date& d) const; // d1 += 100 Date& operator+=(int day); // d1 + 100 Date operator+(int day) const; // d1 -= 100 Date& operator-=(int day); // d1 - 100 Date operator-(int day) const; // 前置 Date& operator++(); // 后置 Date operator++(int); // 前置 Date& operator--(); // 后置 Date operator--(int); // d1 - d2 int operator-(const Date& d) const; Date* operator&() { return this; // 要求这个类的对象不让取地址 //return nullptr; } const Date* operator&() const { return this; //return nullptr; } // 计算日期是一年中的第几天 int DayOfYear() const; private: int _year; int _month; int _day; }; // operator<<(cout, d1) cout<<d1 inline ostream& operator<<(ostream& out, const Date& d) { out << d._year << "年" << d._month << "月" << d._day << "日" << endl; return out; } // cin >> d1 operator(cin, d1) inline istream& operator>>(istream& in, Date& d) { in >> d._year >> d._month >> d._day; return in; } // 将一年中的第几天转换成日期 void DaysToDate();
👉Date.cpp👈
日期类需要实现的函数大多数是运算符重载,这些运算符重载在类和对象(中)这篇文章里讲过。如果还有不懂,可以点击跳转学习一下。运算符重载最重要的就是学会赋用已经写好的运算符重载,这样可以达到事半功倍的效果。
#include "Date.h" 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) { 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; } return 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); } 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; } 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; return ret; } // 前置 Date& Date::operator++() { *this += 1; return *this; } // 后置 -- 多一个int参数主要是为了根前置区分 // 构成函数重载 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; } // 日期差值 d1 - d2 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 n = 0; while (min != max) { ++n; ++min; } return n * flag; } int Date::DayOfYear() const { static int Day[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; int n = Day[_month - 1] + _day; if (_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))) ++n; return n; } void DaysToDate() { int year = 0; int days = 0; cin >> year >> days; static int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && (days > 60)) { --days; } int month = 1; while (days > Day[month]) { days -= Day[month]; month++; } printf("%04d-%02d-%02d\n", year, month, days); }
计算日期是一年中的第几天
- 定义一个数组,数组里存储的是到这个月的月底是该年的第几天
- 计算日期是一年中的第几天
n = Day[_month - 1] + _day- 如果月份
month大于 2 且年份year为闰年,则n++
int Date::DayOfYear() const { static int Day[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; int n = Day[_month - 1] + _day; if (_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0))) ++n; return n; }
将一年中的第几天转换成日期
定义一个数组,数组中存储着平年每个月份的天数
如果年份year为闰年且天数days大于 60,则--days
定义月份month = 1,当days > Day[month]时,进入 while 循环,days -= Day[month] month++;当 while 循环结束时,就能求出month和days
注:1 <= days <= 366
void DaysToDate() { int year = 0; int days = 0; cin >> year >> days; static int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) && (days > 60)) { --days; } int month = 1; while (days > Day[month]) { days -= Day[month]; month++; } printf("%04d-%02d-%02d\n", year, month, days); }
👉Test.cpp👈
Test.cpp源文件主要负责测试函数接口实现得是否正确。验证函数接口都正确后,就可以写过菜单来和用户交互了。不过,本人不太推荐写菜单,所以我就没有写菜单了。如果大家有需求的话,也可以自己写一写菜单。
#include "Date.h" void TestDate1() { Date d1(2022, 11, 14); Date d2(2022, 11, 15); cout << (d1 == d2) << endl; cout << (d1 > d2) << endl; cout << (d1 >= d2) << endl; cout << (d1 <= d2) << endl; cout << (d1 < d2) << endl; cout << (d1 != d2) << endl; cout << (d1 += 100); cout << (d2 -= 100); cout << d1.DayOfYear() << endl; DaysToDate(); } int main() { TestDate1(); return 0; }
看完本篇博客,大家可以做一下下面几道 OJ 题来巩固自己所学到的知识哦。
👉总结👈
本篇博客主要是对前三篇类和对象写的日期类的小总结,如果大家觉得有收获的话,可以点个三连支持一下!谢谢大家啦!💖💝❣️

