#include<stdio.h>
void week()//判断星期几
{
int y,m,d;
printf("请输入年月日:(例如:2014 6 10)\n(请务必输入正确,否则请关闭程序!)\n");
scanf("%d%d%d",&y,&m,&d);
int t,e;
switch(m)
{
case 1:
e=d;
break;
case 2:
e=31+d;
break;
case 3:
e=59+d;
break;
case 4:
e=90+d;
break;
case 5:
e=120+d;
break;
case 6:
e=151+d;
break;
case 7:
e=181+d;
break;
case 8:
e=212+d;
break;
case 9:
e=243+d;
break;
case 10:
e=273+d;
break;
case 11:
e=304+d;
break;
case 12:
e=334+d;
break;
}
if(y%4==0&&y%100!=0||y%400==0)
if(m>2) ++e;
--y;
t=y+y/4-y/100+y/400+e;
t=t%7;
if(t==1)
printf("Monday\n");
else if(t==2)
printf("Tuesday\n");
else if(t==3)
printf("Wednesday\n");
else if(t==4)
printf("Thursday\n");
else if(t==5)
printf("Friday\n");
else if(t==6)
printf("Saturday\n");
else
printf("Sunday\n");
}
void year()//输出月历的函数
{
int year,i,j;
printf("请输入年份:(例如:2014)\n(请务必输入正确,否则请关闭程序!)\n");
scanf("%d",&year);
for(i=1; i<=12; i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
{
printf("%d年%d月\n",year,i);
for(j=1; j<=31; j++)
{
if(j/10==0)
printf("%d ",j);
else
printf("%d ",j);
if(j%7==0)
puts("");
}
puts("");
puts("");
}
else if(i==2&&(year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
{
printf("%d年2月\n",year);
for(j=1; j<=29; j++)
{
if(j/10==0)
printf("%d ",j);
else
printf("%d ",j);
if(j%7==0)
puts("");
}
puts("");
puts("");
}
else if(i==2&&!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
{
printf("%d年2月\n",year);
for(j=1; j<=28; j++)
{
if(j/10==0)
printf("%d ",j);
else
printf("%d ",j);
if(j%7==0)
puts("");
}
puts("");
}
else
{
printf("%d年%d月\n",year,i);
for(j=1; j<=30; j++)
{
if(j/10==0)
printf("%d ",j);
else
printf("%d ",j);
if(j%7==0)
puts("");
}
puts("");
puts("");
}
}
}
int week1(int y,int m,int d)//输出日历的辅助函数1
{
int t,e;
switch(m)
{
case 1:
e=d;
break;
case 2:
e=31+d;
break;
case 3:
e=59+d;
break;
case 4:
e=90+d;
break;
case 5:
e=120+d;
break;
case 6:
e=151+d;
break;
case 7:
e=181+d;
break;
case 8:
e=212+d;
break;
case 9:
e=243+d;
break;
case 10:
e=273+d;
break;
case 11:
e=304+d;
break;
case 12:
e=334+d;
break;
}
if(y%4==0&&y%100!=0||y%400==0)
if(m>2) ++e;
--y;
t=y+y/4-y/100+y/400+e;
t=t%7;
return t;
}
void weak2(int y,int m,int d)//输出日历的辅助函数2
{
int t;
t=week1(y,m,d);
if(d/10==0)
printf("%d ",d);
else
printf("%d",d);
if(t==1)
printf("(Mon) ");
else if(t==2)
printf("(Tue) ");
else if(t==3)
printf("(Wed) ");
else if(t==4)
printf("(Thu) ");
else if(t==5)
printf("(Fri) ");
else if(t==6)
printf("(Sat) ");
else
printf("(Sun) ");
}
void month()//输出日历的函数
{
int y,m,i,j,t;
printf("请输入年月:(例如:2014 6)\n(请务必输入正确,否则请关闭程序!)\n");
scanf("%d%d",&y,&m);
printf("%d年%d月\n",y,m);
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
for(i=1; i<=31; i++)
{
weak2(y,m,i);
if(i%7==0)
puts("");
}
puts("");
puts("");
}
else if(m==2&&(y % 4 == 0 && y % 100 != 0 || y % 400 == 0))
{
for(i=1; i<=29; i++)
{
weak2(y,m,i);
if(i%7==0)
puts("");
}
puts("");
puts("");
}
else if(m==2&&!(y % 4 == 0 && y % 100 != 0 || y % 400 == 0))
{
for(i=1; i<=28; i++)
{
weak2(y,m,i);
if(i%7==0)
puts("");
}
puts("");
}
else
{
for(i=1; i<=30; i++)
{
weak2(y,m,i);
if(i%7==0)
puts("");
}
puts("");
puts("");
}
}
void is_leap_year()//判断是否是闰年函数
{
int year;
printf("请输入年份: (例如:2014)\n(请务必输入正确,否则请关闭程序!)\n");
scanf("%d",&year);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
printf("%d年是闰年\n",year);
else
printf("%d年不是闰年\n",year);
}
int main()
{
int num;
printf("如果你想判断某一年是否为闰年 请输入:1\n如果你想判断某一天是星期几 请输入:2\n如果你想打印某一年的月历 请输入:3\n如果你想打印某一月的日历 请输入:4\n如果你想结束本程序 请输入:0\n");
while(scanf("%d",&num)&&num!=0)
{
if(num>=5||num<0)
{
printf("输入有误,请重新输入:\n");
continue;
}
if(num==1)
is_leap_year();
else if(num==2)
week();
else if(num==3)
year();
else
month();
printf("\n程序继续,请再次输入1,2,3,4或0:\n");
}
return 0;
}
(本文Yran原创,转载请标明出处)