You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<stdio.h>
char* month_str[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char* week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int LeapYear(int year) /*判断这一年是否是闰年*/
{
if((year%4==0&&year%100!=0)||(year%400==0)) //这里是判断是否是闰年的
return 1; //如果是闰年就返回值1
else
return 0;//不是的话返回0
}
int month_day(int year,int month) //这个函数用来判断这年的月分有多少天的
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(LeapYear(year)&&month==2) /*判断是判断是否是闰年如果是闰年而且这个月是2月那这个月有29天*/
return 29;
else
return(mon_day[month-1]);
}
int DaySearch(int year,int month,int day) /*这个函数是计算输入的日期对应的星期*/
{
int c=0;
float s;
int m;
for(m=1;m<month;m++)
c=c+month_day(year,m); //这是计算输入的月分的累计天数
c=c+day; //计算日期在这一年中是第几天
s=year-1+(int)(year-1)/4+(int)(year-1)/100+(int)(year-1)/400-40+c; /*这是计算日期对应的星期公式,这个公式可在网上查到*/
return ((int)s%7); //与上语句同属计算日期对应的星期
}
void festival(int month,int day)
{
if(month==1)
{
if(day==1)
printf("该日是元旦节\n");
else
printf("该日不是公历节日\n");
}
else if(month==3)
{
switch(day)
{
case 8: printf("该日是妇女节\n");break;
case 12: printf("该日是植树节\n");break;
default : printf("该日不是公历节日\n");break;
}
}
else if(month==4)
{
if(day==5)
printf("该日是清明节\n");
else
printf("该日不是公历节日\n");
}
else if(month==5)
{
switch(day)
{
case 1: printf("该日是劳动节\n");break;
case 4: printf("该日是青年节\n");break;
default: printf("该日不是公历节日\n");break;
}
}
else if(month==6)
{
if(day==1)
printf("该日是儿童节\n");
else
printf("该日不是公历节日\n");
}
else if(month==7)
{
if(day==1)
printf("该日是建党节\n");
else
printf("该日不是公历节日\n");
}
else if(month==8)
{
if(day==1)
printf("该日是建军节\n");
else
printf("该日不是公历节日\n");
}
else if(month==9)
{
switch(day)
{
case 3: printf("该日是抗战胜利纪念日\n");break;
case 10: printf("该日是教师节\n");break;
default: printf("该日不是公历节日\n");break;
}
}
else if(month==10)
{
if(day==1)
printf("该日是国庆节\n");
else
printf("该日不是公历节日\n");
}
else
printf("该日不是公历节日\n");
}
int PrintAllYear(int year)/*这个函数是用来输出全年的日历*/
{
int temp;
int i,j;//i为月份j为天数
printf("\n\n%d 日历\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month_str[i-1],i); //输出月份名称
printf("Sun Mon Tue Wen Thu Fri Sat \n");
temp=DaySearch(year,i,1);//每个月第一天对应的星期
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf(" ");//在每个月的1号前留出空格而不输出0或负数
else if(j-temp<10)
printf(" %d ",j-temp);//控制第一、二行日期的格式
else
printf(" %d ",j-temp);
if(j%7==0)//换行
printf("\n");
}
}
return 0;
}
int main(void)
{
int option,da;
char ch;
int year,month,day;
printf("欢迎使用日历系统!\n");
while(1)
{
printf("\n请选择服务:\n"); //用来提示选择执行功能
printf("\n1 查找这一天是星期几"); //选择1时用来计算这一天是星期几
printf("\n2 查找这一年是否是闰年"); //计算是否这年是闰年
printf("\n3 输出全年的日历"); //输入全年的日历
printf("\n4 查询是否是公历节日");
scanf("%d",&option);
switch(option) //用来选择执行
{
case 1:
while(1)
{
printf("\n请输入年月日(XXXX,XX,XX):"); //提示输入
scanf("%d,%d,%d,%c",&year,&month,&day); //读入数据
da=DaySearch(year,month,day); //调用DaySearch()函数来计算是星期几
printf("\n%d-%d-%d is %s,你想要继续吗?(Y/N)",year,month,day,week[da]);
fflush(stdin); //刷新输入缓冲区
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 2: /*当为2时进行相应运算*/
while(1)
{
printf("\n请输入需要查找的年份?(XXXX)");
scanf("%d",&year);
if(LeapYear(year))
printf("\n%d 是闰年,你想要继续吗?(Y/N)",year);
else
printf("\n%d 不是闰年,你想要继续吗(Y/N)?",year);
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 3: /*当为3时运行相应的运算*/
while(1)
{
printf("\n请输入年份(XXXX)");
scanf("%d",&year);
PrintAllYear(year);
printf("\n你想要继续吗(Y/N)?");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
break;
case 4:/*当为4时运行相应的运算*/
while(1)
{
printf("\n请输入月日(XX,XX)");
scanf("%d,%d",&month,&day);
festival(month,day);
printf("\n你想要继续吗(Y/N)?");
fflush(stdin);
scanf("%c",&ch);
if(ch=='N'||ch=='n')
break;
}
}
}
return 0;
}