On the forum, I saw someone asking about the calendar Printing program. I wrote a calendar class during my free time this afternoon. I wrote several simple methods.
Calendar. h
#include <string>using namespace std;#ifndef CALENDAR_H#define CALENDAR_Hclass Calendar{public:Calendar(){}void printAllMonth(const int &year);void printOneMonth(const int &year, const int &month);void printOneDay(const int &year, const int &month, const int &day);private:int oneMonth[6][7];static string monthName[13];static string weekName[8];static int monthDays[13];int calOneDay(const int &year, const int &month, const int &day)const;void fillOneMonth(const int &year, const int &month);void init(const int &year);void printHead(const int &year, const int &month);};const int LINE_NUM = 6;const int DAY_PER_LINE = 7;const int MONTH_NUM = 12;#endif
Calendar. cpp
# Include <string> # include "calendar. H "# include <cstdio> /******************************** *************************************//* zeller's congruence is used to calculate the number of author days of a week in a month: csdn iaccepted/* invalid calendar: caloneday (const Int & year, const Int & month, const Int & day) const {int M = month, j = year/100, K = year % 100; If (month <3) {M + = 12; // note that when the month is less than 3, to change the month to the 13 and 14 months of the previous year, the year also becomes J = (year-1)/100; k = (year-1) % 100 ;} int res = (day + 13 * (m + 1)/5 + K + (K/4) + (J/4) + 5 * j) % 7; return res ;} /*************************************** * ***************** // fill in the calendar information author of the month in the monthly calendar array: csdn iaccepted ********************************* * ************************/Void calendar: fillonemonth (const Int & year, const Int & month) {int first = caloneday (year, month, 1); // The first rule returned (0-Sat, 1-sun, 2-mon, 3 ...) rules after processing this formula (1-mon, 2-tue, 3-wnd .....) first = (first + 5) % 7) + 1; int CNT = 1; memset (onemonth, 0, sizeof (onemonth )); // observe the calendar carefully and you will find that the monthly information is a 6*7 array for (INT I = first; CNT <= monthdays [month]; ++ I) {onemonth [I/day_per_line] [I % day_pe R_line] = CNT; ++ CNT ;}} /*************************************** * *************************** // call the function to print a month to complete all author: csdn iaccepted/* invalid calendar: printallmonth (const Int & year) {for (INT I = 1; I <= month_num; ++ I) {printonemonth (year, I );}} /*************************************** ******************/ /* You can query the author: csdn iaccepted/* Calendar calendar: printoneday (const Int & year, const Int & month, const Int & day) of a week by date) {int dayofweek = caloneday (year, month, day); printf ("% d-% 02d-% 02d: % s \ n", year, month, day, weekname [dayofweek]. c_str ());} /*************************************** * ***************** // * for a specific year and month Call the fillonemonth function/* fill in the calendar information of the current month in the month table, and print out the csdn iaccepted/* Calendar calendar: printonemonth (const Int & year, const Int & month) {If (month = 2) {Init (year);} fillonemonth (year, month); printhead (year, month); For (INT I = 0; I <line_num; ++ I) {for (Int J = 0; j <day_per_line; ++ J) {If (onemonth [I] [J]! = 0) {printf ("% 2D \ t", onemonth [I] [J]);} else {printf ("\ t ");}} printf ("\ n");} printf ("\ n ");} /*************************************** * **************** // enter the year, csdn iaccepted/* symbol calendar: Init (const Int & year) {If (Year % 4 = 0 & amp; Year % 400! = 0) | (Year % 400 = 0) {monthdays [2] = 29;} else {monthdays [2] = 28 ;}} /*************************************** * ***** // enter the year and month to print the header information of each month. csdn iaccepted/* http://blog.csdn.net/iaccepted**********************************************/inline void calendar:: printhead (const Int & year, const Int & month) {printf ("% d-% 02d \ t % s \ n", year, month, monthname [month]. c_str (); printf ("Sun \ tmon \ ttue \ twed \ tthu \ tfri \ tsat \ n");} int calendar: monthdays [13] = {0, 31, 0, 31, 30, 31, 30, 31, 31, 31, 30, 31}; string calendar: monthname [13] = {"", "January", "February", "March", "Apirl", "may", "June", "July", "August", "September", "October ", "November", "December"}; string calendar: weekname [8] = {"sat", "Sun", "mon", "Tue", "wed ", "Thu", "fri "};
The annotations are very detailed and I will not explain them much.