#include #include #include #include int year,month,day; int day_of_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};//每月的最后一天 int current_year,current_month,current_day;//当前的时间 /*******************其他函数********************/ int judgement(int year)/*用来判断闰年*/ { if(year%400==0||year%4==0&&year%100!=0)//闰年能被400整除或者被4整除但是不能整除100 return 1; else return 0; } int show_week(int year,int month,int day) { /* 公式:w=(y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1)%7 w是星期,y是年份的后两位减1,c是世纪,m是月份(其中1月按13算,2月按14算),d是当天的日期 */ int w ,k; //记录是周几 int year_last=year%100,c=year/100 , m = month; if (month==1 ) { year_last-=1 ; m=13 ; } else if (month==2) { year_last-=1; m=14; } w = (year_last + year_last/4 + c/4 - 2*c +26*(m+1)/10+day-1); if (w<0) { k=(w%7+7)%7; } else k=w%7; return k ; } int current_time() { time_t timep; struct tm *p; time(&timep); p=gmtime(&timep); current_year=1900+p->tm_year; current_month=1+p->tm_mon; current_day=p->tm_mday; } /*------------------功能一 -----------------*/ int print_year(int year)//输入年份 { int i,k,j,frist_week; printf("Please input the year whose calender you want to know:"); scanf("%d",&year) ; printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The Calender of Year %d~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",year); printf("\n"); if(judgement(year))//调用judgement,判断年份是否为闰年 { day_of_month[1]=29;//闰年 } else day_of_month[1]=28; for(i=1;i<13;i++) { frist_week=show_week(year,i,1); printf(" %d月 \n",i); printf("SUN\tMOD\tTUE\tWED\tTHU\tFRI\tSAT\n"); for(j=1;j<=frist_week-1;j++) { printf("\t"); if (j%7==0) printf ("\n"); } for (k=1;k<=day_of_month[i-1];k++) { printf("%d\t",k); if (j%7==0) printf ("\n"); j++; } printf("\n"); printf("\n"); printf("\n"); } } /*-------------------功能二------------------*/ int print_year_month()//输入年月 { int x,y,i,frist_week; printf("Please input the month whose calender you want to know"); do { scanf("%d.%d",&year,&month); if(month<1||month>12) { printf("the month is error\n"); printf("Please input the month whose calender you want to know\n"); } }while(month<1||month>12); printf("*********************%d年%d月**********************\n",year,month); if(judgement(year)) { day_of_month[1]=29; } else day_of_month[1]=28; frist_week=show_week(year,month,1); printf ("SUN\tMOD\tTUE\tWED\tTHU\tFRI\tSAT\n"); for(x=1;x<=frist_week;x++) { printf("\t"); if (x%7==0) printf ("\n"); } for (y=1;y<=day_of_month[month-1];y++) { printf("%d\t",y); if (x%7==0) printf ("\n"); x++; } printf("\n"); } /*-----------------功能三------------------*/ int sumdays(int year,int month,int day) { int days=0,days1=0,days2=0,days3=0,temp_day=0;//部分时间之和 int i,judgement1,sum_days;//当前时间,总时间 judgement1=judgement(year);//闰年数 if(year current_year )/***********今年之后**************///同理 { for (i =current_year+1;i < current_year ;i++)//计算今年与输入的年份之间的天数 { if (judgement(i)) { days=days+366; } else days+=365; } for (i = current_month+1;i<=12;i++)//今年还剩多少个月 { days=days+day_of_month[i-1];//今年还剩多少天,月份相加 } days=days+day_of_month[month-1]-current_day; for (i = 0;i current_month)//假设输入月份在当前月份之后 { for (i=current_month+1;iday_of_month[month-1]); printf("%d年%d月%d号是:",year,month,day); } while(day<=0||day>day_of_month[month-1]); week=show_week(year,month,day); switch(month) { case 1:switch(day) { case 1:printf("元旦"); break; default:printf("不是阳历节日"); } break; case 2:switch(day) { case 14:printf("情人节"); break; default:printf("不是阳历节日"); } break; case 3:switch(day) { case 8:printf("妇女节");break; case 12:printf("植树节");break; default:printf("不是阳历节日"); } break; case 4:switch(day) { case 1:printf("愚人节");break; case 22:printf("地球日");break; default:printf("不是阳历节日"); } break; case 5:switch(day) { case 1:printf("劳动节");break; case 4:printf("青年节");break; default:printf("不是阳历节日"); } break; case 6:switch(day) { case 1:printf("儿童节");break; default:printf("不是阳历节日"); } break; case 7:switch(day) { case 1:printf("建党节");break; case 26:printf("火把节");break; default:printf("不是阳历节日"); } break; case 8:switch(day) { case 1:printf("建军节");break; default:printf("不是阳历节日"); } break; case 9:switch(day) { case 10:printf("教师节");break; default:printf("不是阳历节日"); } break; case 10:switch(day) { case 1:printf("国庆节");break; case 31:printf("万圣夜");break; default:printf("不是阳历节日"); } break; case 11:switch(day) { case 11:printf("光棍节");break; default:printf("不是阳历节日"); } break; case 12:switch(day) { case 24:printf("平安夜");break; case 25:printf("圣诞节");break; default:printf("不是阳历节日"); } break; } printf("\n"); printf("%d年%d月%d号是:",year,month,day); switch(week) { case 0:printf("Sunday");break; case 1:printf("Monday");break; case 2:printf("Tuesday");break; case 3:printf("Wednesday");break; case 4:printf("Thursday");break; case 5:printf("Friday");break; case 6:printf("Saturday");break; } printf("\n"); printf("距离今天还有%d天\n",sumdays(year,month,day)); printf("\n"); } /********************************主函数**************************/ int main() { int select; printf("========================================日历菜单========================================\n"); printf("\n"); printf("-------------------------------------请输入您的选择-------------------------------------\n"); printf("\n"); printf("------------------------------------------功能:----------------------------------------\n"); printf("\n"); printf("***********************************1:查找某年的日历*************************************\n"); printf("\n"); printf("***********************************2:查找某月的日历*************************************\n"); printf("\n"); printf("***********************************3:查找某日的日历*************************************\n"); printf("\n"); printf("***********************************0:退出***********************************************\n"); printf("\n"); printf("========================================================================================\n"); printf("\n"); while(1) { printf("请输入您的选择\n"); scanf("%d",&select); switch (select) { case 1: print_year(year); break; case 2: print_year_month(); break; case 3: current_time(); print(year,month,day); sumdays(year,month,day); break; case 0: break; } } }