简易的万年历查询系统(C++版本)

要求:
在这里插入图片描述

本人是在VS2017中完成该代码。
注:以下代码并非全部原创,本人在一定程度上参考了百度文库中一篇文章的代码,原址如下
https://wenku.baidu.com/view/3af97dd580eb6294dd886cf4.html
其中,双月并排输出的代码并未参考他人。

计算某年1月1号的算法借鉴于由罗建军,朱丹军,顾刚,刘路放编著的C++程序设计教程(第二版),具体步骤如下:
设year和1900年之间所差的年份为n,则n = year - 1900。
(1)非闰年每年都是52个星期多1天,因此n年多出的天数是n天。
(2)闰年每年是52个星期多2天,因此x个闰年多出的天数是2x天。故总共多出来的天数n = n + 闰年数。
(3)将多出来的天数n用7除,求其余数,即星期几:n = n % 7。
(4)再加上1900年元旦的星期序号:n = n + 1。
(5)如果n≥7则再减去7即year年元旦的星期序号(0表示星期日):n=n %7。
在不考虑历史的前提下,已知上述算法的基础上略作修改可逆推出公元元年(即公元1年)1月1号是星期一。

代码如下:

#include<iostream>
#include<iomanip>
#include<string>
#include<windows.h>
using namespace std;

const string MonName[12] = {
    "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int MonNum[12] = {
    31,28,31,30,31,30,31,31,30,31,30,31 };
void Start();                           //打印开始时的语句
void End();                             //打印退出时的语句

class Calendar
{
   
public:
	Calendar();                         //构造函数,用来进行第一次年历查询
	~Calendar() {
   }                      //析构函数
	int LeapYear(int);                  //判断该年是否为闰年
	int week();                         //求公历该年的1月1日是星期几
	int GetYear();                      //获取输入的年份
	void PrintDistance();               //打印并排双月间的间隔
	void ChangeYear(int);               //用于继续查询时改变成员year
	void GetEveryMonthWeek();           //获取该年每月1号是星期几
	void PrintWeek(int);                //打印每月表头
	void Display();                     //双月并排打印日历
	void Menu();                        //菜单操作系统
private:
	int year;                           //存放输入的年份
	int Week[12];                       //存放输入年份中每个月1号是星期几
};

int main()
{
   
	Start();
	Calendar a;
	a.Display();
	a.Menu();
	return 0;
}

void Start()
{
   
	system("color 0f");               //将字体设为亮白色
	cout << "                        ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆" << endl;
	cout << "                        ▲                                                              ▼" << endl;
	cout << "                        ▲                                                              ▼" << endl;
	cout << "                        ▲                 【欢迎进入万年历查询系统】                   ▼" << endl;
	cout << "                        ▲                                                              ▼" << endl;
	cout << "                        ▲                                                              ▼" << endl;
	cout << "                        ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆" << endl;
	cout << endl;
}

void End()
{
   
    //Sleep()函数包含在头文件windows.h中,该函数可以程序执行挂起(即停滞),参数单位为毫秒
    //特别注意在Codeblocks环境下是无法使用sleep函数的
	Sleep(700);             
	cout << endl;
	cout << "                         ================================================================" << endl;
	cout << "                         *                                                              *" << endl;
	Sleep(1200);
	cout << "                         *                 【感谢使用万年历查询系统】                   *" << endl;
	cout << "                         *                                                              *" << endl;
	Sleep(1200);
	cout << "                         *                    【期待您下一次使用】                      *" << endl;
	cout << "                         *                                                              *" << endl;
	Sleep(1200);
	cout << "                         *                          【再见】                            *" << endl;
	Sleep(1200);
	cout << "                         *                                                              *" << endl;
	cout << "                         ======================================
#include int main (void) { int year,month,j,i,day1,day2,one1,one2,w; printf("enter a year:"); scanf("%d",&year); printf("\n\n"); printf("---------------------%d---------------------\n\n",year); one1=((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400+1)%7; for(month=1;month<=12;month+=2) { printf(" ",month,year); printf(" \n",month+1,year); printf("-------------------- --------------------\n"); printf("日 一 二 三 四 五 六 日 一 二 三 四 五 六\n"); switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day1=31;break; case 4: case 6: case 9: case 11:day1=30;break; default:if(!(year%4)&&(year0)||!(year%400)) day1=29; else day1=28; } for(j=1;j<=one1;j++) { if(one1==7) break; else printf(" "); } for(i=1;i<=7-one1;i++) printf("%2d ",i); printf(" "); switch(month+1) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day2=31;break; case 4: case 6: case 9: case 11:day2=30;break; default:if(!(year%4)&&(year0)||!(year%400)) day2=29; else day2=28; } one2=(one1+day1)%7; for(j=1;j<=one2;j++) { if(one2==7) break; if(j!=7) printf(" "); } for(i=1;i<=7-one2;i++) printf("%2d ",i); printf("\n"); for(i=8-one1;i<=14-one1;i++) printf("%2d ",i); printf(" "); for(i=8-one2;i<=14-one2;i++) printf("%2d ",i); printf("\n"); for(i=15-one1;i<=21-one1;i++) printf("%2d ",i); printf(" "); for(i=15-one2;i<=21-one2;i++) printf("%2d ",i); printf("\n"); for(i=22-one1;i<=28-one1;i++) printf("%2d ",i); printf(" "); for(i=22-one2;i<=28-one2;i++) printf("%2d ",i); printf("\n"); for(i=29-one1;i<=35-one1&&i<=day1;i++) printf("%2d ",i); printf(" "); for(w=1;w<=35-day1-one1;w++) printf(" "); for(i=29-one2;i<=35-one2&&i<=day2;i++) printf("%2d ",i); printf("\n"); for(i=36-one1;i<=day1;i++) printf("%2d ",i); for(w=1;w<=35-day1-one1;w++) printf(" "); if(day1==31&&(one1==4||one1==3||one1==2||one1==1||one1==7)) printf(" "); if(day1==30&&(one1==4||one1==3||one1==2||one1==1||one1==7)) printf(" "); for(i=36-one2;i<=day2;i++) printf("%2d ",i); printf("\n-------------------- --------------------\n\n"); printf("\n"); one1=(one2+day2)%7; } printf("---------------------%d---------------------\n",year); getchar(); printf("按任意键退出"); getchar(); return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Nothing_f

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

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

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

打赏作者

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

抵扣说明:

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

余额充值