在论坛上看到有人在问打印日历的程序,今天下午空闲的时候写了一个日历类,简单的写了几个方法。
calendar.h
#include <string> using namespace std; #ifndef CALENDAR_H #define CALENDAR_H class 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来计算某年某月某日为周几author:csdn iaccepted /*http://blog.csdn.net/iaccepted *********************************************************************/ inline int 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; //这里要注意,当月份小于3的时候,要把月份变成上年的13、14月份,所以年份也变成上年了 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; } /**********************************************************/ /*向月日历数组中填写本月的日历信息author:csdn iaccepted /*(第一行的第一个位置即周日的位置永远不可能有元素) /*http://blog.csdn.net/iaccepted ***********************************************************/ void Calendar::fillOneMonth(const int &year, const int &month){ int first = calOneDay(year, month, 1); //返回的first规则(0-sat,1-sun,2-mon,3...)进过这个公式处理后的规则(1-mon, 2-tue, 3-wnd.....) first = ((first + 5) % 7) + 1; int cnt = 1; memset(oneMonth, 0, sizeof(oneMonth)); //仔细观察日历就会发现其实每个月的信息就是一个6*7的数组 for (int i = first; cnt <= monthDays[month]; ++i){ oneMonth[i / DAY_PER_LINE][i % DAY_PER_LINE] = cnt; ++cnt; } } /******************************************************************/ /*通过调用打印某个月的函数来完成所有月份的打印author:csdn iaccepted /*http://blog.csdn.net/iaccepted *****************************************************************/ void Calendar::printAllMonth(const int &year){ for (int i = 1; i <= MONTH_NUM; ++i){ printOneMonth(year, i); } } /*********************************************************/ /*可以通过具体日期查询该日期是周几author:csdn iaccepted /*http://blog.csdn.net/iaccepted **********************************************************/ void Calendar::printOneDay(const int &year, const int &month, const int &day){ int dayOfWeek = calOneDay(year, month, day); printf("%d-%02d-%02d : %s\n", year, month, day, weekName[dayOfWeek].c_str()); } /*********************************************************/ /*对于特定的年份和月份,调用fillOneMonth函数 /*在月份表中填写本月日历信息,并正常打印出来csdn iaccepted /*http://blog.csdn.net/iaccepted **********************************************************/ void 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"); } /********************************************************/ /*输入年份,来处理闰年和平年二月份天数的问题csdn iaccepted /*http://blog.csdn.net/iaccepted *********************************************************/ inline void Calendar::init(const int &year){ if ((year % 4 == 0 && year % 400 != 0) || (year % 400 == 0)){ monthDays[2] = 29; } else{ monthDays[2] = 28; } } /*********************************************/ /*输入年和月打印每个月的表头信息 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, 30, 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" };
注释写的很详细了,不多做解释了。
自写打印日历类
时间: 2024-10-12 09:18:41