输入年份如2013,显示2013年的日历。
思路:
1.查找每个月1号是星期几(这里利用了1990年1月1号是星期一)
计算年份如2013年1月1号到1990年1月1号有Days天,Day%7得到星期索引WeekDay
2.每个月日历打印六行内容,每行七个日期,不是日历内容打印空格
#include
#define TRUE 1
#define FALSE 0int GetWeekDay(int year, int month, int day); /* 获取某一年,某一月,某一天是星期几 */
void PrintCalendar(int year); /* 打印第year年的日历 */
BOOL IsLeap(int year); /* 判断是否为闰年 */int main()
{int year;
// char* week[] = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; /* 字符指针数组 */// int WeekDay = GetWeekDay(2000, 1, 1);// printf("今天是星期:%s\n", week[WeekDay]);
printf("请输入要查询的年份:\n");scanf("%d", &year);PrintCalendar(year);return 0;
}
/*-------------------------------------------------------------------------功能:获取year年,month月,day天是星期几 这里利用了1900年1月1号是星期一输入:年份year,月份month日期day输出:星期索引
---------------------------------------------------------------------------*/
int GetWeekDay(int year, int month, int day)
{int count, Week_Index,i;int Day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* 每月的天数 */int MonthAdd[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; /* 每月前所有月份的天数之和 */count = MonthAdd[month - 1]; /* 月份month前面所有月份的天数 */count = count + (year - 1900) * 365; count = count + day; /* 与1900年1月1号相差多少天 */if(month > 2 && IsLeap(year)) /* 月份month超过2月份 且是闰年 */count ++;for(i = 1900; i
}
/*------------------------功能:打印year年的日历输入:year年份输出:year年的日历
--------------------------*/
void PrintCalendar(int year)
{int i, j, k;int WeekDay;int Day[13] &#61; {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};int MonthDays;for(i &#61; 1; i <13; i &#43;&#43;) /* 依次打印每个月份的日历 */{int temp &#61; 1;MonthDays &#61; Day[i];if(IsLeap(year) && i &#61;&#61; 2) /* 闰年第二个月为29天 */MonthDays &#61; 29;WeekDay &#61; GetWeekDay(year, i, 1); /* 获取每个月1号 星期索引 */printf("%d月\n", i);printf("日\t一\t二\t三\t四\t五\t六\n");for(j &#61; 1; j <&#61; 42; j &#43;&#43;) /* 每个月日历打印六行 */{if(WeekDay !&#61; 0){printf("\t");WeekDay --;if(j % 7 &#61;&#61; 0)printf("\n");continue;} if(MonthDays > 0) /* 每个月的日历 */{printf("%d\t", temp); temp &#43;&#43;;if(j % 7 &#61;&#61; 0)printf("\n");MonthDays --;}elseprintf("\t"); }printf("\n");}
}
/*--------------------------功能&#xff1a;判断year是否为闰年输入&#xff1a;年份year输出&#xff1a;闰年TRUE平年FALSE
---------------------------*/
BOOL IsLeap(int year)
{BOOL result;if(((year % 100 &#61;&#61; 0) && (year %400 &#61;&#61; 0)) || ((year % 100 !&#61; 0) && (year % 4 &#61;&#61; 0))) /* 闰年 */{result &#61; TRUE;}elseresult &#61; FALSE;return result;
}