文章目录
- 日期类的设计
- 日期计算相关OJ题
- HJ73 计算日期到天数转换
- KY111 日期差值
- KY222 打印日期
- KY258 日期累加
在软件开发中,处理日期是一项常见的任务。为了方便地操作日期,我们可以使用C++编程语言来创建一个简单的日期类。在本文中,我们将介绍如何使用C++实现一个基本的日期类,包括日期的加减、大小比较等功能。
日期类的设计
下面是日期类的基本实现代码:
#pragma once
#include<iostream>
using namespace std;class Date {
public:// 获取某年某月的天数int GetMonthDay(const int year, const int month);// 构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 打印日期void print()const;// 赋值运算符重载Date& operator=(const Date& d);// +=运算符重载Date& operator+=(const int day);// +运算符重载Date operator+(const int day);// -=运算符重载Date& operator-=(int day);// -运算符重载Date operator-(int day);// 计算两个日期之间的天数差int operator-(const Date& d) const;// ++前置运算符重载Date& operator++();// ++后置运算符重载Date operator++(int);// --前置运算符重载Date& operator--();// --后置运算符重载Date operator--(int);// 大于运算符重载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;// 地址运算符重载const Date* operator & () const;// 输出流运算符重载friend ostream& operator << (ostream& out, const Date& d);// 输入流运算符重载friend istream& operator >> (istream& in, Date& d);private:int _year; // 年int _month; // 月int _day; // 日
};// 获取某年某月的天数
int Date::GetMonthDay(const int year, const int month) {int monthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (2 == month && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {return 29;}return monthDay[month];
}// 构造函数
Date::Date(int year, int month, int day) {if ((year < 1) || (month < 1) || (month > 12) || (day < 1) || (day > GetMonthDay(year, month))) {cout << "非法日期:" << endl;}_year = year;_month = month;_day = day;
}// 拷贝构造函数
Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date() {_year = 1900;_month = 1;_day = 1;
}// 打印日期
void Date::print()const {cout << _year << "/" << _month << "/" << _day << endl;
}// 赋值运算符重载
Date& Date::operator=(const Date& d) {if (this != &d) {_day = d._day;_month = d._month;_year = d._year;}return *this;
}// +=运算符重载
Date& Date::operator+=(const int 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) {Date tmp(*this);tmp += day;return tmp;
}// -=运算符重载
Date& Date::operator-=(int day) {_day -= day;while (_day < 0) {--_month;if (_month == 0) {--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// -运算符重载
Date Date::operator-(int day) {Date tmp(*this);tmp -= day;return tmp;
}// 计算两个日期之间的天数差
int Date::operator-(const Date& d) const {Date BigDate = *this;Date SmallDate = d;if (SmallDate > BigDate) {BigDate = d;SmallDate = *this;}int count = 0;while (SmallDate != BigDate) {++SmallDate;++count;}return count;
}// ++前置运算符重载
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;
}// 大于运算符重载
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;}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);
}// 小于等于运算符重载
bool Date::operator <= (const Date& d) const {return !(*this > d);
}// 不等于运算符重载
bool Date::operator != (const Date& d) const {return !(*this == d);
}// 地址运算符重载
const Date* Date::operator & () const {return this;
}// 输出流运算符重载
ostream& operator << (ostream& out, const Date& d) {out << d._year << "/" << d._month << "/" << d._day;return out;
}// 输入流运算符重载
istream& operator >> (istream& in, Date& d) {in >> d._year;in >> d._month;in >> d._day;return in;
}
上面的代码实现了日期类的基本功能,包括构造函数、加减运算符重载、比较运算符重载、输入输出流运算符重载等。
下面是主函数用于测试代码功能:
int main() {// 创建日期对象并打印Date d1(2023, 11, 13);cout << "日期1:";d1.print();// 拷贝构造函数测试Date d2(d1);cout << "日期2(拷贝构造):";d2.print();// 赋值运算符重载测试Date d3 = d1;cout << "日期3(赋值运算符重载):";d3.print();// += 运算符重载测试d1 += 10;cout << "日期1(+=运算符重载后):";d1.print();// + 运算符重载测试Date d4 = d2 + 5;cout << "日期4(+运算符重载):";d4.print();// -= 运算符重载测试d2 -= 3;cout << "日期2(-=运算符重载后):";d2.print();// - 运算符重载测试Date d5 = d3 - 7;cout << "日期5(-运算符重载):";d5.print();// - 运算符重载测试int diff = d5 - d4;cout << "日期4和日期5之间的天数差:" << diff << endl;// ++ 前置运算符重载测试++d1;cout << "日期1(++前置运算符重载后):";d1.print();// ++ 后置运算符重载测试Date d6 = d2++;cout << "日期6(++后置运算符重载):";d6.print();cout << "日期2(++后置运算符重载后):";d2.print();// -- 前置运算符重载测试--d3;cout << "日期3(--前置运算符重载后):";d3.print();// -- 后置运算符重载测试Date d7 = d4--;cout << "日期7(--后置运算符重载):";d7.print();cout << "日期4(--后置运算符重载后):";d4.print();// 大于运算符重载测试cout << "日期5大于日期6吗?" << (d5 > d6 ? "是" : "否") << endl;// 等于运算符重载测试cout << "日期1等于日期2吗?" << (d1 == d2 ? "是" : "否") << endl;// 不等于运算符重载测试cout << "日期3不等于日期4吗?" << (d3 != d4 ? "是" : "否") << endl;// 输出流运算符重载测试cout << "日期1的输出流运算符重载:" << d1 << endl;// 输入流运算符重载测试Date d8;cout << "请输入一个日期(年 月 日):";cin >> d8;cout << "您输入的日期为:" << d8 << endl;return 0;
}
C++实现一个简单的日期类,包括日期的加减、大小比较等功能。日期类的实现可以帮助我们更方便地处理日期,提高代码的可读性和可维护性。
日期计算相关OJ题
HJ73 计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37&&tqId=21296&rp=1&ru=/activity/oj&qru=/ta/huawei/question-ranking
#include <iostream>
using namespace std;int main() {int month[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};int y, m, d;cin >> y >> m >> d;int day = d;if (2 < m && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))){day += 1;}day += month[m];cout << day << endl;
}
KY111 日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62&&tqId=29468&rp=1&ru=/activity/oj&qru=/ta/sju-kaoyan/question-ranking
#include <iostream>
#include <string>
using namespace std;// 判断是否为闰年
bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}// 获取某年某月的天数
int getDaysOfMonth(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}// 计算日期距离公元0年的天数
int getDaysFromZero(int year, int month, int day)
{int days = 0;for (int y = 1; y < year; ++y) {days += isLeapYear(y) ? 366 : 365;}for (int m = 1; m < month; ++m) {days += getDaysOfMonth(year, m);}days += day;return days;
}// 计算两个日期之间的天数差值
int getDaysDiff(const string& date1, const string& date2)
{int year1, month1, day1, year2, month2, day2;sscanf(date1.c_str(), "%4d%2d%2d", &year1, &month1, &day1);sscanf(date2.c_str(), "%4d%2d%2d", &year2, &month2, &day2);int days1 = getDaysFromZero(year1, month1, day1);int days2 = getDaysFromZero(year2, month2, day2);return abs(days2 - days1) + 1;
}int main()
{string date1, date2;while (cin >> date1 >> date2) {int daysDiff = getDaysDiff(date1, date2);cout << daysDiff << endl;}return 0;
}
KY222 打印日期
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69&&tqId=29669&rp=1&ru=/activity/oj&qru=/ta/hust-kaoyan/question-ranking
#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}int main() {int year, day;while (cin >> year >> day) { int month = 1;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;}printf("%4d-%02d-%02d\n", year, month, day);}
}
KY258 日期累加
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40&&tqId=31013&rp=1&ru=/activity/oj&qru=/ta/kaoyan/question-ranking
#include <iostream>
using namespace std;bool isLeapYear(int year)
{return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}int getMonthDay(int year, int month)
{int daysOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if (month == 2 && isLeapYear(year)) {return 29;}return daysOfMonth[month];
}
int main() {int t, year, month, day, add;cin >> t;while (t--) { // 注意 while 处理多个 casecin >> year >> month >> day >> add;day += add;while (day > getMonthDay(year, month)){day -= getMonthDay(year, month);++month;if (13 == month){month = 1;++year;}}printf("%4d-%02d-%02d\n", year, month, day);}
}