自己写的C语言简单万年历

#include
#include

int get_year_jan_1th_week(int year);
int get_month_1th_week(int year,int month,int jan1_week);
int get_sumday_of_month(int year,int month);
void display_cal(int year);

char *month_name[]={
    "January",
    "Febrary",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
    NULL,
};
    


int main(void)
{
    int year = 0;
    printf("Please input a year (1~9999): ");
    scanf("%d",&year);
    printf("\n\n");	
    display_cal(year);
    printf("Press Enter to Exit...");
    getchar();
    getchar();
	
    return 0;

}

/*
 * F_description: display the calendar of given year
 * F_return:      void
 *
 */

void display_cal(int year)
{
	int jan1_week = get_year_jan_1th_week(year);
	int start_week = 0;
	int sumday = 0;

	for(int i_month = 1; i_month <= 12; ++i_month)
	{
		// get 1 the day of the week in this month
		start_week = get_month_1th_week(year,i_month,jan1_week);
		sumday = get_sumday_of_month(year,i_month);
		int enter_signal = start_week;                       // init enter signal
                printf("\t%d  %s\n",year,month_name[i_month-1]);
		printf("Mon  Tue  Wed  Thu  Fri  Sat  Sun\n");
		for(int i_week = 1; i_week < start_week; ++i_week)   // print space
		{
			printf("%-5c",' ');
		}

        for(int i_day = 1; i_day <= sumday; ++i_day)        // print the days
        {
            printf("%-5d",i_day);
            if(enter_signal == 7)
            {
                printf("\n");
                enter_signal = 1;
            }
            else
            {
                ++ enter_signal;
            }
        }
        
        puts("\n---------------------------------\n");
    }
}

/*
 * F_description:  calculate the given year of January 1,is the day of the week?
 * F_return     :  (int) a week
 *
 */
int get_year_jan_1th_week(int year)
{
	int sum_day = 0;
	for(int i_year = 1; i_year < year; ++i_year)
	{
		if((i_year % 4 == 0 && i_year % 100 != 0) || (i_year % 400 == 0))
		{
			sum_day += 366;
		}
		else
		{
			sum_day += 365;
		}
	}
	
	int week = ((sum_day % 7) + 6 + 2) % 7;
	if(week == 0)
	{
		return 7;
	}
	else
	{
		return week;
	}
}


/*
 * F_description: calculate the given month of 1 ,is the day of the week?
 * F_return:      (int) a week
 *
 */
int get_month_1th_week(int year,int month,int jan1_week)
{
	int sum_day = 0;
	for(int i_mon = 1; i_mon < month; ++i_mon)
	{
		sum_day += get_sumday_of_month(year,i_mon);
	}

	int week = (sum_day % 7 + jan1_week) % 7;
	if(week == 0)
	{
		return 7;
	}
	else
	{
		return week;
	}
}

/*
 * F_description: calculate the sum of days in given month
 * F_return :     (int) sum of days
 *
 */

int get_sumday_of_month(int year,int month)
{	
	switch(month)
	{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;
			break;
		case 2:
			if((year%400==0) || (year%4==0 && year%100!=0))
			{
				return 29;
			}
			else
			{
				return 28;
			}
			break;
		default:
			break;
	}
}


你可能感兴趣的:(自己写的C语言简单万年历)