输入年份月份实现日历打印,C到C++过渡。

注意事项

笔者初学C++,代码并不是纯C++

算法介绍

设要算的年份是x,S=(x-1)+[ (x-1)/4 ]-[ (x-1) / 100 ]+[(x-1)/400]+C 。
C表示从这一年的元旦算到你要算的那一天为止(包含那一天)的日数 。
最终除的得出的S除以7后,若整除表示是星期天 若余数为1,就是星期一,以此类推。
从这个算法可以推断是外国人,但是谁我忘了…
其次蔡勒公式也是很出名的,在最后开源无函数封装的蔡锷公式写的代码。

函数封装

1.计算某年是闰年

算法:满足闰年的条件是 年数整除4并且年数整除100 是闰年 或者 年数整除400是闰年

返回值  ?1:0
	char R(int year)
	{
		return (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0;
	}

2.获取某年某月距1月1日共有多少天

特殊在2月,利用判断闰年函数计算,返回值 总天数

	int getdaycount(int year,int month)
	{
		int daycount = 31;
		if(month==4||month==6||month==9||month==11)
		{
			daycount = 30;
		}
		else if(month==2)
		{
			daycount = (R(year) ? 29 : 28);
		}
		return daycount;
	}

3.计算某年某月到1900年1月1日有多少天

以1900年1月1日是星期一为标准日,计算总天数

	int getinterval(int year,int month)
	{
		int interval = 0;//interval:间隔,间隙
		int i = 1900;
		for(;i<year;i++)//累加年份的天数
		{
			interval += R(year) ? 366 : 365;//年x日=除前年份总天数
		}
		for(i=1;i<month;i++)//累加月份的天数
		{
			interval += getdaycount(year,i);//加上当年总天数
		}
		return interval;//返回值为间隔的总天数和
	}

4.日历显示

void showcalendar(int year,int month)
	{
		int daycount = getdaycount(year,month);
		int blanks = (getinterval(year,month) + 1 ) % 7;	
		printf("==================== %d - %d ====================\n",year,month);
		printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
		int i;
		for(i=0;i<blanks;i++)
		{
			printf("\t");
		}
		for(i=1;i <= daycount;i++)
		{
			printf("%d\t",i);
			if(( i + blanks ) % 7 == 0)
			{
				printf("\n");
			}
		}
	}

C++编程实现输入年份月份打印日历功能

声明一个Cal类。

#include <iostream>
#include <cstdio>
using namespace std;
class Cal{
public:
	//计算某年是闰年,是返回1,不是返回0;
	char R(int year)
	{
		return (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0;
	}
	//计算某年某月有多少天
	int getdaycount(int year,int month)
	{
		int daycount = 31;
		if(month==4||month==6||month==9||month==11)
		{
			daycount = 30;
		}
		else if(month==2)
		{
			daycount = (R(year) ? 29 : 28);
		}
		return daycount;
	}
	//计算某年月的1号到1900年1月1日有多少天
	int getinterval(int year,int month) 	//interval:间隔,间隙
	{
		int interval = 0;
		int i = 1900;
		for(;i<year;i++)//累加年份的天数
		{
			interval += R(year) ? 366 : 365;
		}
		for(i=1;i<month;i++)//累加月份的天数
		{
			interval += getdaycount(year,i);
		}
		return interval;
	}
	//显示某年月的日历
	void showcalendar(int year,int month)
	{
		int daycount = getdaycount(year,month);
		int blanks = (getinterval(year,month) + 1 ) % 7;
		printf("==================== %d - %d ====================\n",year,month);
		printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
		int i;
		for(i=0;i<blanks;i++)
		{
			printf("\t");
		}
		for(i=1;i <= daycount;i++)
		{
			printf("%d\t",i);
			if(( i + blanks ) % 7 == 0)
			{
				printf("\n");
			}
		}
	}
	private:
		int year,month;
};

int main()
{
	int year,month;
	printf("input a year: ");
	scanf("%d",&year);
	printf("input a month: ");
	scanf("%d",&month);

	Cal p1;
	p1.showcalendar(year,month);
	return 0;
}

C语言编程:利用蔡勒公式算法实现打印日历

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int i,c,year,month,blanks;
	printf("input a year: ");
	scanf("%d",&year);
	printf("input a month: ");
	scanf("%d",&month);
	char R = year % 100 != 0 && year % 4 == 0 || year % 400 ==0;
	int day = 31;
	//计算某年的某月有多少天
	if(month==4||month==6||month==9||month==11)
	{
		day = 30;
	}
	else if(month==2)
	{
		day = (R ? 29 : 28);
	}
	//套用蔡勒公式计算某天星期几,只适合于1582年10月15日之后
	//当时的罗马教皇将恺撒大帝制订的儒略历修改成格里历,即今天使用的公历。
	c = year / 100;
	year = year % 100;
	if(month==1||month==2)
	{
		year= year - 1;
		c = year / 100;
		if(month==1)
			month=13;
		else if(month==2)
			month=14;
	}
	blanks = (year+(year/4)+(c/4)-2*c+26*(month+1)/10+1-1) % 7;
	if(blanks<0)		//结果会出现负数,再上述求和部分添加了一个7的整数倍
	{
		blanks = (year+(year/4)+(c/4)-2*c+26*(month+1)/10+1-1+70) % 7;
	}
	//display
	printf("==================== %d - %d ====================\n",year,month);
	printf("Sun\tMon\tTue\tWed\tThu\tFri\tSat\n");
	for(i=0;i<blanks;i++)
	{
		printf("\t");
	}
	for(i=1;i <= day;i++)
	{
		printf("%d\t",i);
		if((i+blanks)%7==0)
		{
			printf("\n");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/GameStrategist/article/details/106738305