项目展示
项目代码:
#include<iostream>
#include<iomanip>
using namespace std;
int Date(int y, int m, int d)
{
int week;
if (m == 1 || m == 2)
{
y = y - 1;
m = m + 12;
}
week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7; //利用基姆拉尔森计算公式计算某年某月某日是星期几
//(其中基姆拉尔森计算公式:
//W = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7
return week;
}
int DayNumofMonth(int y, int m)
{
int monthmax = 0;
switch (m)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthmax = 31;
break;
case 4:
case 6:
case 9:
case 11:
monthmax = 30;
break;
case 2:
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
monthmax = 29;
else
monthmax = 28;
}
return monthmax;
}
void DisCalendar(int y, int m)
{
//标题模块
cout << " 公元 " << y << "年 第" << m << "月日历" << endl;
int a = 1;
for (a = 0; a < 7; a++)
cout << setw(5) << a;
cout << endl << "===========================================" << endl;
//日期模块
int Date(int y, int m, int d);
int b = 0, week;
week = Date(y, m, 1);
for (b = 0; b < week; b++)
cout << setw(5) << " ";//每月一号对应的周几前面要有缩进
int DayNumofMonth(int y, int m);
int c = 1, monthmax;
monthmax = DayNumofMonth(y, m);
while(c <= monthmax)
{
cout << setw(5) << c;
c++;
week++;
if (week % 7 == 0)
cout << endl;
}
cout << endl << "===========================================" << endl;
}
//增加菜单模块
void MenuFunc()
{
while (1)
{
int n;
cout << "***************************" << endl;
cout << "请输入1或0进入程序" << endl << "1----日历查询" << endl << "0----推出程序" << endl;
cout << "***************************" << endl;
cin >> n;
if (n == 0)
break;
if (n == 1)
{
int y, m;
cout << "请输入年份:";
cin >> y;
cout << "请输入月份:";
cin >> m;
cout << endl;
void DisCalendar(int y, int m);
DisCalendar(y, m);
}
}
}
int main()
{
MenuFunc();
return 0;
}