C++日期类Date的实现
作者:野牛程序员:2024-07-02 12:35:32 C++阅读 2317
C++日期类Date的实现
下面是一个简单的C++日期类Date
的实现,包括基本的日期操作,如设置日期、获取日期、日期加减法以及日期比较。
#include <iostream> #include <stdexcept> class Date { private: int year; int month; int day; // 检查是否为闰年 bool isLeapYear(int year) const { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } // 获取某年某月的天数 int daysInMonth(int year, int month) const { static const int days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2 && isLeapYear(year)) { return 29; } else { return days_in_month[month - 1]; } } // 检查日期是否合法 void validateDate(int year, int month, int day) const { if (year < 1 || month < 1 || month > 12 || day < 1 || day > daysInMonth(year, month)) { throw std::invalid_argument("Invalid date"); } } public: // 默认构造函数 Date() : year(1900), month(1), day(1) {} // 带参数的构造函数 Date(int year, int month, int day) { validateDate(year, month, day); this->year = year; this->month = month; this->day = day; } // 获取年、月、日 int getYear() const { return year; } int getMonth() const { return month; } int getDay() const { return day; } // 设置日期 void setDate(int year, int month, int day) { validateDate(year, month, day); this->year = year; this->month = month; this->day = day; } // 日期加减天数 void addDays(int days) { while (days > 0) { int days_left_in_month = daysInMonth(year, month) - day; if (days <= days_left_in_month) { day += days; break; } else { day = 1; days -= (days_left_in_month + 1); if (++month > 12) { month = 1; ++year; } } } } void subtractDays(int days) { while (days > 0) { if (days < day) { day -= days; break; } else { days -= day; if (--month < 1) { month = 12; --year; } day = daysInMonth(year, month); } } } // 日期比较 bool operator==(const Date& other) const { return year == other.year && month == other.month && day == other.day; } bool operator!=(const Date& other) const { return !(*this == other); } bool operator<(const Date& other) const { if (year < other.year) return true; if (year > other.year) return false; if (month < other.month) return true; if (month > other.month) return false; return day < other.day; } bool operator>(const Date& other) const { return other < *this; } bool operator<=(const Date& other) const { return !(*this > other); } bool operator>=(const Date& other) const { return !(*this < other); } // 输出日期 friend std::ostream& operator<<(std::ostream& os, const Date& date) { os << date.year << '-' << (date.month < 10 ? "0" : "") << date.month << '-' << (date.day < 10 ? "0" : "") << date.day; return os; } }; int main() { try { Date date1(2024, 7, 2); Date date2(2023, 12, 31); std::cout << "Date1: " << date1 << std::endl; std::cout << "Date2: " << date2 << std::endl; date1.addDays(10); std::cout << "Date1 after adding 10 days: " << date1 << std::endl; date2.subtractDays(15); std::cout << "Date2 after subtracting 15 days: " << date2 << std::endl; if (date1 > date2) { std::cout << "Date1 is later than Date2" << std::endl; } else { std::cout << "Date1 is not later than Date2" << std::endl; } } catch (const std::invalid_argument& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; }
此实现包括了日期的基本操作和比较,并带有一些简单的错误处理。这样可以在需要日期处理的场景中使用这个类。
4o
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
- 上一篇:C++探索智能指针的设计原理
- 下一篇:c语言中回车算字符吗