自寫打印日曆類

在論壇上看到有人在問打印日曆的程序,今天下午空閒的時候寫了一個日曆類,簡單的寫了幾個方法。


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" };


註釋寫的很詳細了,不多做解釋了。


發佈了222 篇原創文章 · 獲贊 45 · 訪問量 63萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章