本人是在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
#include
#include
#include
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 << " ================================================================" << endl;
Sleep(1200);
cout << " D" ;
Sleep(150); cout << "e"; Sleep(150); cout << "s"; Sleep(150); cout << "i"; Sleep(150); cout << "g";
Sleep(150); cout << "n"; Sleep(150); cout << "e"; Sleep(150); cout << "d"; Sleep(150); cout << " ";
Sleep(150); cout << "b"; Sleep(150); cout << "y"; Sleep(150); cout << " "; Sleep(150); cout << "L";
Sleep(150); cout << "." << endl;
}
//构造函数承载了开头部分的输入功能和检错纠错功能
Calendar::Calendar()
{
cout << " 您可以查询公元1年至公元9999年间任一年份的全年日历" << endl << endl;
cout << " 请输入您想要查询的年份:";
while (true)
{
char ch;
cin >> year;
cin.get(ch);
if (cin.good() && (year < 1 || year>9999) && ch == '\n')
{
cout << endl << " 您的输入有误,所输入的年份必须在公元1——9999年间!" << endl << endl;
cout << " 请您重新输入:";
}
else if (cin.fail() || ch != '\n')
{
cout << endl << " 您的输入有误,所输入的年份必须在公元1——9999年间!" << endl << endl;
cout << " 请您重新输入:";
cin.clear();
cin.ignore(1000, '\n');
}
else
break;
}
}
//获取输入的年份
int Calendar::GetYear()
{
return this->year;
}
//判断该年是否为闰年
int Calendar::LeapYear(int year)
{
int leap = 0;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
leap = 1;
return leap;
}
//求公历该年的1月1日是星期几
int Calendar::week()
{
int n = this->year - 1;
int num = 0; //num为year到公元元年的闰年数
for (int i = 1; i < this->year; i++)
{
if (LeapYear(i) == 1)
num++;
}
n = (n + num) % 7 + 1;
return n;
}
//打印并排双月间的间隔
void Calendar::PrintDistance()
{
cout << " ";
}
//用于继续查询时改变成员year
void Calendar::ChangeYear(int year)
{
this->year = year;
}
//获取该年每月1号是星期几
void Calendar::GetEveryMonthWeek()
{
Week[0] = week();
if (LeapYear(GetYear()))
MonNum[1] = 29;
else
MonNum[1] = 28;
for (int i = 1; i < 12; i++)
{
Week[i] = Week[i - 1] + MonNum[i - 1] % 7;
if (Week[i] > 6)
{
Week[i] = Week[i] % 7;
if (Week[i] == 0)
Week[i] = 7;
}
}
}
//打印每月表头
void Calendar::PrintWeek(int i)
{
cout << endl << " " << MonName[i - 1];
cout << " " << MonName[i] << endl;
cout << " ---------------------------------" << " ";
cout << " ---------------------------------" << endl;
cout << " SUN MON TUE WED THU FRI SET" << " ";
cout << " SUN MON TUE WED THU FRI SET" << endl;
cout << " ---------------------------------" << " ";
cout << " ---------------------------------" << endl;
}
//双月并排打印日历
void Calendar::Display()
{
int i, j, k;
int day1, day2; //day1为单月天,day2为双月天
int location[2]; //记录双月每周一的日期
GetEveryMonthWeek(); //获取该年每月1号是星期几,填充Week[12]
cout << endl << endl << " 公元" << year << "年年历如下:" << endl << endl;
for (i = 1; i < 12; i = i + 2)
{
PrintWeek(i); //打印双月表头
cout << " "; //打印日历第一行
for (j = 1; j <= Week[i - 1]; j++) //打印单月1号前的空格使之对齐
{
if (Week[i - 1] == 7)
break;
cout << " ";
}
for (day1 = 1; day1 <= 7 - Week[i - 1] % 7; day1++) //Week[]%7是针对Week[]=7的情况
{
cout << setfill(' ') << setw(5) << day1;
}
location[0] = day1;
PrintDistance();
for (k = 1; k <= Week[i]; k++) //打印双月1号前的空格使之对齐
{
if (Week[i] == 7)
break;
cout << " ";
}
for (day2 = 1; day2 <= 7 - Week[i] % 7; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == 7 - Week[i] % 7)
cout << endl;
}
location[1] = day2;
int three = 3;
while (three--) //打印日历第二三四行
{
cout << " ";
for (day1 = location[0]; day1 <= location[0] + 6; day1++)
{
cout << setfill(' ') << setw(5) << day1;
}
location[0] = day1;
PrintDistance();
for (day2 = location[1]; day2 <= location[1] + 6; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == location[1] + 6)
cout << endl;
}
location[1] = day2;
}
/*单月日历必然存在第五行
当单月日历只有五行时,下一个双月日历可能存在四行、五行、六行
当单月日历有六行时,下一个双月日历必然只存在五行*/
int n1 = MonNum[i - 1] - location[0] + 1; //n1记录从第五行第一天到单月最后一天的天数
int n2 = MonNum[i] - location[1] + 1; //n2记录从第五行第一天到双月最后一天的天数
if (n1 <= 7) //当该单月日历只有五行时
{
int m1 = 7 - n1; //m1记录该单月第五行空白的天数
cout << " ";
for (day1 = location[0]; day1 <= location[0] + n1 - 1; day1++)
{
cout << setfill(' ') << setw(5) << day1;
}
for (int l = 1; l <= m1; l++)
cout << " ";
if (n2 == 0) //在该单月日历只有五行的情况下,该双月日历只有四行时
{
cout << endl << endl << endl;
}
else if (n2 <= 7) //在该单月日历只有五行的情况下,该双月日历只有五行时
{
PrintDistance();
int m2 = 7 - n2; //m2记录该双月第五行空白的天数
for (day2 = location[1]; day2 <= location[1] + n2 - 1; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == location[1] + n2 - 1)
cout << endl << endl;
}
}
else //在该单月日历只有五行的情况下,该双月日历有六行时
{
//打印该双月第五行日历
PrintDistance();
for (day2 = location[1]; day2 <= location[1] + 6; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == location[1] + 6)
cout << endl;
}
location[1] = day2;
//打印该双月第六行日历
cout << " ";
PrintDistance();
for (day2 = location[1]; day2 <= MonNum[i]; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == MonNum[i])
cout << endl;
}
}
}
if (n1 > 7) //当该单月日历有六行时
{
//打印该单月第五行日历
cout << " ";
for (day1 = location[0]; day1 <= location[0] + 6; day1++)
{
cout << setfill(' ') << setw(5) << day1;
}
location[0] = day1;
PrintDistance();
//打印该双月第五行日历
int m2 = 7 - n2; //m2记录该双月第五行空白的天数
for (day2 = location[1]; day2 <= location[1] + n2 - 1; day2++)
{
cout << setfill(' ') << setw(5) << day2;
if (day2 == location[1] + n2 - 1)
cout << endl;
}
//打印该单月第六行日历
cout << " ";
n1 = n1 - 7;
for (day1 = location[0]; day1 <= location[0] + n1 - 1; day1++)
{
cout << setfill(' ') << setw(5) << day1;
}
cout << endl;
}
Sleep(1000);
}
}
//菜单操作系统
void Calendar::Menu()
{
int n;
char ch1;
ABC1:
cout << endl << " ◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆菜单系统◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆" << endl;
cout << endl;
Sleep(750);
cout << " 您有如下操作可以选择:" << endl;
cout << endl;
Sleep(750);
cout << " 1.继续查询其他年份" << endl;
cout << endl;
Sleep(750);
cout << " 2.退出本次使用" << endl << endl;
Sleep(750);
cout << " 请输入您所需要的操作的序号:";
ABC2:
cin >> n;
cin.get(ch1);
if (cin.good() && n != 1 && n != 2 && ch1 == '\n')
{
cout << endl << " 您所输入的选项有误,请重新输入:";
goto ABC2;
}
else if (cin.fail() || ch1 != '\n')
{
cout << endl << " 您所输入的选项有误,请重新输入:";
cin.clear();
cin.ignore(1000, '\n');
goto ABC2;
}
else if (n == 2 && ch1 == '\n')
End();
else if (n == 1 && ch1 == '\n')
{
int year;
char ch2;
cout << endl << " 请输入您想要查询的年份:";
while (true)
{
cin >> year;
cin.get(ch2);
if (cin.good() && (year < 1 || year>9999) && ch2 == '\n')
{
cout << endl << " 您的输入有误,所输入的年份必须在公元1——9999年间!" << endl << endl;
cout << " 请您重新输入:";
}
else if (cin.fail() || ch2 != '\n')
{
cout << endl << " 您的输入有误,所输入的年份必须在公元1——9999年间!" << endl << endl;
cout << " 请您重新输入:";
cin.clear();
cin.ignore(1000, '\n');
}
else
break;
}
ChangeYear(year);
Display();
goto ABC1;
}
}