#include <iostream>
using namespace std;
#define calendar 35
#define M 100
int isgrid(int a[], int b, int c);
bool isYaer(int year) {//判断闰年
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
else {
return false;
}
}
void printCanlendar(int year, int mon) {
int a[12] = { 31, 28, 31, 30, 31, 30 ,31, 31, 30, 31, 30, 31 };
char day[7][20] = { {"Monday"},{"Tuesday"}, {"Wednesday"}, {"Thursday"}, {"Friday"}, {"Saturday"}, {"Sunday"}};
int bData[367] = { 0 };
int count = 0;
int interval = (mon - 1) * calendar;//根据月份让数组从不同位置输出
int model = 0;
int lattice = ((year - 1901) % 7 + 1 + (year - 1901) / 4) % 7;//计算出1901年往后的每一年一月份前面的空格
if (isYaer(year)) {
a[1] = 29;
}
//把日历的数值赋值给数组
for (int i = 0; i < 12; i++) {
for (int j = 0; j < a[i]; j++) {
bData[count++] = j + 1;
}
}
if (1 != mon) {
interval -= isgrid(bData, mon, lattice) * 7;//矫正输出位置
}
//打印日历表标题
for (int i = 0; i < 7; i++) {
printf("%s", day[i]);
}
//打印日历表
for (int i = interval - lattice; i < (calendar + interval - lattice + 7) && i < 366; i++, model++) {
if (model % 7 == 0) {
printf("\n");
}
if (i < 0) {
printf(" \t");
continue;
}
printf("%d \t", bData[i]);
}
}
int isgrid(int bData[],int mon, int lattice) {
int sum = 0;
int ismon = 1;
for (int i = 35 - lattice - 1; i < 367 && ismon < mon; ) {
if (bData[i] > 10) {
i += 35;
ismon++;
}
else {
sum++;
i += 28;
ismon++;
}
}
return sum;
}
int main() {
printCanlendar(2014, 7);
}
怕什么真理无穷,进一寸有进一寸的欢喜 -胡适