c语言万年历 日历

/*
 * cal.c
 *
 *  Created on: 2013-3-11
 *      Author: J.WANG
 */
#include <stdlib.h>
#include <stdio.h>
//三维数组存放日期,每年12月,日历中最多排6行(星期),每星期7天
int days[12][6][7];
//平年,闰年每个月的天数
int ds[2][12] = { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 31, 29,
		31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
//标题
char title[] = "SUM MON TUE WED THU FRI SAT";
//月份
int m[4][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
//月份
char mt[][4] = { "JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC" };

//根据蔡勒公式计算日期星期几
int wd(int year, int m, int d) {
	int c, y, w;
	if (m <= 2) {
		year--;
		m += 12;
	}
	c = year / 100;
	y = year % 100;
	w = (c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1);
	return (w % 7 + 7) % 7;
}
//闰年平年
int leapYear(int year) {
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int main() {
	int i, j, k, leap, fd,year;

	scanf("%d%*c",&year);

	//初始三维数组为0
        //days[12][6][7]={0};
	for (i = 0; i < 12; i++) {
		for (j = 0; j < 6; j++) {
			for (k = 0; k < 7; k++) {
				days[i][j][k] = 0;
			}
		}
	}
	fd = wd(year, 1, 1);
	leap = leapYear(year);
	//三维数组赋值
	for (i = 0; i < 12; i++) {
		for (k = 0, j = 1; j <= ds[leap][i]; j++) {
			days[i][k][fd] = j;
			fd = (++fd) % 7;
			if (fd == 0) {
				k++;
			}
		}
	}
	//输出数据
	for (i = 0; i < 4; i++) {
		printf(" %13s   %27s   %27s \n", mt[m[i][0] - 1], mt[m[i][1] - 1],
				mt[m[i][2] - 1]);
		printf(" %s   %s   %s \n", title, title, title);
		for (j = 0; j < 6; j++) {
			//1,4,7
			for (k = 0; k < 7; k++) {
				if (days[m[i][0] - 1][j][k])
					printf("%4d", days[m[i][0] - 1][j][k]);
				else
					printf("    ");
			}
			printf("  ");
			//2,5,8
			for (k = 0; k < 7; k++) {
				if (days[m[i][1] - 1][j][k])
					printf("%4d", days[m[i][1] - 1][j][k]);
				else
					printf("    ");
			}
			printf("  ");
			//3,6,9
			for (k = 0; k < 7; k++) {
				if (days[m[i][2] - 1][j][k])
					printf("%4d", days[m[i][2] - 1][j][k]);
				else
					printf("    ");
			}
			printf("\n");
		}

	}
	return 0;
}

 

 打印格式:



c语言万年历 日历_第1张图片
 
 

你可能感兴趣的:(C语言)