#include <stdio.h>
int year;
int leap(int year); //判断闰年
int newday(); //判断改年的元旦是星期几
char *month_name(int n); //月份转换函数
int print_head(void); //打印日历头
int print_screen(void); //打印日历到文件
int save_to_file(void); //保存到文件
int print_ymonth_screen(void); //按年月显示
int leap(int year)
{
if(((0 == year%4) && (0 != year%100)) || (0 == year%400))
{
return (1);
}
else
{
return (0);
}
}
int newday()
{
int i,n;
if(year >= 1900)
{
n = year-1900;
for(i = 1900; i < year-1; i++)
{
if(leap(i))
{
n++;
}
}
}
n = (n+1)%7;
return (n);
}
char *month_name(int n)
{
char *month[] =
{
"Illegal month", //无效月份
"January",
"Februray",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
return (((n>=1)&&(n<=12))?month[n]:month[0]);
}
int print_head()
{
printf("-------------------------------------------------\n");
printf("SUN MON TUE WED THU FRI SET \n");
printf("-------------------------------------------------\n");
return (1);
}
int print_screen()
{
int i,month,day,weekday,len_month;
printf("\t\t\tPlease input year:");
scanf("%d", &year);
printf("-----------------------%d------------------------\n", year);
weekday = newday();
for(month = 1; month <= 12; month++)
{
printf("\n%d %s\n", month,month_name(month));
print_head();
if(2 == month)
{
if(leap(year))
{
len_month = 29;
}
else
{
len_month = 28;
}
}
else if(4 == month || 6 == month || 9 == month || 11==month)
{
len_month = 30;
}
else
{
len_month = 31;
}
for(i = 0; i <weekday; i++) //寻找当月1日打印位置
{
printf(" ");
}
for(day=1; day<=len_month; day++)
{
printf(" %2d ", day);
weekday++;
if(7 == weekday) //打满一周换行
{
weekday = 0;
printf("\n");
}
}
printf("\n");
}
return (1);
}
int save_to_file()
{
FILE *fp;
int i,month,day,weekday,len_month;
fp=fopen("calendar.txt","w+");
fprintf(fp,"-----------------------%d------------------------\n", year);
weekday = newday();
for(month = 1; month <= 12; month++)
{
fprintf(fp,"\n%d %s\n", month,month_name(month));
fprintf(fp,"-------------------------------------------------\n");
fprintf(fp,"SUN MON TUE WED THU FRI SET \n");
fprintf(fp,"-------------------------------------------------\n");
if(2 == month)
{
if(leap(year))
{
len_month = 29;
}
else
{
len_month = 28;
}
}
else if(4 == month || 6 == month || 9 == month || 11==month)
{
len_month = 30;
}
else
{
len_month = 31;
}
for(i = 0; i <weekday; i++) //寻找当月1日打印位置
{
fprintf(fp," ");
}
for(day=1; day<=len_month; day++)
{
fprintf(fp," %2d ", day);
weekday++;
if(7 == weekday) //打满一周换行
{
weekday = 0;
fprintf(fp,"\n");
}
}
fprintf(fp,"\n");
}
return (1);
}
int print_ymonth_screen()
{
int i,month,day,weekday,len_month;
printf("\t\t\tPlease input year:");
scanf("%d", &year);
printf("\t\t\tPlease input month(1-12):");
scanf("%d", &month);
printf("-----------------------%d------------------------\n", year);
weekday = newday();
printf("\n%d %s\n", month,month_name(month));
print_head();
if(2 == month)
{
if(leap(year))
{
len_month = 29;
}
else
{
len_month = 28;
}
}
else if(4 == month || 6 == month || 9 == month || 11==month)
{
len_month = 30;
}
else
{
len_month = 31;
}
for(i = 0; i <weekday; i++) //寻找当月1日打印位置
{
printf(" ");
}
for(day=1; day<=len_month; day++)
{
printf(" %2d ", day);
weekday++;
if(7 == weekday) //打满一周换行
{
weekday = 0;
printf("\n");
}
}
printf("\n");
return (1);
}
int main()
{
char ch;
int i;
printf("\t 您好!欢迎使用简易万年历!\n\n\n\n");
do
{
printf("\t|===========================================================|\n");
printf("\t| 欢迎您使用万年历 愿您的生活更美好 |\n");
printf("\t| |\n");
printf("\t| 简易万年历 |\n");
printf("\t| 制作人:xk_qq |\n");
printf("\t|===========================================================|\n");
printf("\t| 1................只在屏幕上输出某年日历.......|\n");
printf("\t| 2...........既屏幕输出也文件保存calendar.tx...|\n");
printf("\t| 3................只在屏幕上输出某年某月日历...|\n");
printf("\t|===========================================================|\n");
printf("\t请输入您的选择:");
scanf("%d", &i);
switch(i)
{
case 1:
print_screen();
break;
case 2:
print_screen();
save_to_file();
break;
case 3:
print_ymonth_screen();
break;
default:
printf("对不起,您的选择无效!\n");
break;
}
printf("按y键重新选择,其他键退出本系统!");
scanf("\n%c", &ch);
}while((ch=='y')||(ch=='Y'));
printf("感谢您的使用,欢迎下次使用!\n");
return 1;
}