c语言编写的日历

时间:2021-10-10 14:08:22

输入年份如2013,显示2013年的日历。

思路:

1.查找每个月1号是星期几(这里利用了1990年1月1号是星期一)

  计算年份如2013年1月1号到1990年1月1号有Days天,Day%7得到星期索引WeekDay

2.每个月日历打印六行内容,每行七个日期,不是日历内容打印空格

#include <stdio.h>

#define BOOL int
#define TRUE 1
#define FALSE 0 int 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 ;
}
/*-------------------------------------------------------------------------
功能:获取year年,month月,day天是星期几 这里利用了1900年1月1号是星期一
输入:年份year,月份month日期day
输出:星期索引
---------------------------------------------------------------------------*/
int GetWeekDay(int year, int month, int day)
{
int count, Week_Index,i;
int Day[] = {, , , , , , , , , , , , }; /* 每月的天数 */
int MonthAdd[] = {, , , , , , , , , , , }; /* 每月前所有月份的天数之和 */ count = MonthAdd[month - ]; /* 月份month前面所有月份的天数 */
count = count + (year - ) * ;
count = count + day; /* 与1900年1月1号相差多少天 */
if(month > && IsLeap(year)) /* 月份month超过2月份 且是闰年 */
count ++;
for(i = ; i < year; i ++)
{
if(IsLeap(i)) /* 如果是闰年 */
{
count ++;
}
} Week_Index = count % ; return Week_Index;
}
/*------------------------
功能:打印year年的日历
输入:year年份
输出:year年的日历
--------------------------*/
void PrintCalendar(int year)
{
int i, j, k;
int WeekDay;
int Day[] = {, , , , , , , , , , , , };
int MonthDays; for(i = ; i < ; i ++) /* 依次打印每个月份的日历 */
{
int temp = ;
MonthDays = Day[i];
if(IsLeap(year) && i == ) /* 闰年第二个月为29天 */
MonthDays = ;
WeekDay = GetWeekDay(year, i, ); /* 获取每个月1号 星期索引 */
printf("%d月\n", i);
printf("日\t一\t二\t三\t四\t五\t六\n");
for(j = ; j <= ; j ++) /* 每个月日历打印六行 */
{
if(WeekDay != )
{
printf("\t");
WeekDay --;
if(j % == )
printf("\n");
continue;
}
if(MonthDays > ) /* 每个月的日历 */
{
printf("%d\t", temp);
temp ++;
if(j % == )
printf("\n");
MonthDays --;
}
else
printf("\t");
}
printf("\n");
}
}
/*--------------------------
功能:判断year是否为闰年
输入:年份year
输出:闰年TRUE平年FALSE
---------------------------*/
BOOL IsLeap(int year)
{
BOOL result;
if(((year % == ) && (year % == )) || ((year % != ) && (year % == ))) /* 闰年 */
{
result = TRUE;
}
else
result = FALSE; return result;
}

c语言编写的日历