c语言万年历
- 提问者网友:遮云壑
- 2021-05-07 13:20
- 五星知识达人网友:想偏头吻你
- 2021-05-07 14:52
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
enum{Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday};
int M_GetYear(void);
void M_PrintMonthNum(int year);
void M_PrintMonth(int month, int year);
char *M_MonthName(int month);
int M_MonthDays(int year, int month);
int M_IsLeapYear(int year);
int M_YeFirstMonthDay(int year, int month);
void M_BlankUnit(int weekday);
int main(void)
{
int year;
year = M_GetYear();
M_PrintMonthNum(year);
getch();
}
int M_GetYear(void)
{
int year;
while(TRUE)
{
printf("(大于1900)请输入年份 : ");
scanf("%d", &year);
if(year > 1900)
{
return year;
}
}
}
void M_PrintMonthNum(int year)
{
int month;
for(month = 1; month <= 12; month++)
{
M_PrintMonth(month, year);
printf("\n");
}
}
void M_PrintMonth(int month, int year)
{
int weekday, ndays, day;
printf(" %s", M_MonthName(month));
printf("\n Su Mo Tu We Th Fr Sa\n");
ndays = M_MonthDays(year, month);
weekday = M_YeFirstMonthDay(year, month);
M_BlankUnit(weekday);
for(day = 1; day <= ndays; day++)
{
printf(" %2d", day);
if(weekday == Saturday)
{
printf("\n");
}
weekday = (weekday + 1) % 7;
}
if(weekday != Sunday)
printf("\n");
}
char *M_MonthName(int month)
{
char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (month < 1 || month > 12) ? name[1] : name[month];
}
int M_MonthDays(int year, int month)
{
switch(month)
{
case 2 :
if(M_IsLeapYear(year))
{
return 29;
}
return 28;
case 4 : case 6 : case 9 : case 11 :
return 30;
default :
return 31;
}
}
int M_IsLeapYear(int year)
{
return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
}
int M_YeFirstMonthDay(int year, int month)
{
int i, weekday;
weekday = Monday;
for(i = 1900; i < year; i++)
{
weekday = (weekday + 365) % 7;
if(M_IsLeapYear(i))
{
weekday = (weekday + 1) % 7;
}
}
for(i = 1; i < month; i++)
{
weekday = (weekday + M_MonthDays(year, i)) % 7;
}
return weekday;
}
void M_BlankUnit(int weekday)
{
int i;
for(i = 0; i < weekday; i++)
{
printf(" ");
}
}
懒得写,百度上面找的,还真找着了