@SiberiaBear
2016-03-09T10:24:25.000000Z
字数 3011
阅读 1591
代码
#include <stdio.h>
#define FIRSTDAY_OF_BEGINYEAR (4) // the first January of 1970 is Thursday
#define BEGIN_YEAR (1970) // begin with 1970
//#define TEST
地臩ꮽ
int find_leapyear(int beginyear, int endyear);
int main(int argc, char *argv[])
{
int input_year = 0;
int leapyear = 0;
int before_full_day = 0;
int weekday_of_inputyear_firstday = 0;
int weekday_count = 0;
int yearday_count = 1;
int monthday_count = 1;
int month_count = 1;
int inputyear_leap_flag = 0; // zero is normal year, one is leap year
if( 1 == argc) {
fprintf(stderr, "BlackFriday: error: without any year input!\n");
return -1;
}
if( argc > 2) {
fprintf(stderr, "BlackFriday: error: out of parameters!\n");
fprintf(stdout, "BlackFriday: Please input only one year.\n");
return -2;
}
input_year = ((unsigned int)argv[1][0]-48)*1000 + ((unsigned int)argv[1][1]-48)*100 + ((unsigned int )argv[1][2]-48)*10 + ((unsigned int )argv[1][3]-48)*1;
if(input_year <= 1970 || input_year > 3000) {
fprintf(stderr, "BlackFriday: error: out of allowed year!\n");
fprintf(stderr, "BlackFriday: error: Please input a year within the range of 1970 to 3000.\n");
return -3;
}
fprintf(stdout, "Input year is %d\n", input_year);
if( ((input_year%4 == 0) && (input_year % 100 != 0)) || (input_year % 400 == 0)) {
inputyear_leap_flag = 1;
fprintf(stdout, "Input year leap year\n");
}
else {
inputyear_leap_flag = 0;
fprintf(stdout, "Input year normal year\n");
}
leapyear = find_leapyear(BEGIN_YEAR, input_year);
fprintf(stdout, "before input year(without input year) have %d leap year\n", leapyear);
before_full_day = leapyear * 366 + (input_year - BEGIN_YEAR - leapyear) * 365;
#ifdef TEST
fprintf(stdout, "before_full_day is %d days\n", before_full_day);
#endif
weekday_of_inputyear_firstday = (FIRSTDAY_OF_BEGINYEAR + (before_full_day % 7))%7;
#ifdef TEST
fprintf(stdout, "weekday of %s firstday is %d\n", argv[1], weekday_of_inputyear_firstday);
#endif
weekday_count = weekday_of_inputyear_firstday;
fprintf(stdout, "In the year of %d :\n", input_year);
while(month_count <= 12) // Begin a year loop
{
#ifdef TEST
fprintf(stdout, "Calendar: weekday_count: %d, monthday_count: %2d, month_count: %2d, yearday_count: %3d\n", weekday_count, monthday_count, month_count, yearday_count);
#endif
if(monthday_count == 13) {
if(weekday_count == 5)
fprintf(stdout, "BlackFriday: Month %d . Day %d\n", month_count, monthday_count);
}
weekday_count ++;
monthday_count ++;
yearday_count ++;
if(weekday_count > 6)
weekday_count = 0;
if(month_count == 1 || month_count == 3 || month_count == 5 || month_count == 7 || month_count == 8 || month_count == 10 || month_count == 12) {
if(monthday_count > 31) {
month_count ++;
monthday_count = 1;
}
}
else if(month_count == 4 || month_count == 6 || month_count == 9 || month_count == 11 ) {
if(monthday_count > 30) {
month_count ++;
monthday_count = 1;
}
}
else if(month_count == 2) {
if(monthday_count > (28 + inputyear_leap_flag) ) {
month_count ++;
monthday_count = 1;
}
}
}// End a year loop
return 0;
}
int find_leapyear(int beginyear, int endyear) {
#ifdef TEST
fprintf(stdout, "beginyear: %d, endyear: %d\n", beginyear, endyear);
#endif
int year_count = beginyear;
int leap_count = 0;
do {
#ifdef TEST
fprintf(stdout, "leap_year: %d, year_count: %d\n", leap_count, year_count);
#endif
if ((year_count % 4 == 0)&&(year_count % 100 != 0) || (year_count % 400 == 0)) {
fprintf(stdout, "%d is leap year\n", year_count);
leap_count ++;
}
year_count ++;
}while(year_count != endyear);
return leap_count;
}