您当前的位置:首页 > 计算机 > 编程开发 > VC/VC++

年历显示(控制台)

时间:09-15来源:作者:点击数:
城东书院 www.cdsy.xyz

年历显示(控制台)

项目显示

完整源码

#include "stdafx.h"
#include <time.h>
#include <CMATH>

long int f(int year, int month)
{
	if (month < 3) return year - 1;
	else return year;
}
long int g(int month)
{
	if (month < 3) return month + 13;
	else return month + 1;
}

long int n(int year, int month, int day)
{
	return 1461L * f(year, month) / 4 + 153L * g(month) / 5 + day;
}

int w(int year, int month, int day)
{
	return(int)((n(year, month, day) % 7 - 621049L % 7 + 7) % 7);
}

int date[12][6][7];
int day_tbl[][12] = { {31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31} };
void days();
void fish();

int main(int argc, char* argv[])
{
	printf("***************欢迎来到年历显示*****************\n");
	printf("*****1、输入年份,显示该年的日历*****************\n");
	printf("*****2、输入年月,显示该月的日历*****************\n");
	printf("*****3、输入年月日,显示距今天多少天,是星期几,是否公历节日\n");
	printf("*****4、输入日期,显示这一天是打鱼还是晒网\n");
	printf("请输入您的选择:\n");
	int choice;
	scanf("%d", &choice);
	if (choice == 1)
	{
		//输入年份输出日历
		int sw, leap, i, j, k, wd, day;
		int year;
		char title[] = "SUM MON TUE WED THU FRI SAT";
		printf("请输入你想知道的日历年份:\n");
		scanf("%d", &year);
		sw = w(year, 1, 1);
		leap = year % 4 == 0 && year % 100 || year % 400 == 0;
		for (i = 0; i < 12; i++)
			for (j = 0; j < 6; j++)
				for (k = 0; k < 7; k++)
					date[i][j][k] = 0;
		for (i = 0; i < 12; i++)
			for (wd = 0, day = 1; day <= day_tbl[leap][i]; day++)
			{
				date[i][wd][sw] = day;
				sw = ++sw % 7;
				if (sw == 0) wd++;
			}
		printf("\n|================== The Calendar of Year %d==================|\n|", year);
		for (i = 0; i < 6; i++)
		{
			for (wd = 0, k = 0; k < 7; k++)
				wd += date[i][5][k] + date[i + 6][5][k];
			wd = wd ? 6 : 5;
			printf(" %2d %s %2d %s|\n|", i + 1, title, i + 7, title);
			for (j = 0; j < wd; j++)
			{
				printf("   ");
				for (k = 0; k < 7; k++)
					if (date[i][j][k])
						printf("%4d", date[i][j][k]);
					else printf("    ");
				printf("   ");
				for (k = 0; k < 7; k++)
					if (date[i + 6][j][k])
						printf("%4d", date[i + 6][j][k]);
					else printf("    ");
				printf("|\n|");
			}
		}
		puts("==============================================================|");
	}
	else if (choice == 2)
	{
		//输入年月 输出当月日历
		int m, d, monthmax, y;
		while (1)
		{
			printf("请输入年份:\n");
			scanf("%d",&y);
			printf("请输入月份:\n");
			scanf("%d",&m);
			//判断年份和月份
			if (y % 4 == 0 || (y % 100 == 0 && y % 400 == 0))//闰年
			{
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
					monthmax = 31;
				if (m == 2)
					monthmax = 29;
				if (m == 4 || m == 6 || m == 9 || m == 11)
					monthmax = 30;
			}
			if (y % 4 != 0)//平年
			{
				if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
					monthmax = 31;
				if (m == 2)
					monthmax = 28;
				if (m == 4 || m == 6 || m == 9 || m == 11)
					monthmax = 30;
			}

			//基姆拉尔森计算公式取得一号周几
			if (m == 1 || m == 2)
			{
				m += 12;    //一月算做上一年的十三月,二月算做上一年的十四月
				y--;
			}
			d = (2 + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;//算出这月一号周几
			if (d == 0)
			{
				d = 7;
			}
			//输出日历
			{
				{
					//标题模块
					if (m == 13)
					{
						printf("   公元%d年 第1月日历\n",y+1);
					}
					if (m == 14)
					{
						printf("   公元%d年 第2月日历\n",y+1);
					}
					if (m != 13 && m != 14)
					{
						printf("   公元%d年 第%d月日历\n",y,m);
					}
				}
				printf(" 1   2   3   4   5   6   7   \n");
				printf("===========================\n");
				for (int i = 1; i <= d - 1; i++)
				{
					printf("    ");
				}
				for (int j = 1; j <= monthmax; j++)
				{
					if (j < 10)
					{
						printf(" %d  ",j);
					}
					if (j >= 10)
					{
						printf(" %d ",j);
					}
					if ((j + d - 1) % 7 == 0)
					{
						printf("\n");
					}
					if (j == monthmax)
					{
						printf("\n");
					}
				}
				printf("\n");
			}
		}
	}
	else if (choice == 3)
	{
		days();
	}
	else if (choice == 4)
	{
		fish();
	}
	else
	{
		printf("输入选择错误!");
	}
	return 0;
}

void days()
{
	struct tm* loca_t;
	time_t ti;
	ti = time(NULL);
	loca_t = localtime(&ti);
	int s_year, s_month, s_day, s_sum = 0;
	static int days = 0;
	static int day = 0;
	int e_year, e_month, e_day, e_sum = 0;
	static int leap = 0;
	int tmp, tmp1, tmp2;

	s_year = loca_t->tm_year + 1900;
	s_month = loca_t->tm_mon + 1;
	s_day = loca_t->tm_mday;

	printf("输入年:\n");
	scanf("%d", &e_year);
	printf("输入月:\n");
	scanf("%d", &e_month);
	printf("输入日:\n");
	scanf("%d", &e_day);

	if (s_year > e_year) //判断哪个年份大,如果起始时间大于结束时间,则互换值
	{
		tmp = s_year;
		s_year = e_year;
		e_year = tmp;
		tmp1 = s_month;
		s_month = e_month;
		e_month = tmp1;
		tmp2 = s_day;
		s_day = e_day;
		e_day = tmp2;
	}
	while (s_year < e_year)
	{
		if (e_year % 400 == 0 || (e_year % 4 == 0 && e_year % 100 != 0)) /*判断是否为闰年*/
		{
			days = days + 366; /*闰年*/
		}
		else
		{
			days = days + 365; /*平年*/
		}
		e_year--;
	}
	switch (s_month)
	{
	case 1:s_sum = 0;
		break;
	case 2:s_sum = 31;
		break;
	case 3:s_sum = 31 + 28;
		break;
	case 4:s_sum = 31 + 28 + 31;
		break;
	case 5:s_sum = 31 + 28 + 31 + 30;
		break;
	case 6:s_sum = 31 + 28 + 31 + 30 + 31;
		break;
	case 7:s_sum = 31 + 28 + 31 + 30 + 31 + 30;
		break;
	case 8:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31;
		break;
	case 9:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
		break;
	case 10:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
		break;
	case 11:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
		break;
	case 12:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
		break;
	}

	s_sum = s_sum + s_day; /*计算起始日期天数*/

	switch (e_month)
	{
	case 1:e_sum = 0;
		break;
	case 2:e_sum = 31;
		break;
	case 3:e_sum = 31 + 28;
		break;
	case 4:e_sum = 31 + 28 + 31;
		break;
	case 5:e_sum = 31 + 28 + 31 + 30;
		break;
	case 6:e_sum = 31 + 28 + 31 + 30 + 31;
		break;
	case 7:e_sum = 31 + 28 + 31 + 30 + 31 + 30;
		break;
	case 8:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31;
		break;
	case 9:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
		break;
	case 10:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
		break;
	case 11:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
		break;
	case 12:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
		break;
	}

	if (s_year % 400 == 0 || (s_year % 4 == 0 && s_year % 100 != 0))
	{
		if (e_month > 2)
		{
			day++;
		}
	}
	e_sum = e_sum + e_day; /*计算结束日期天数*/
	days = days - s_sum + e_sum + day; /*相差的天数*/

	printf("输入的日期距今天还有%d天\n", abs(days)); //因天数计算可能会出现负数,abs()函数可把相差的天数变成正数

	int week = 0;
	if (e_month == 1 || e_month == 2)
	{
		e_month += 12;
		e_year--;
	}
	week = (day + 2 * e_month + 3 * (e_month + 1) / 5 + e_year + e_year / 4 - e_year / 100 + e_year / 400) % 7;
	printf("输入的日期是星期%d\n", week);
	printf("输入的日期是公历\n");

}

void fish()
{
	int s_year, s_month, s_day, s_sum = 0;
	static int days = 0;
	static int day = 0;
	int e_year, e_month, e_day, e_sum = 0;
	static int leap = 0;
	int tmp, tmp1, tmp2;

	s_year = 1990;
	s_month = 1;
	s_day = 1;

	printf("输入年:\n");
	scanf("%d", &e_year);
	printf("输入月:\n");
	scanf("%d", &e_month);
	printf("输入日:\n");
	scanf("%d", &e_day);

	if (s_year > e_year) //判断哪个年份大,如果起始时间大于结束时间,则互换值
	{
		tmp = s_year;
		s_year = e_year;
		e_year = tmp;
		tmp1 = s_month;
		s_month = e_month;
		e_month = tmp1;
		tmp2 = s_day;
		s_day = e_day;
		e_day = tmp2;
	}
	while (s_year < e_year)
	{
		if (e_year % 400 == 0 || (e_year % 4 == 0 && e_year % 100 != 0)) /*判断是否为闰年*/
		{
			days = days + 366; /*闰年*/
		}
		else
		{
			days = days + 365; /*平年*/
		}
		e_year--;
	}
	switch (s_month)
	{
	case 1:s_sum = 0;
		break;
	case 2:s_sum = 31;
		break;
	case 3:s_sum = 31 + 28;
		break;
	case 4:s_sum = 31 + 28 + 31;
		break;
	case 5:s_sum = 31 + 28 + 31 + 30;
		break;
	case 6:s_sum = 31 + 28 + 31 + 30 + 31;
		break;
	case 7:s_sum = 31 + 28 + 31 + 30 + 31 + 30;
		break;
	case 8:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31;
		break;
	case 9:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
		break;
	case 10:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
		break;
	case 11:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
		break;
	case 12:s_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
		break;
	}

	s_sum = s_sum + s_day; /*计算起始日期天数*/

	switch (e_month)
	{
	case 1:e_sum = 0;
		break;
	case 2:e_sum = 31;
		break;
	case 3:e_sum = 31 + 28;
		break;
	case 4:e_sum = 31 + 28 + 31;
		break;
	case 5:e_sum = 31 + 28 + 31 + 30;
		break;
	case 6:e_sum = 31 + 28 + 31 + 30 + 31;
		break;
	case 7:e_sum = 31 + 28 + 31 + 30 + 31 + 30;
		break;
	case 8:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31;
		break;
	case 9:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
		break;
	case 10:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
		break;
	case 11:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
		break;
	case 12:e_sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
		break;
	}

	if (s_year % 400 == 0 || (s_year % 4 == 0 && s_year % 100 != 0))
	{
		if (e_month > 2)
		{
			day++;
		}
	}
	e_sum = e_sum + e_day; /*计算结束日期天数*/
	days = days - s_sum + e_sum + day; /*相差的天数*/
	if ((abs(days)) % 5 <= 3)
	{
		printf("这一天是打鱼\n");
	}
	else
	{
		printf("这一天是晒网\n");
	}
	
}

 

城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