|
|
#include<stdio.h>
|
|
|
#include<string.h>
|
|
|
#include<math.h>
|
|
|
#include<time.h>
|
|
|
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=year+1;i<current_year;i++)//例,2000,指2001-2018
|
|
|
{
|
|
|
if(judgement(i))//判断2001-2018之间的闰年
|
|
|
{
|
|
|
days=days+366;//将2001-2018之间每个闰年的天数相加
|
|
|
}
|
|
|
else days=days+365;//将2001-2018之间除闰年之外的天数相加
|
|
|
}
|
|
|
for(i=month;i<=12;i++)//计算输入年份剩余的月数
|
|
|
{
|
|
|
days1=days1+day_of_month[i-1];//将月份的天数相加
|
|
|
}
|
|
|
days2=days1+-day;//将那些年的天数和剩下月份的天数相加
|
|
|
days3=days2+days;
|
|
|
for(i=0;i<current_month-1;i++)//计算今年已经过了多少个月
|
|
|
{
|
|
|
if(judgement1)
|
|
|
{
|
|
|
day_of_month[1]=29;
|
|
|
}
|
|
|
temp_day=temp_day+day_of_month[i];//将今年过去的月份的天数相加
|
|
|
}
|
|
|
temp_day=temp_day+current_day;//将今年过去月份的天数和今年这个月的天数相加
|
|
|
sum_days=temp_day+days3;//总天数
|
|
|
}
|
|
|
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 <month-1;i++ )
|
|
|
{
|
|
|
if (judgement1)
|
|
|
{
|
|
|
day_of_month[1]=29;
|
|
|
}
|
|
|
temp_day=temp_day+day_of_month[i];
|
|
|
}
|
|
|
//当前日子是这一年的多少天
|
|
|
temp_day = temp_day+ day;
|
|
|
sum_days=temp_day+days;
|
|
|
}
|
|
|
if(year==current_year)/**********************今年***********************/
|
|
|
{
|
|
|
if(month <current_month)//假设输入月份在当前月份之前
|
|
|
{
|
|
|
for (i=month+1;i<current_month;i++)//计算中间相隔了多少个月份
|
|
|
{
|
|
|
if (judgement1)//今年是否为闰年
|
|
|
{
|
|
|
day_of_month[1]=29;
|
|
|
}
|
|
|
days=days+day_of_month[i];//计算这些月有多少天
|
|
|
}
|
|
|
sum_days=days+current_day+day_of_month[month-1]-day;//将这些月的天数和当前日期(当月过了几天)和输入日期此月还剩多少天
|
|
|
}
|
|
|
if (month>current_month)//假设输入月份在当前月份之后
|
|
|
{
|
|
|
for (i=current_month+1;i<month;i++)//将输入月份减去当前月份
|
|
|
{
|
|
|
if (judgement1)
|
|
|
{
|
|
|
day_of_month[1]=29;
|
|
|
}
|
|
|
days = days + day_of_month[i];//将间隔月份天数相加
|
|
|
}
|
|
|
sum_days=days+day+day_of_month[month-1]-current_day;//间隔月份加一个月时间加上输入月份的日期(即是输入月份以及过了的天数)再减去当前月份已经过了了天数
|
|
|
}
|
|
|
if (month==current_month)//假设输入月份在当前这个月内
|
|
|
|
|
|
sum_days=fabs(day-current_day);//距离的天数即为两者相减的绝对值
|
|
|
}
|
|
|
return sum_days ;
|
|
|
}
|
|
|
int print(int year,int month,int day)
|
|
|
{
|
|
|
int week;
|
|
|
printf("Please input the day whose calender you want to kown");
|
|
|
do
|
|
|
{
|
|
|
scanf("%d.%d.%d",&year,&month,&day);
|
|
|
if(judgement(year))
|
|
|
{
|
|
|
day_of_month[1]=29;
|
|
|
}
|
|
|
printf("\n");
|
|
|
if(day<=0||day>day_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;
|
|
|
}
|
|
|
}
|
|
|
}
|