C++ 万年历 查找年、月、日、星期数以及节日等信息

C++ 万年历

显示公历的万年历,输入的年份在1800-2100年之间。功能要求如下:

(1)输入一个年份,输出是在屏幕上显示该年的日历。

(2)输入年、月,输出该月的日历。

(3)输入年月日,输出距今天还有多少天,星期几,是否是为公历节日。

部分算法

程序主要分为四个部分:菜单,年历,月历,具体某天的日历信息。

年历

void nianli()     //输出一整年的年历
{
	cout << "请输入年份:";
	runnian();
	system("cls");     
	cout << "Please input the year whose calendar you want to know:" << year << endl;
	cout << endl;
	char str[100] = "===========The Calendar of Year ";
		for (int m = 0; m < 32; m++) {
			cout << str[m];
		}
	cout << (year / 1000) % 10 << (year / 100) % 10 << (year / 10) % 10 << year % 10;
	for (int m = 41; m < 52; m++) {
		cout <<'=';
	}
	cout << endl << endl;
	s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1) % 7;     //该年1月1日的星期数
	for (i = 1; i <= 12; i++)
	{
		cout << "***********************************************"<<endl;
		cout << setw(2) << i << setw(4) << "SUN" << setw(6) << "MON" << setw(6) << "TUE" << setw(6) << "WED" << setw(6) << "THU" << setw(6) << "PRI" << setw(6) << "SAT" << endl;
		for (j = 0; j < s; j++)
		{
			cout << setw(6) << " ";
		}
		for (t = 1; t <= months[i]; t++)
		{
			cout << setw(6) << t;
			s = (s + 1) % 7;
			if (s % 7 == 0)     //当打印到星期六时,换行
			{
				cout << endl;
			}
		}
		cout << endl;
	}
	cout << "***********************************************" << endl;
	cout << endl;
	fflush(stdin);
	cout << "请按4返回主菜单:";
	cin >> c;
	system("cls");
	menu();
}

月历

void yueli()//输出一个月的月历
{
	int s = 0;
	cout << "请输入年份:";
	runnian();
	cout << "请输入月份:";
	cin >> month;
	system("cls");
	cout << "请输入年份:" << year << endl;
	cout << "请输入月份:" << month << endl;
	for (i = 1; i <= month - 1; i++)
	{
		s = s + months[i];     //该年1月1日到所求日期前一天的天数
	}
	s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + 1 + s) % 7;     //所求日期的星期数
	cout << "***********************************************" << endl;
	cout << setw(2) << i << setw(4) << "SUN" << setw(6) << "MON" << setw(6) << "TUE" << setw(6) << "WED" << setw(6) << "THU" << setw(6) << "PRI" << setw(6) << "SAT" << endl;
	for (j = 0; j < s; j++)
	{
		cout << setw(6) << " ";
	}
	for (t = 1; t <= months[month]; t++)
	{
		cout << setw(6) << t;
		s = (s + 1) % 7;
		if (s % 7 == 0)
		{
			cout << endl;
		}
	}
	cout << endl;
	cout << "***********************************************" << endl;
	cout << "请按4返回主菜单:";
	cin >> c;
	system("cls");
	menu();
}

具体某一天

此处默认为2020.1.13,可自行输入。

void tianshu()   //计算天数,默认今天为2020.01.10
{
	int s = 0;
	cout << "请输入年份:";
	runnian();
	cout << "请输入月份:";
	cin >> month;
	cout << "请输入日期:";
	cin >> day;
	system("cls");
	cout << "请输入年份:" << year << endl;
	cout << "请输入月份:" << month << endl;
	cout << "请输入日期:" << day << endl;
	if (year < 2020) {
		if (month == 2) {
			sum += months[2] - day + 1;
		}
		for (int m = month + 1; m <= 12; m++) {
			if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
				sum += 31;
			}
			if (m == 3 || m == 4 || m == 6 || m == 9 || m == 10 || m == 11 || m == 1) {
				sum += 30;
			}
			if (m == 2);
			sum += months[2];
		}
		for (int m = year + 1; m < 2020; m++) {
			if ((m % 4 == 0 && m % 100 != 0) || (m % 400 == 0))     //闰年判断公式
			{
				months[2] = 29;
			}
			for (i = 1; i <= 12; i++)
			{
				sum = sum + months[i];
			}
		}
		sum += 10;
	}
	if (year > 2020) {
		for (int m = year + 1; m < 2020; m++) {
			if ((m % 4 == 0 && m % 100 != 0) || (m % 400 == 0))     //闰年判断公式
			{
				months[2] = 29;
			}
			for (i = 1; i <= 12; i++)
			{
				sum = sum + months[i];
			}
		}
		sum += 355;
	}
	if (year == 2020) {
		if (month == 1) {
			sum += fabs(day - 10);
		}
		if (month != 1) {
			for (int m = month + 1; m <= 12; m++) {
				sum += months[m];
			}
			sum += 21;
		}
	}
	for (i = 1; i <= month - 1; i++)
	{
		s = s + months[i];
	}
	s = (year - 1 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400 + day + s) % 7;
	cout << "星期数:" << s << endl;
	cout << "距离2020.01.01 " << sum << " 天";
	cout << endl;
	if ((month == 2 && day == 14) || (month == 3 && day == 8) || (month == 1 && day == 1) || (month == 3 && day == 12) || ((month == 5 || month == 6 || month == 7 || month == 8 || month == 10) && day == 1) || (month == 9 && day == 10)) {
		cout << year << "." << month << "." << day << "是公历节日" << endl;
	}
	else {
		cout << year << "." << month << "."<< day << "不是公历节日" << endl;
	}
	cout << endl;
	cout << "请按4返回主界面:";
	cin >> c;
	system("cls");
	menu();
}

主函数

int main()//主函数
{
	setlocale(LC_ALL, "chs");//转中文
	menu();
	while (number != 0||(year >= 1800 && year <= 2100))
	{
		switch (number)
		{
		case 1:
		{
			nianli();
			break;
		}
		case 2:
		{
			yueli();
			break;
		}
		case 3:
		{
			tianshu();
			break;
		}
		}
		months[2] = 28;  //把months[2]变为初值28
	}
	if (number == 0)
	{
		system("pause");//退出

	}
	return 0;
}

运行截图

菜单:

年历(2020):

月历:

具体某一天:

  • 17
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PlanetRT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值