C语言 万年历 三种版本

C语言 万年历

万年历

一、第1版:

制作一个万年历(阳历版)。程序从键盘读入年份和月份,然后输出该年该月的月历。
【实现提示】:
本问题的关键是确定所求月份的第一天是星期几。如我们想确定2009年12月1日是星期几,我们可以采用下面的方法:
(1) 求出2009年12月1日是当年的第几天,存在c里面。
(2) s=year-1+(year-1)/4-(year-1)/100+(year-1)/400 + c;
(3) num=s%7; 则num表示该天星期几,0表示星期天,1表示星期一,……

C语言 万年历 三种版本_第1张图片

另外:本问题另一个关键是如何按照上面的格式来输出。输出的时候先输出第一行,再输出第二行,……。这里的问题是如何在输出的时候进行对齐。

二、第2版:

要求:将第1版的万年历程序进行改写,定义如下函数:

  1. 判断某一年是否为闰年:int isLeapYear (int year)
  2. 求某个月有多少天:int monthDays (int year, int month)
  3. 求某一天是该年的第几天:int dayNumber (int year, int month, int day)
  4. 求某一天是星期几:int week(int year, int month, int day)

三、第3版:

要求:将之前的万年历程序v2版进行改写,对如下函数进行调整:

  1. 判断某一年是否为闰年(不变)
  2. 求某个月有多少天(去掉,用数组实现)
  3. 求某一天是该年的第几天(改写,用数组实现)
  4. 求某一天是星期几(不变)

C语言 万年历 三种版本_第2张图片

第一版参考程序

#include 

int main()
{
	int year, month, days, c=0, s, num, i, j;
	
	while(scanf("%d%d", &year, &month))
	{
		switch(month)  //累加前month-1个月
		{
		case 1: c = 0; days = 31; break;
		case 2: c = 31; days = 28; break; 
		case 3: c = 31+28; days = 31; break;
		case 4: c = 31+28+31; days = 30; break;
		case 5: c = 31+28+31+30; days = 31; break;
		case 6: c = 31+28+31+30+31; days = 30; break;
		case 7: c = 31+28+31+30+31+30; days = 31; break;
		case 8: c = 31+28+31+30+31+30+31; days = 31; break;
		case 9: c = 31+28+31+30+31+30+31+31; days = 30; break;
		case 10: c = 31+28+31+30+31+30+31+31+30; days = 31; break;
		case 11: c = 31+28+31+30+31+30+31+31+30+31; days = 30; break;
		case 12: c = 31+28+31+30+31+30+31+31+30+31+30; days = 31; break;
		}
		c++;  //加上该月的1号
		if( (year%4 == 0 && year%100 != 0) || (year%400 == 0) ) //闰年
		{
			if(month >= 3)
			{
				c++;
			}
			if(month == 2)
			{
				days = 29;
			}
		}
		
		s=year-1+(year-1)/4-(year-1)/100+(year-1)/400 + c;
		num=s%7;
		
		printf("\n日\t一\t二\t三\t四\t五\t六\n");
		for (i=1; i<=num; i++)  //跳过1号前面num个空位置
		{
			printf("\t");
		}
		
		for (j=1; j<=days; j++) //打印1号到days号
		{
			printf("%d\t", j);
			if ((j+num)%7 == 0)
			{
				printf("\n");
			}
		}
		printf("\n");
	}
	return 0;
}

第二版参考程序

#include 


int isLeapYear (int year); //判断某一年是否为闰年
int monthDays(int year, int month); //求某个月有多少天
int dayNumber(int year, int month, int day); //求某一天是该年的第几天
int week(int year, int month, int day); //求某一天是星期几

