How to realize the function of perpetual calendar on C++
This article will explain in detail how to achieve the perpetual calendar function of C++. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The details are as follows
1. This perpetual calendar function
1 > date plus or minus days
2 > the difference between dates
3 > enter the year and month to display the calendar of the current month
two。 Code implementation
# include#includeusing namespace std; class Date {public: Date (int year = 1990, int month = 1, int day = 1) / / Constructor: _ year (year), _ month (month), _ day (day) {if (JudgeRightDate ()) / / determine whether the passed value is legal. If it is illegal, it is set to January 1, 1990 {_ year = 1990; _ month = 1; _ day = 1 }} bool JudgeRightDate () / / determine whether the value is legal function {if (_ year)
< 1 || ((_month< 1)||_month>12) | | (_ dayGetMonthDay (_ year,_month)) {return true;} else {return false;}} int JudgeYear (int year) / / function to determine whether it is a leap year {if ((year% 4 = = 0) & & (year% 100! = 0)) | | (year% 400 = = 0) {return 1;} else return 0 } int GetMonthDay (int year, int month) / / the corresponding number of days {int arr [13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int days = arr [month]; if (month = = 2) {days + = JudgeYear (year); / / the number of days in February in a leap year plus 1} return days } Date operator + (int days) / / date plus days function, overload "+" to achieve {_ day + = days; / / first add all the days to the "day" of the given date (_ year, _ month, _ day); / / then calculate the correct date. Return * this;} void GetRightDate (int & year, int & month, int & day) / / calculate the correct date {if (day 12) {year++; month = 1;}} Date operator-(int days) / / overloaded "-" realization date minus days {_ day-= days; GetRightDate (_ year, _ month, _ day); return * this } bool operator > (const Date & d) / / judge the size of two dates {if (_ year > d._year) {return true;} else if (_ year = = d._year) {if (_ month > d._month) {return true;} else if (_ month==d._month) {if (_ day > d._day) {return true;}} return false } bool operator = = (const Date & d) / / judge whether two dates are equal {if (_ year = = d._year & & _ month = = d._month & & _ day = = d._day) {return true;} else return false;} int operator-(Date & d) / / calculate the date difference function, overload "-" to achieve {int count = 0; Date tmp (* this); if (* this > d) {tmp = d; d = * this * this= tmp;} while (! (* this==d)) {count++; * this= * this+1;} return count;} void print () / / print function {cout