打印日历挂
一、代码
.h
#ifndef __CALEN_H__
#define __CALEN_H__
extern int LeapYear(int year);//判断是否为闰年
extern int Year_1900(int year);//某年距离1990.1.1有多少天
extern int GetDay(int month);//给一个月份输出天数
extern int Month_1(int month);//某月距离1.1有多少天
extern int SumYM(int year,int month);//某年某月距离1990.1.1共多少天
extern int WeekDay(int day);//某月为周几
extern void PrintCal(int year, int day);//打印日历
#endif
.c
#include <stdio.h>
#include "main.h"
extern int LeapYear(int year)//判断是否为闰年
{
if (0 == year % 4 && 0 != year % 100 || 0 == year % 400)
{
return 366;
}
else
{
return 365;
}
}
extern int Year_1900(int year)//某年距离1990.1.1有多少天
{
int sum = 0;
int i = year - 1;
for (i; i >= 1900; --i)
{
sum += LeapYear(i);
}
return sum;
}
extern int GetDay(int year, int month)//判断某月多少天
{
switch(month)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;
case 4:case 6:case 9:case 11:return 30;
case 2:
if (366==LeapYear(year))
{
return 29;
}
else
{
return 28;
}
}
}
extern int Month_1(int year, int month)
{
int sum = 0;
for (month; month > 1; --month)
{
sum += GetDay(year, month);
}
return sum;
}
extern int SumYM(int year, int month)//某年某月距离1990.1.1共多少天
{
int sum = 0;
sum = Year_1900(year) + Month_1(year, month);
return sum;
}
extern int WeekDay(int year, int month)//某月为周几
{
int week = (SumYM(year, month)+1)% 7;
return week;
}
extern void Print7(void)
{
int i = 0;
for (i; i < 7; ++i)
{
}
}
extern void PrintCal(int year, int month)
{
printf("------%d年%d月-------\n", year, month);
printf("日 一 二 三 四 五 六\n");
int cal[35] = { 0 };
int i, j=0,k = 1;
i = WeekDay(year, month);//4
int t;
for (t = 0; t < i; ++t)
{
printf(" ");
}
int days = GetDay(year, month);
for (j = 1; j <= days; ++j)
{
printf("%2d ", j);
++t;
if (t % 7 == 0)
{
puts("");
}
}
}
main.c
int main(void)
{
int year = 2025;
int month = 1;
PrintCal(year,month);
}
二、测试用例