int main()
{
	int year, month, num, i, j, day;
	
	while(scanf("%d%d", &year, &month))
	{
		
		num = week(year, month, 1);
		printf("\n日\t一\t二\t三\t四\t五\t六\n");
		for (i=1; i<=num; i++)  //跳过1号前面num个空位置
		{
			printf("\t");
		}
		
		day = monthDays(year, month);
		for (j=1; j<=day; j++) //打印1号到day号
		{
			printf("%d\t", j);
			if ((j+num)%7 == 0)
			{
				printf("\n");
			}
		}
		printf("\n");
	}
	return 0;
}

//判断某一年是否为闰年
int isLeapYear (int year) 
{
	if( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//求某个月有多少天
int monthDays(int year, int month)
{
	int day;
	switch(month)  
	{
	case 1: day = 31; break;
	case 2: day = 28; break;
	case 3: day = 31; break;
	case 4: day = 30; break;
	case 5: day = 31; break;
	case 6: day = 30; break;
	case 7: day = 31; break;
	case 8: day = 31; break;
	case 9: day = 30; break;
	case 10: day = 31; break;
	case 11: day = 30; break;
	case 12: day = 31; break;
	}
	if(isLeapYear (year) && month ==2)
	{
		day = 29;
	}
	return day;
}

//求某一天是该年的第几天
int dayNumber(int year, int month, int day)
{
	int c;
	switch(month)  //累加前month-1个月
	{
	case 1: c = 0; break;
	case 2: c = 31; break; 
	case 3: c = 31+28; break;
	case 4: c = 31+28+31; break;
	case 5: c = 31+28+31+30; break;
	case 6: c = 31+28+31+30+31; break;
	case 7: c = 31+28+31+30+31+30; break;
	case 8: c = 31+28+31+30+31+30+31; break;
	case 9: c = 31+28+31+30+31+30+31+31; break;
	case 10: c = 31+28+31+30+31+30+31+31+30; break;
	case 11: c = 31+28+31+30+31+30+31+31+30+31; break;
	case 12: c = 31+28+31+30+31+30+31+31+30+31+30; break;
	}
	c += day;  
	if( isLeapYear (year) && month > 3) //闰年
	{
		c++;
	}
	return c;
}

//求某一天是星期几
int week(int year, int month, int day)
{
	int c = dayNumber(year, month, day);
	int s=year-1+(year-1)/4-(year-1)/100+(year-1)/400 + c;
	int num=s%7;
	return num;
}

第三版参考程序

#include 

int isLeapYear(int year); //判断某一年是否为闰年
int dayNumber(int year, int month, int day); //求某一天是该年的第几天
int week(int year, int month, int day); //求某一天是星期几

int main()
{
	int year, month, num, i, j;
	int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	
	while(scanf("%d%d", &year, &month))
	{
		if(isLeapYear(year))  //修正2月份的天数
		{
			days[2]=29;
		}
		else
		{
			days[2]=28;
		}
		num = week(year, month, 1);
		printf("\n日\t一\t二\t三\t四\t五\t六\n");
		for (i=1; i<=num; i++)  //跳过1号前面num个空位置
		{
			printf("\t");
		}
		
		for (j=1; j<=days[month]; j++) //打印1号到day号
		{
			printf("%d\t", j);
			if ((j+num)%7 == 0)
			{
				printf("\n");
			}
		}
		printf("\n");
	}
	return 0;
}

//判断某一年是否为闰年
int isLeapYear(int year) 
{
	if( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )
	{
		return 1;
	}
	else
	{
		return 0;
	}
}


//求某一天是该年的第几天
int dayNumber(int year, int month, int day)
{
	int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	int c=0, i;
	for(i=1; i<month; i++)
	{
		c += days[i];
	}
	c += day;  
	if( isLeapYear(year) && month > 3) //闰年
	{
		c++;
	}
	return c;
}

//求某一天是星期几
int week(int year, int month, int day)
{
	int c = dayNumber(year, month, day);
	int s=year-1+(year-1)/4-(year-1)/100+(year-1)/400 + c;
	int num=s%7;
	return num;
}

你可能感兴趣的:(C/C++,小程序,c语言,链表,程序设计,switch)