#include <stdio.h> //判断输入的年份是否是闰年 int IsLeap(int year) { if((year%400==0) || ((year%4==0)&&(year%100!=0))) return 1; else return 0; } int Day[12]={31,28,31,30,31,30,31,31,30,31,30,31}; //计算一个月的最大天数(上一个月) int Max_day(int year,int month) { if(IsLeap(year)==1) Day[1]=29; return Day[month-1]; } //计算输入的日期是这一年的第几天 int Total_day(int year,int month,int day) { int sum = 0; for(int i=1;i<month;i++) sum+=Max_day(year,i); sum+=day; return sum; } //由输入的日期判断当天是星期几 int Weekday(int year,int month,int day) { if(month==1||month==2) { month+=12; year--; } int week = (day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7; return week; } void Display_week(int year,int month,int day) { int weekday = Weekday(year,month,day); switch(weekday) { case 0:printf("%d年%d月%d日是星期一\n",year,month,day);break; case 1:printf("%d年%d月%d日是星期二\n",year,month,day);break; case 2:printf("%d年%d月%d日是星期三\n",year,month,day);break; case 3:printf("%d年%d月%d日是星期四\n",year,month,day);break; case 4:printf("%d年%d月%d日是星期五\n",year,month,day);break; case 5:printf("%d年%d月%d日是星期六\n",year,month,day);break; case 6:printf("%d年%d月%d日是星期日\n",year,month,day);break; } } //显示当月日历 void ShowCalendar(int year,int month,int day) { int i; printf("==================================================\n"); printf("日 一 二 三 四 五 六\n"); printf("==================================================\n"); int week = Weekday(year,month,1); //由每月1号确定打印制表符的个数 //printf("week:%d",week); for(i=0;i<=week;i++) printf("\t"); int max = Max_day(year,month); for(int j=1;j<=max;j++) { if(i%7==0) printf("\n"); printf("%d\t",j); i++; } printf("\n==================================================\n"); } int main() { int year,month,day; printf("欢迎使用万年日历v1.0!\n"); printf("请依次输入年,月,日:"); scanf("%d%d%d",&year,&month,&day); Display_week(year,month,day); printf("%d年%d月%d日是这一年中的第%d天\n",year,month,day,Total_day(year,month,day)); ShowCalendar(year,month,day); return 0; }
时间: 2024-10-10 15:32:26