Article directory
- Date design
- Date calculation related OJ questions
-
- HJ73 Calculate date to day conversion
- KY111 date difference
- KY222 Print date
- KY258 date accumulation
In software development, dealing with dates is a common task. In order to easily manipulate dates, we can use the C++ programming language to create a simple date class. In this article, we will introduce how to use C++ to implement a basic date class, including date addition, subtraction, size comparison and other functions.
Design of date class
The following is the basic implementation code of the date class:
#pragma once #include<iostream> using namespace std; class Date {<!-- --> public: // Get the number of days in a certain month of a certain year int GetMonthDay(const int year, const int month); // Constructor Date(int year = 1900, int month = 1, int day = 1); // copy constructor Date(const Date & d); // destructor ~Date(); // print date void print()const; // Assignment operator overloading Date & amp; operator=(const Date & amp; d); // + = operator overloading Date & amp; operator + =(const int day); // + operator overloading Date operator + (const int day); // -= operator overloading Date & operator-=(int day); // - operator overloading Date operator-(int day); // Calculate the difference in days between two dates int operator-(const Date & amp; d) const; // + + Prefix operator overloading Date & operator + + (); // + + post operator overloading Date operator + + (int); // --prefix operator overloading Date & operator--(); // --post operator overloading Date operator--(int); // Greater than operator overloading bool operator>(const Date & amp; d) const; //Equal operator overloading bool operator==(const Date & amp; d) const; // Greater than or equal to operator overloading bool operator >= (const Date & amp; d) const; // Less than operator overloading bool operator < (const Date & amp; d) const; // Less than or equal to operator overloading bool operator <= (const Date & amp; d) const; // Not equal to operator overloading bool operator != (const Date & amp; d) const; //Address operator overloading const Date* operator & amp; () const; //Output stream operator overloading friend ostream & amp; operator << (ostream & amp; out, const Date & amp; d); //Input stream operator overloading friend istream & amp; operator >> (istream & amp; in, Date & amp; d); private: int _year; // year int _month; // month int _day; // day }; // Get the number of days in a certain month of a certain year 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 & amp; & amp; ((year % 4 == 0 & amp; & amp; year % 100 != 0) || (year % 400 == 0))) {<!-- --> return 29; } return monthDay[month]; } // Constructor Date::Date(int year, int month, int day) {<!-- --> if ((year < 1) || (month < 1) || (month > 12) || (day < 1) || (day > GetMonthDay(year, month))) {<!-- --> cout << "Illegal date:" << endl; } _year = year; _month = month; _day = day; } // copy constructor Date::Date(const Date & amp; d) {<!-- --> _year = d._year; _month = d._month; _day = d._day; } // destructor Date::~Date() {<!-- --> _year = 1900; _month = 1; _day = 1; } // print date void Date::print()const {<!-- --> cout << _year << "/" << _month << "/" << _day << endl; } // Assignment operator overloading Date & amp; Date::operator=(const Date & amp; d) {<!-- --> if (this != & amp;d) {<!-- --> _day = d._day; _month = d._month; _year = d._year; } return *this; } // + = operator overloading 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; } // + operator overloading Date Date::operator + (const int day) {<!-- --> Date tmp(*this); tmp + = day; return tmp; } // -= operator overloading Date & Date::operator-=(int day) {<!-- --> _day -= day; while (_day < 0) {<!-- --> --_month; if (_month == 0) {<!-- --> --_year; _month = 12; } _day + = GetMonthDay(_year, _month); } return *this; } // - operator overloading Date Date::operator-(int day) {<!-- --> Date tmp(*this); tmp -= day; return tmp; } // Calculate the difference in days between two dates int Date::operator-(const Date & amp; 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; } // + + Prefix operator overloading Date & Date::operator + + () {<!-- --> *this + = 1; return *this; } // + + post operator overloading Date Date::operator + + (int) {<!-- --> Date tmp(*this); *this + = 1; return tmp; } // --prefix operator overloading Date & Date::operator--() {<!-- --> *this -= 1; return *this; } // --post operator overloading Date Date::operator--(int) {<!-- --> Date tmp(*this); *this -= 1; return tmp; } // Greater than operator overloading bool Date::operator>(const Date & amp; d) const {<!-- --> if (_year > d._year || (_year == d._year & amp; & amp; _month > d._month) || (_year == d._year & amp; & amp; _month == d._month & amp; & amp; _day > d._day)) {<!-- --> return true; } return false; } //Equal operator overloading bool Date::operator==(const Date & amp; d) const {<!-- --> return _year == d._year & amp; & amp; _month == d._month & amp; & amp; _day == d._day; } // Greater than or equal to operator overloading bool Date::operator >= (const Date & amp; d) const {<!-- --> return (*this > d) || (*this == d); } // Less than operator overloading bool Date::operator < (const Date & amp; d) const {<!-- --> return !(*this >= d); } // Less than or equal to operator overloading bool Date::operator <= (const Date & amp; d) const {<!-- --> return !(*this > d); } // Not equal to operator overloading bool Date::operator != (const Date & amp; d) const {<!-- --> return !(*this == d); } //Address operator overloading const Date* Date::operator & amp; () const {<!-- --> return this; } //Output stream operator overloading ostream & amp; operator << (ostream & amp; out, const Date & amp; d) {<!-- --> out << d._year << "/" << d._month << "/" << d._day; return out; } //Input stream operator overloading istream & amp; operator >> (istream & amp; in, Date & amp; d) {<!-- --> in >> d._year; in >> d._month; in >> d._day; return in; }
The above code implements the basic functions of the date class, including constructors, addition and subtraction operator overloading, comparison operator overloading, input and output stream operator overloading, etc.
Below is the main function used to test the code functionality:
int main() {<!-- --> //Create a date object and print it Date d1(2023, 11, 13); cout << "Date 1:"; d1.print(); //Copy constructor test Date d2(d1); cout << "Date 2 (copy construction):"; d2.print(); // Assignment operator overload test Date d3 = d1; cout << "Date 3 (assignment operator overloaded):"; d3.print(); // + = operator overload test d1 + = 10; cout << "Date 1 (after + = operator overloading):"; d1.print(); // + operator overload test Date d4 = d2 + 5; cout << "Date 4 (+ operator overloaded):"; d4.print(); // -= operator overload test d2 -= 3; cout << "Date 2 (after overloading of -= operator):"; d2.print(); // - Operator overload testing Date d5 = d3 - 7; cout << "Date 5 (-operator overloading):"; d5.print(); // - Operator overload testing int diff = d5 - d4; cout << "The difference in days between date 4 and date 5:" << diff << endl; // + + Prefix operator overload test + + d1; cout << "Date 1 (after + + prefix operator overloading):"; d1.print(); // + + post operator overload test Date d6 = d2 + + ; cout << "Date 6 ( + + post operator overloading): "; d6.print(); cout << "Date 2 (after + + post operator overloading):"; d2.print(); // -- Prefix operator overload test --d3; cout << "Date 3 (-- after prefix operator overloading):"; d3.print(); // -- post operator overload test Date d7 = d4--; cout << "Date 7 (--post operator overloading):"; d7.print(); cout << "Date 4 (--after operator overloading):"; d4.print(); // Greater than operator overload test cout << "Is date 5 greater than date 6?" << (d5 > d6 ? "Yes" : "No") << endl; //Equality operator overload test cout << "Is date 1 equal to date 2?" << (d1 == d2 ? "Yes" : "No") << endl; // Not equal to operator overload test cout << "Isn't date 3 equal to date 4?" << (d3 != d4 ? "Yes" : "No") << endl; // Output stream operator overload test cout << "Output stream operator overload for date 1:" << d1 << endl; // Input stream operator overload test Date d8; cout << "Please enter a date (year, month, day):"; cin >> d8; cout << "The date you entered is:" << d8 << endl; return 0; }
C++ implements a simple date class, including date addition, subtraction, size comparison and other functions. The implementation of the date class can help us process dates more conveniently and improve the readability and maintainability of the code.
OJ questions related to date calculation
HJ73 Calculate date to day conversion
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded?tpId=37 & amp; & amp;tqId=21296 & amp;rp=1 & amp;ru=/activity/oj & amp;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 & amp; & amp; ((y % 4 == 0 & amp; & amp; y % 100 != 0) || (y % 400 == 0))) {<!-- --> day + = 1; } day + = month[m]; cout << day << endl; }
KY111 date difference
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c?tpId=62 & amp; & amp;tqId=29468 & amp;rp=1 & amp;ru=/activity/oj & amp;qru=/ta/sju -kaoyan/question-ranking
#include <iostream> #include <string> using namespace std; // Determine whether it is a leap year bool isLeapYear(int year) {<!-- --> return (year % 4 == 0 & amp; & amp; year % 100 != 0) || (year % 400 == 0); } // Get the number of days in a certain month of a certain year int getDaysOfMonth(int year, int month) {<!-- --> int daysOfMonth[] = {<!-- -->0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 & amp; & amp; isLeapYear(year)) {<!-- --> return 29; } return daysOfMonth[month]; } // Calculate the number of days from AD 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; } // Calculate the difference in days between two dates int getDaysDiff(const string & amp; date1, const string & amp; date2) {<!-- --> int year1, month1, day1, year2, month2, day2; sscanf(date1.c_str(), "M--", & amp;year1, & amp;month1, & amp;day1); sscanf(date2.c_str(), "M--", & amp;year2, & amp;month2, & amp;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 print date
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=69 & amp; & amp;tqId=29669 & amp;rp=1 & amp;ru=/activity/oj & amp;qru=/ta/hust -kaoyan/question-ranking
#include <iostream> using namespace std; bool isLeapYear(int year) {<!-- --> return (year % 4 == 0 & amp; & amp; 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 & amp; & amp; 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("M- d- d\ ", year, month, day); } }
KY258 date accumulation
https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d?tpId=40 & amp; & amp;tqId=31013 & amp;rp=1 & amp;ru=/activity/oj & amp;qru=/ta/kaoyan /question-ranking
#include <iostream> using namespace std; bool isLeapYear(int year) {<!-- --> return (year % 4 == 0 & amp; & amp; 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 & amp; & amp; isLeapYear(year)) {<!-- --> return 29; } return daysOfMonth[month]; } int main() {<!-- --> int t, year, month, day, add; cin >> t; while (t--) {<!-- --> // Note that while handles multiple cases cin >> year >> month >> day >> add; day + = add; while (day > getMonthDay(year, month)) {<!-- --> day -= getMonthDay(year, month); + + month; if (13 == month) {<!-- --> month = 1; + + year; } } printf("M- d- d\ ", year, month, day); } }