#include"iostream"
#include"windows.h"//获取系统时间s
#include"string"
void year();
void ymonth();
void day();
char menu_choose();
void yue(int i,int*k,int*month_numble,int*year);//函数实现输出每月的日历
void yue1(int*k,int*month_numble,int*year,int*ymonth);//函数实现输出每月的日历
void day2(int*k,int*month_numble,int*year,int*month1,int*day1);
using namespace std;
void main()
{
while(1)
{
int choice=menu_choose()-48;
switch(choice)
{
case 1:year(); break;
case 2:ymonth();break;
case 3:day(); break;
case 4:exit(0); //退出程序
}
}
}
char menu_choose()
{
char choice;
cout<<"1.输出该年日历\n";
cout<<"2.输出该年该月的日历\n";
cout<<"3.输出距离今天还有多少天,为星期几,是否为公历节日\n";
cout<<"4.退出程序\n";
cout<<"请选择功能模块,选择数字1——4:";
while(1)
{
cin>>choice;
if((choice+0)>=49&&(choice+0)<=52)
break;
else
cout<<"输入的数字有误,请重新输入1——4:";
}
return choice;
}
void year()
{
int year;
while(1)
{
cout<<"请输入要查看的年份:";
cin>>year;
if(year>=1940)
{
int month_numble[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int month=1,k=2,test;
//k=2表示对应的天为星期一,1940年1月1号为星期一,以这一天为基准确定之后的日历
test=((year-1940)/4*5+((year-1940)%4==0?0:((year-1940)%4+1)))%7+2;
k=test%7==0?7:test%7;//确定每年的第一天为星期几
for(int i=0;i<12;i++)
{
switch(month+i)
{
case 1: yue(i,&k,month_numble,&year);break;
case 2: yue(i,&k,month_numble,&year);break;
case 3: yue(i,&k,month_numble,&year);break;
case 4: yue(i,&k,month_numble,&year);break;
case 5: yue(i,&k,month_numble,&year);break;
case 6: yue(i,&k,month_numble,&year);break;
case 7: yue(i,&k,month_numble,&year);break;
case 8: yue(i,&k,month_numble,&year);break;
case 9: yue(i,&k,month_numble,&year);break;
case 10:yue(i,&k,month_numble,&year);break;
case 11:yue(i,&k,month_numble,&year);break;
case 12:yue(i,&k,month_numble,&year);break;
}
}break;
}
else
cout<<"输入的年份不在范围之内,请输入1940及以后的年份。"<<endl;
}
}
void ymonth()
{
int year,ymonth,k=2,test;
while(1)
{
cout<<"请输入要查看的年份:";
cin>>year;
if(year>=1940)
{
int month_numble[12]={31,29,31,30,31,30,31,31,30,31,30,31};
//k=2表示对应的天为星期一,1940年1月1号为星期一,以这一天为基准确定之后的日历
test=((year-1940)/4*5+((year-1940)%4==0?0:((year-1940)%4+1)))%7+2;
k=test%7==0?7:test%7;//确定每年的第一天为星期几
while(1)
{
cout<<"请输入要查看的月份:";
cin>>ymonth;
if(ymonth>=1&&ymonth<=12)
{
yue1(&k,month_numble,&year,&ymonth);break;
}
else
cout<<"输入的月份不在范围之内,请输入正确的月份。"<<endl;
}break;
}
else
cout<<"输入的年份不在范围之内,请输入1940及以后的年份。"<<endl;
}
}
void day()
{
int year,month1,day1;
int month_numble[12]={31,29,31,30,31,30,31,31,30,31,30,31};
int month=1,k=2,test;
while(1)
{
cout<<"请输入要查看的年份:";
cin>>year;
//k=2表示对应的天为星期一,1940年1月1号为星期一,以这一天为基准确定之后的日历
test=((year-1940)/4*5+((year-1940)%4==0?0:((year-1940)%4+1)))%7+2;
k=test%7==0?7:test%7;//确定每年的第一天为星期几
if(year>=1940)
{
while(1)
{
cout<<"请输入要查看的月份:";
cin>>month1;
if(month1>=1&&month1<=12)
{
while(1)
{
cout<<"请输入要查看的天数:";
cin>>day1;
if(day1>=1&&day1<=(month1!=2?month_numble[month1-1]:(year%4==0?29:28)))
{
day2(&k,month_numble,&year,&month1,&day1);break;
}
else
cout<<"输入的天数不在范围之内,请输入正确的天数:"<<endl;
}break;
}
else
cout<<"输入的月份不在范围之内,请输入正确的月份:"<<endl;
}break;
}
else
cout<<"输入的年份不在范围之内,请输入1940及以后的年份:"<<endl;
}
}
void yue(int i,int*k,int*month_numble,int*year)//函数实现输出每月的日历
{
cout<<"======================="<<i+1<<"月======================="<<endl<<endl;
cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;
for(int h=1;h<*k;h++)
cout<<"\t";//控制每月1号从对应的星期几开始输出
for(int j=1;j<=(i!=1?month_numble[i]:(*year%4==0?29:28));j++)//区分平年和闰年
{
if(j%7!=(8-*k)%7)
cout<<j<<"\t";
else
cout<<j<<endl;
}
for(int j=1;j<=(i!=1?month_numble[i]:(*year%4==0?29:28));j++)
{
*k=*k%7;
*k+=1;
}
cout<<endl<<endl<<endl;
}
void yue1(int*k,int*month_numble,int*year,int*ymonth)//函数实现输出每月的日历
{
cout<<"======================="<<*ymonth<<"月======================="<<endl<<endl;
cout<<"日"<<"\t"<<"一"<<"\t"<<"二"<<"\t"<<"三"<<"\t"<<"四"<<"\t"<<"五"<<"\t"<<"六"<<endl;
for(int m=1;m<*ymonth;m++)
for(int j=1;j<=(m!=2?month_numble[m-1]:(*year%4==0?29:28));j++)
{
*k=*k%7;
*k+=1;
}
for(int h=1;h<*k;h++)
cout<<"\t";//控制每月1号从对应的星期几开始输出
for(int j=1;j<=month_numble[*ymonth-1];j++)//区分平年和闰年
{
if(j%7!=(8-*k)%7)
cout<<j<<"\t";
else
cout<<j<<endl;
}
cout<<endl<<endl<<endl;
}
void day2(int*k,int*month_numble,int*year,int*month1,int*day1)
{
for(int m=1;m<*month1;m++)
for(int j=1;j<=(m!=2?month_numble[m-1]:(*year%4==0?29:28));j++)
{
*k=*k%7;
*k+=1;
}
for(int j=2;j<=*day1;j++)
{
*k=*k%7;
*k+=1;
}
cout<<"这一天为星期";//输出这一天为星期几
switch(*k-1)
{
case 0: cout<<"天"<<endl;break;
case 1: cout<<"一"<<endl;break;
case 2: cout<<"二"<<endl;break;
case 3: cout<<"三"<<endl;break;
case 4: cout<<"四"<<endl;break;
case 5: cout<<"五"<<endl;break;
case 6: cout<<"六"<<endl;break;
}
if(*month1==1&&*day1==1)
cout<<"这一天为元旦"<<endl;
if(*month1==4&&*day1==4)
cout<<"这一天为清明节"<<endl;
if(*month1==5&&*day1==1)
cout<<"这一天五一国际劳动节"<<endl;
if(*month1==6&&*day1==1)
cout<<"这一天为儿童节"<<endl;
if(*month1==10&&*day1==1)
cout<<"这一天为国庆节"<<endl;
if(*month1==12&&*day1==25)
cout<<"这一天为圣诞节"<<endl;//输出这一天是否为公历节日
SYSTEMTIME sys;
GetLocalTime(&sys);//获取系统时间
int year_num,ymonth_num=0,day_num=0,tyear_num,tmonth_num=0,tday_num=0,fomer_num,today_num;
year_num=(*year-1940)/4*(366+365*3)+((*year-1940)%4==0?0:((*year-1940)%4*365+1));
for(int i=0;i<*month1-1;i++)
for(int j=1;j<=(i!=1?month_numble[i]:(*year%4==0?29:28));j++)
ymonth_num+=1;
for(int i=1;i<*day1;i++)
day_num+=1;
fomer_num=year_num+ymonth_num+day_num;//从1940年1月1号到这一天的天数
tyear_num=(sys.wYear-1940)/4*(366+365*3)+((sys.wYear-1940)%4==0?0:((sys.wYear-1940)%4*365+1));
for(int i=0;i<sys.wMonth-1;i++)
for(int j=1;j<=(i!=1?month_numble[i]:(sys.wYear%4==0?29:28));j++)
tmonth_num+=1;
for(int i=1;i<sys.wDay;i++)
tday_num+=1;
today_num=tyear_num+tmonth_num+tday_num;//从1940年1月1号到今天的天数
cout<<"这一天与今天相距的天数为:"<<abs(today_num-fomer_num)<<endl<<endl;//输出这一天与今天相距多少天
}
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)