#include
<stdio.h>
#include <stdlib.h>
//*****输入年或年月或年月日查日历、查星期*****
//根据输入的年月日查询星期几
int weekDay(int y,int m,int d)
{
int k;
//1、2月转换为13、14月,年数减一
if((m == 1) || (m == 2))
{
m += 12;
y--;
}
//蔡勒公司得到星期数 d是指当月的第几天
k = ((y-1)+ (y-1)/4+ (y-1)/400- (y-1)/100 +13*(m+1)/5 +d)%7;
/*switch(k)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
k += 1;
break;
case 6:
k = 0;
break;
}*/
return k;
}
void print_title()
{
printf(" SUM MON TES WEN THU FRI STA\n");
printf("***********************************\n");
}
//判断闰年
int leapYear(int year)
{
int leap;
if((year%4 == 0)&&(year%100 != 0))
{
leap = 1;
}
else if(year%400 == 0)
{
leap = 1;
}
else
{
leap = 0;
}
return leap;
}
void print_leap(int year)
{
if(leapYear(year) == 1)
{
printf("\n\t%d is leap year\n",year);
}
else
{
printf("\n\t%d is not leap year\n",year);
}
}
//获得当月的天数
int monthDay(int month)
{
int countDay;
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
countDay = 31;
break;
case 2:
countDay = 28;
break;
case 4:
case 6:
case 9:
case 11:
countDay = 30;
break;
default :
break;
}
return countDay;
}
void menu()
{
printf("\n\t\t calendar for you \n");
printf("\t\t search weekday or date\n\n");
printf("===================================\n\n");
printf("1.input year format like:2008 \n ");
printf("2.input year and month format like:200810 \n");
printf("3.input year,month and day format like:20081010 \n\n");
printf("===================================\n");
}
//如果输入年月格式将当月的日历打印出来
void print_month(long y_m_d,int month)
{
int i,j,day1,h;
//获得当月天数
int c = monthDay(month);
//如果是闰年又是2月,加1
if((1 == leapYear(y_m_d)) &&(c == 28))
{
c += 1;
}
//确定当月的第一天是星期几,打印一个月格式时用
day1 = weekDay(y_m_d,month,1);
print_title();
//将前面的空出
for(i = 0; i < day1*5; i++)
{
printf(" ");
}
for(i = 1; i <= c; i++)
{
printf("%5d",i);
//后移一位
day1++;
if(day1%7 == 0)
{
printf("\n");
}
if((i == c) && (day1%7 != 0))
{
printf("\n");
}
}
}
void print_week(int k)
{
switch(k)
{
case 0:
printf("sunday\n");
break;
case 1:
printf("monday\n");
break;
case 2:
printf("tuesday\n");
break;
case 3:
printf("wednesday\n");
break;
case 4:
printf("thursday\n");
break;
case 5:
printf("friday\n");
break;
case 6:
printf("saturday\n");
break;
default:
break;
}
}
int main()
{
int y,m,d;
int k,h,s,x;
long y_m_d;
menu();
printf("please input the year or year-month or year-month-day you want to search\n");
scanf("%ld",&y_m_d);
//输入的是四位数
if(y_m_d/10000 == 0 && y_m_d != 0)
{
for(k = 0; k <= 12; k++)
{
printf("\n\t\t%d month\n\n",k);
print_month(y_m_d,k);
printf("====================================\n");
}
print_leap(y_m_d);
}
//输入的是六位数
else if(y_m_d/1000000 == 0&&y_m_d/100 != 0&&y_m_d%100 != 0)
{
y = y_m_d/100;
m = y_m_d%100;
if(m > 0 && m < 13)
{
print_month(y,m);
print_leap(y);
}
else
{
printf("format wrong! input again...\n");
}
}
else if(y_m_d/100000000 == 0 &&y_m_d != 0)
{
y = y_m_d/10000;
m = y_m_d%10000/100;
d = y_m_d%100;
h = leapYear(y);
s = monthDay(m);
if(h == 1&&s == 28)
{
s += 1;
}
if((d > 0&&d < s)&&(m < 13 &&m > 0))
{
x = weekDay(y,m,d);
print_week(x);
print_leap(y);
}
else
{
printf("format wrong ...input again!\n");
}
}
else
{
printf("format wrong....input again!\n");
}
// getch();
return 0;
}