#include <stdio.h>
#include <windows.h>
int integer_detection(char ch)
//判断字符是否为数字
{
if(ch>='0'&&ch<='9')
return 1;
else
return 0;
}
int Date_input_detection(char date[])
//判断日期格式
{
int i=0;
if(date[4]=='-')
{
if(date[7]=='-')
{
while(i<10)
{
if(i==4||i==7)
{
i++;
continue;
}
if(i==9)
{
if(date[i]=='\0')
{
//******************
if(date[5]!='0'&&date[5]!='1')
return 0;
if(date[5]=='1')
{
if(date[6]>'2')
return 0;
}
//*****************
//以上为判断月份是否超过12,若超过12则为输入错误
return 1;
}
}
if(integer_detection(date[i])==1)
i++;
else
return 0;
}
//****************************************
if(date[5]!='0'&&date[5]!='1')
return 0;
if(date[5]=='1')
{
if(date[6]>'2')
return 0;
}
if(date[8]!='2'&&date[8]!='1'&&date[8]!='3'&&date[8]!=0)
return 0;
if(date[8]=='3')
{
if(date[9]!='1'&&date[9]!='0')
return 0;
}
if(date[5]=='0')
{
if(date[6]=='2')
{
if(date[8]>'2')
return 0;
}
}
//***********************************
//以上为判断日期是否超出正常值
return 1;
}
else if(date[6]=='-')
{
while(i<9)
{
if(i==4||i==6)
{
i++;
continue;
}
if(i==8)
{
if(date[i]=='\0')
{
return 1;
}
}
if(integer_detection(date[i])==1)
i++;
else
return 0;
}
if(date[5]=='2')
{
if(date[7]>'2')
return 0;
}
return 1;
}
}
}
void translation_date(char date[],int *year,int *month,int *day)
//翻译日期
{
int tmp=0;
tmp=tmp+(date[0]-'0')*1000;
tmp=tmp+(date[1]-'0')*100;
tmp=tmp+(date[2]-'0')*10;
tmp=tmp+(date[3]-'0');
*year=tmp;
tmp=0;
if(date[6]=='-')
{
tmp=date[5]-'0';
*month=tmp;
tmp=0;
if(date[9]=='\0')
{
tmp=tmp+(date[7]-'0')*10;
tmp=tmp+(date[8]-'0');
*day=tmp;
tmp=0;
}
else if(date[8]=='\0')
{
tmp=tmp+(date[7]-'0');
*day=tmp;
tmp=0;
}
}
else if(date[7]=='-')
{
tmp=tmp+(date[5]-'0')*10;
tmp=tmp+(date[6]-'0');
*month=tmp;
tmp=0;
if(date[9]=='\0')
{
tmp=tmp+(date[8]-'0');
*day=tmp;
tmp=0;
}
else if(date[10]=='\0')
{
tmp=tmp+(date[8]-'0')*10;
tmp=tmp+(date[9]-'0');
*day=tmp;
tmp=0;
}
}
}
int determine_leap_year(int year)
//判断是否闰年
{
if(year%100==0)
{
if(year%400==0)
return 1;
}
else if(year%4==0)
{
return 1;
}
else
{
return 0;
}
}
int Number_of_days_in_February(int run)
//返回二月份天数
{
switch(run)
{
case 0:
{
return 28;
}
case 1:
{
return 29;
}
}
}
int Week_translation(int year,int month,int day)
{
int parameter1=0;
int parameter2=0;
int parameter3=0;
int parameter4=0;
int parameter5=0;
int parameter6=0;
int month1=0,year1=0,day1=0;
int week=0;
if(month!=1&&month!=2)
{
month1=month;
year1=year;
}
else if(month==1)
{
month1=13;
year1=year-1;
}
else if(month==2)
{
month1=14;
year1=year-1;
}
parameter1=year1/100/4;
parameter2=year1/100*2;
parameter3=year1%100;
parameter4=parameter3/4;
parameter5=13*(month1+1)/5;
parameter6=day-1;
week=(parameter1-parameter2+parameter3+parameter4+parameter5+parameter6)%7;
return week;
}
void put_week(int week,char date[])
{
switch(week)
{
case 0:
{
printf("%s是星期天\n",date);
break;
}
case 1:
{
printf("%s是星期一\n",date);
break;
}
case 2:
{
printf("%s是星期二\n",date);
break;
}
case 3:
{
printf("%s是星期三\n",date);
break;
}
case 4:
{
printf("%s是星期四\n",date);
break;
}
case 5:
{
printf("%s是星期五\n",date);
break;
}
case 6:
{
printf("%s是星期六\n",date);
break;
}
}
}
void calendar_year()
//查询年历
{
int year;
int week;
int tmp1=0,run;
int i=0,j=0;
int February;
printf("请输入要查找的年份(如:2012):");
scanf("%d",&year);
run=determine_leap_year(year);
February=Number_of_days_in_February(run);
while(tmp1<12)
{
printf(" %d月\n",tmp1+1);
printf(" ");
for(j=0;j<7;j++)
{
switch(j)
{
case 0:
{
printf("日 ");
break;
}
case 1:
{
printf("一 ");
break;
}
case 2:
{
printf("二 ");
break;
}
case 3:
{
printf("三 ");
break;
}
case 4:
{
printf("四 ");
break;
}
case 5:
{
printf("五 ");
break;
}
case 6:
{
printf("六 ");
break;
}
}
}
i=0;
j=0;
printf("\n");
if(tmp1==0)
week=Week_translation(year,1,1);
for(j=0;j<week;j++)
{
printf(" ");
}
switch(tmp1)
{
case 0:j=31;break;
case 1:j=February;break;
case 2:j=31;break;
case 3:j=30;break;
case 4:j=31;break;
case 5:j=30;break;
case 6:j=31;break;
case 7:j=31;break;
case 8:j=30;break;
case 9:j=31;break;
case 10:j=30;break;
case 11:j=31;break;
}
i=0;
while(i<j)
{
printf("%4d",i+1);
week++;
if(week%7==0)
printf("\n");
i++;
}
week=week%7;
printf("\n");
tmp1++;
}
}
int main()
{
int year,month,day;
int tmp=0;
int run=0;
int February;
int week;
char date[10];
int ch;
do{
do{
printf("==================欢迎使用日历查询系统===============\n");
printf(" 1.日期查询星期几 2.查询年历\n");
printf(" 0.退出\n");
printf(" 请输入命令:");
scanf("%d",&ch);
fflush(stdin);
}while(ch!=1&&ch!=2&&ch!=0);
tmp=0;
switch(ch)
{
case 1:
{
do{
if(tmp)
{
printf("输入错误,请重新输入日期!\n");
}
printf("请输入一个日期(如:1990-01-01):");
gets(date);
tmp++;
}while(Date_input_detection(date)==0);
tmp=0;
translation_date(date,&year,&month,&day);
run=determine_leap_year(year);
February=Number_of_days_in_February(run);
tmp=0;
switch(month)
{
case 12:tmp=tmp+30;
case 11:tmp=tmp+31;
case 10:tmp=tmp+30;
case 9:tmp=tmp+31;
case 8:tmp=tmp+31;
case 7:tmp=tmp+30;
case 6:tmp=tmp+31;
case 5:tmp=tmp+30;
case 4:tmp=tmp+31;
case 3:tmp=tmp+February;
case 2:tmp=tmp+31;
case 1:tmp=tmp+day;
}
printf("\n%s是%d年的第%d天\n",date,year,tmp);
week=Week_translation(year,month,day);
put_week(week,date);
system("pause");
system("cls");
break;
}
case 2:
{
calendar_year();
system("pause");
system("cls");
break;
}
case 0:break;
}
}while(ch!=0);
}