万年历(C语言代码实现)

学了一个学期的C,由于C语言基础不扎实,所以重新学习了一次。参考了一部分网上代码,按照框架思路自己再编写了程序代码。只是简单万年历功能的实现。一些不足之处,还请多多包涵~~


#include 
#include  

enum weekday { Sun, Mon, Jue, Wed, Thu, Fri, Sat };
enum month { January = 1, February, Match, Apirl, May, June, July, 
		August, September, October, November, December };

void color(int a)  //颜色函数 
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}

void printMonthHeader(void)  //输出日历每个月份的表头
{
	printf("周日周一周二周三周四  周五  周六\n");
}

int judgeYear(int Y)  //判断平年闰年,若为平年则为 0,闰年为 1
{
	enum differentYear{ nonleapYear, leapYear };
	return ( Y % ( Y % 100 ? 4:400) ? nonleapYear : leapYear);
}

void printMonth( int firstDay, int days )  //输出每个月份的日历表,firstDay是每个月的第一天,days是一个月的总天数
{
	int position = firstDay;
	int i;

	printMonthHeader();

	for( i = 0; i < position; ++i )
	{
		printf("");
	}

	for( i = 1; i <= days; ++i )
	{
		position = (++position) % 7;
		printf("%-7d", i);
		if(position == 0)
		printf("\n");
	}
	printf("\n");
}

int firstDayOfYear(const int getYear)  //计算一年中第一天是周几 
{
	int weekDay;
	weekDay = ( (getYear - 1) + (getYear - 1) / 4 - (getYear / 100) + (getYear / 400) + 1 ) % 7;
	return weekDay;
}


void printfYear(const int year)
{
	weekday yDay = (enum weekday)(firstDayOfYear(year));//计算一年的第一天是周几
	int aDay = judgeYear(year);//判断平年闰年,若为平年则为0,闰年为1
	int monDays[13];//每个月的天数
	int monFirstday[13];//计算每个月的第一天是周几

	monDays[January] = 31;
	monDays[February]=28+aDay;
	monDays[Match]=31;  
	monDays[Apirl]=30;
	monDays[May]=31;
	monDays[June]=30;  
	monDays[July]=31;
	monDays[August]=31;
	monDays[September]=30;  
	monDays[October]=31;
	monDays[November]=30;
	monDays[December]=31;  

	monFirstday[0]=0;  
	monFirstday[January]   = yDay;  
	monFirstday[February]  = ((monFirstday[January]    +monDays[January]    )%7);  
	monFirstday[Match]     = ((monFirstday[February]   +monDays[February]   )%7);  
	monFirstday[Apirl]     = ((monFirstday[Match]      +monDays[Match]      )%7);  
	monFirstday[May]       = ((monFirstday[Apirl]      +monDays[Apirl]      )%7);  
	monFirstday[June]      = ((monFirstday[May]        +monDays[May]        )%7);  
	monFirstday[July]      = ((monFirstday[June]       +monDays[June]       )%7);  
	monFirstday[August]    = ((monFirstday[July]       +monDays[July]       )%7);  
	monFirstday[September] = ((monFirstday[August]     +monDays[August]     )%7);  
	monFirstday[October]   = ((monFirstday[September]  +monDays[September]  )%7);  
	monFirstday[November]  = ((monFirstday[October]    +monDays[October]    )%7);  
	monFirstday[December]  = ((monFirstday[November]   +monDays[November]   )%7); 

	int j;
	for( j = January; j <= December; ++j )
	{
		printf("\n*******************************************************************\n\n");
		printf("                   %d     %d                         \n\n", year, j);
		printMonth(monFirstday[j], monDays[j]);
		printf("\n");
	}
	return;
}

int main()
{
	color(11);

	int quit = 0;
	int year = 0;
	do
	{
		printf("请输入您想查询的年份(选择0退出):     ");
		scanf("%d", &year);
		printf("\n");

		if( year == 0 )
			quit = 1;
		else
		printfYear(year);
	}while(!quit);

	return 0;
}

//大概每四年一个闰年,所以可计算:
//  公元一年到公元 year-1 年内的闰年个数 = (year - 1) / 4
//但是每一百年要扣掉一个闰年,所以修正后:
//  公元一年到公元 year-1 年内的闰年个数 = (year - 1) / 4 - (year / 100) 
//其实每四百年又要增加一个闰年,再次修正:
//  公元一年到公元 year-1 年内的闰年个数 = (year - 1) / 4 - (year / 100) + (year / 400)
//一年的第一天距离起始天的差是:
//  (year - 1)*365 + (公元一年到公元year-1年内的闰年个数(每个闰年多一天))
//故得到
//  (year - 1)*365 + (year - 1) / 4 - (year / 100) + (year / 400)
//这天是周几
//  [(year - 1)*365 + (year - 1) / 4 - (year / 100) + (year / 400)] % 7 + 1
//  = [(year - 1)*365 + (year - 1) / 4 - (year / 100) + (year / 400) + 1] % 7 
//  = [(year - 1)*364 + (year - 1) + (year - 1) / 4 - (year / 100) + (year / 400) + 1] % 7
//  = [ (year - 1) + (year - 1) / 4 - (year / 100) + (year / 400) + 1 ] % 7 

  

 


你可能感兴趣的:(万年历(C语言代码实现))