#include <stdio.h>
#include <ctype.h>
#define SYEAR 1900
#define LMonth 31
#define SMonth 30
#define VMonth 28
typedef enum {
Sun,
Mon,
Jue,
Wed,
Thu,
Fri,
Sat
} WEEK;
typedef enum {
January = 1,
February,
Match,
Apirl,
May,
June,
July,
August,
September,
October,
November,
December
} MONTH;
void
printMonHead(void){
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
return;
}
void
printMonth(const WEEK firstDay, unsigned long length ){
WEEK weekDay = firstDay % 7;
unsigned long i;
printMonHead();
for (i=0; i<weekDay;++i)
printf(" ");
for(i=1;i<=length;++i) {
weekDay = (++weekDay)%7;
printf("%7d", i);
if(weekDay==0) printf("\n");
}
printf("\n");
return;
}
unsigned long
isLeapYear(const unsigned long yr){
return ( yr%( yr%100 ? 4:400) ? 0 : 1);
}
unsigned long
getYearDay(const unsigned long year) {
unsigned long lastYear = year-1;
unsigned long yearNum = lastYear-1899;
unsigned long walker,counter=0;
for(walker=SYEAR; walker < year; ++walker) {
if(isLeapYear(walker)==1)
++counter;
}
return (365*yearNum+counter+1)%7;
}
void
printYear(const unsigned long y, const unsigned long m) {
WEEK yDay = getYearDay(y);
unsigned long vDay = isLeapYear(y);
unsigned char* monName [13] = {0,
"January", "February", "Match", "Apirl",
"May", "June", "July", "August",
"September","October", "November", "December"
};
unsigned long monLen [13];
monLen[0]=0;
monLen[January]=31; monLen[February]=28+vDay; monLen[Match]=31;
monLen[Apirl]=30; monLen[May]=31; monLen[June]=30;
monLen[July]=31; monLen[August]=31; monLen[September]=30;
monLen[October]=31; monLen[November]=30; monLen[December]=31;
unsigned long monWeeks[13];
monWeeks[0]=0;
monWeeks[January] = yDay;
monWeeks[February] = ((monWeeks[January] +monLen[January] )%7);
monWeeks[Match] = ((monWeeks[February] +monLen[February] )%7);
monWeeks[Apirl] = ((monWeeks[Match] +monLen[Match] )%7);
monWeeks[May] = ((monWeeks[Apirl] +monLen[Apirl] )%7);
monWeeks[June] = ((monWeeks[May] +monLen[May] )%7);
monWeeks[July] = ((monWeeks[June] +monLen[June] )%7);
monWeeks[August] = ((monWeeks[July] +monLen[July] )%7);
monWeeks[September] = ((monWeeks[August] +monLen[August] )%7);
monWeeks[October] = ((monWeeks[September] +monLen[September] )%7);
monWeeks[November] = ((monWeeks[October] +monLen[October] )%7);
monWeeks[December] = ((monWeeks[November] +monLen[November] )%7);
printf("\n-------------------------------------------------\n");
printf(" %s, %d \n",monName[m],y);
printf("-------------------------------------------------\n");
printMonth(monWeeks[m],monLen[m]); printf("\n");
return;
}
int
main(void){
int yr, mon , ok;
loop:
do
{
ok=1;
fflush(stdin);
printf("\nPlease Enter Year(0 to quit): ");
scanf("%4d",&yr);
if(yr ==0)
{
return 0;
}
if(yr < SYEAR)
{
printf("\nOut Of Range! Please Enter A Year Later Than %d",SYEAR);
ok=0;
}
} while(!ok);
do
{
ok=1;
fflush(stdin);
printf("\nPlease enter Month (0 to quit): ");
scanf("%2d",&mon);
if(mon ==0) return 0;
if(mon<1 || mon >12)
{
printf("\nOut Of Range! Please Input Inside【1-12】");
ok=0;
}
} while(!ok);
printf("\n"); printYear(yr,mon);printf("\n\n");
goto loop;
system("PAUSE");
return 0;
}
|