📄 万年历.cpp
字号:
#include<iostream>
using namespace std;
#include<math.h>
class dateofyear
{
public:
dateofyear():
itsyear(2008),
itsmounth(1),
itsdate(1),
itsweek(2)
{}
dateofyear(int theyear,int themounth,int thedate,int theweek):
itsyear(theyear),
itsmounth(themounth),
itsdate(thedate),
itsweek(theweek)
{}
void setyear(int theyear){itsyear=theyear;}
void setmounth(int mounth){itsmounth=mounth;}
void setdate(int date){itsdate=date;}
void setweek(int week){itsweek=week;}
int getyear()const{return itsyear;}
int getmounth()const{return itsmounth;}
int getdate()const{return itsdate;}
int getweek()const{return itsweek;}
private:
int itsyear;
int itsmounth;
int itsdate;
int itsweek;
};
//判断该年是否闰年
int judgeleap(int theyear)
{
int flag=0;
if (theyear%400==0)
{
flag=1;
}
else if ((theyear%4==0)&&(theyear%100!=0))
{
flag=1;
}
return flag;
}
//计算下一天的星期
int nextdaysweek(dateofyear they)
{
int weekday;
weekday=they.getweek();
if (weekday==6)
{
weekday=0;
}
else weekday++;
return weekday;
}
//计算上一天的星期
int forthdaysweek(dateofyear they)
{
int weekday;
weekday=they.getweek();
if (weekday==0)
{
weekday=6;
}
else weekday--;
return weekday;
}
//计算下一天的月份
int nextdaysmounth(dateofyear theyear)
{
int themounth;
int thedate;
int flagleap=0;
themounth=theyear.getmounth();
thedate=theyear.getdate();
flagleap=judgeleap(theyear.getyear());
if (themounth==1||themounth==3||themounth==5||themounth==7||themounth==8||themounth==10||themounth==12)
{
if(thedate==31)
themounth++;
}
if (themounth==4||themounth==6||themounth==9||themounth==11)
{
if(thedate==30)
themounth++;
}
if (themounth==2)
{
if((thedate==28)&&(flagleap==0))
themounth++;
if((thedate==29)&&(flagleap==1))
themounth++;
}
return themounth;
}
//计算上一天的月份
int forthdaysmounth(dateofyear theyear)
{
int themounth;
int thedate;
int flagleap;
themounth=theyear.getmounth();
thedate=theyear.getdate();
flagleap=judgeleap(theyear.getyear());
if (thedate==1)
{
themounth--;
}
return themounth;
}
//计算上一天的日期
int forthdaysdate(dateofyear theyear)
{
int themounth;
int flagleap=0;
int thedate;
themounth=theyear.getmounth();
thedate=theyear.getdate();
flagleap=judgeleap(theyear.getyear());
if (themounth==5||themounth==7||themounth==10||themounth==12)
{
if (thedate==1)
{
thedate=30;
}
else thedate--;
}
if (themounth==4||themounth==6||themounth==9||themounth==11||themounth==8||themounth==1||themounth==2)
{
if (thedate==1)
{
thedate=31;
}
else thedate--;
}
if ((themounth==3)&&(flagleap==1))
{
if (thedate==1)
{
thedate=29;
}
else thedate--;
}
if ((themounth==3)&&(flagleap==0))
{
if (thedate==1)
{
thedate=28;
}
else thedate--;
}
return thedate;
}
//计算下一天的日期
int nextdaysdate(dateofyear theyear)
{
int themounth;
int thedate;
int flagleap=0;
flagleap=judgeleap(theyear.getyear());
themounth=theyear.getmounth();
thedate=theyear.getdate();
if (themounth==1||themounth==3||themounth==5||themounth==7||themounth==8||themounth==10||themounth==12)
{
if (thedate==31)
{
thedate=1;
}
else thedate++;
}
if (themounth==4||themounth==6||themounth==9||themounth==11)
{
if (thedate==30)
{
thedate=1;
}
else thedate++;
}
if ((themounth==2)&&(flagleap))
{
if (thedate==29)
{
thedate=1;
}
else thedate++;
}
if ((themounth==2)&&(!flagleap))
{
if (thedate==28)
{
thedate=1;
}
else thedate++;
}
return thedate;
}
//计算下一天的日期和星期
dateofyear nextday(dateofyear theday)
{
dateofyear correntday;
correntday.setyear(theday.getyear());
correntday.setweek(nextdaysweek(theday));
correntday.setmounth(nextdaysmounth(theday));
correntday.setdate(nextdaysdate(theday));
return correntday;
}
//计算上一天的日期和星期
dateofyear forthday(dateofyear theday)
{
dateofyear correntday;
correntday.setyear(theday.getyear());
correntday.setmounth(forthdaysmounth(theday));
correntday.setdate(forthdaysdate(theday));
correntday.setweek(forthdaysweek(theday));
return correntday;
}
int calculateweek(int datesto2008)
{
int theweekday=2;
int i;
if(datesto2008>=0)
{
for (i=0;i<datesto2008;i++)
{
if (theweekday==6)
{
theweekday=0;
}
else theweekday++;
}
}
else
{
for (i=0;i>datesto2008;i--)
{
if (theweekday==0)
{
theweekday=6;
}
else theweekday--;
}
}
return theweekday;
}
//输出日历
void outputcalinder(dateofyear year)
{
int theyear;
int themounth;
int thedate;
int theweek;
int datesofoneyear;
int flagleap=0;
int i;
flagleap=judgeleap(year.getyear());
if (flagleap==0)
{
datesofoneyear=365;
}
if (flagleap==1)
{
datesofoneyear=366;
}
theyear=year.getyear();
themounth=year.getmounth();
thedate=year.getdate();
theweek=year.getweek();
for (i=0;i<datesofoneyear;i++)
{
if(year.getdate()==1)
{
cout<<endl;
cout<<"========================"<<year.getmounth()<<"月========================"<<endl;
cout<<"日\t一\t二\t三\t四\t五\t六"<<endl;
}
theyear=year.getyear();
themounth=year.getmounth();
thedate=year.getdate();
theweek=year.getweek();
if (thedate==1)
{
switch(theweek)
{
case 0:
break;
case 1:cout<<"\t";
break;
case 2:cout<<"\t\t";
break;
case 3:cout<<"\t\t\t";
break;
case 4:cout<<"\t\t\t\t";
break;
case 5:cout<<"\t\t\t\t\t";
break;
case 6:cout<<"\t\t\t\t\t\t";
break;
default:cout<<"出错啦!";
}
}
cout<<year.getdate()<<"\t";
if (year.getweek()==6)
{
cout<<endl;
}
year=nextday(year);
}
}
//计算离2008年一月一日的天数
int datesto200811(int yearofinput)
{
int counterofyear;
int flagofleap,datesto2008=0;
if(yearofinput>=2008)
{
for (counterofyear=2008;counterofyear<yearofinput;counterofyear++)
{
flagofleap=judgeleap(counterofyear);
if (flagofleap==1)
{
datesto2008+=366;
}
else datesto2008+=365;
}
}
else
{
for (counterofyear=2007;counterofyear>=yearofinput;counterofyear--)
{
flagofleap=judgeleap(counterofyear);
if (flagofleap==1)
{
datesto2008-=366;
}
else datesto2008-=365;
}
}
return datesto2008;
}
int main()
{
int flagofleap=0;
int datesto2008=0;
int judgeyear;
int theweek;
dateofyear theyear;
cout<<"输入年,按回车即可按月输出日历。"<<endl;
cin>>judgeyear;
cout<<" ========"<<judgeyear<<"年========";
datesto2008=datesto200811(judgeyear);
theweek=calculateweek(datesto2008);
theyear.setyear(judgeyear);
theyear.setweek(theweek);
outputcalinder(theyear);
cout<<endl;
//flagofleap=judgeleap(judgeyear);
//cout<<flagofleap;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -