/*
这是一个万年历程序,阅读并理解程序
(1)先看main()函数,菜单中列出了程序能实现的功能。
(2)再浏览Date 类的定义,清楚类中有哪些数据成员与成员函数(先只知其名即可);
(3)针对main()函数中的每个功能,模拟计算机的执行,按计算机执行这些代码的顺序,阅读程
序的细节。中间有不明白的地方标记出来与同学讨论。有些功能,如cin.sync(),在课堂上肯定不
会讲到,通过上网找到对其的解释(这将是今后学习的方式)。
(4)再从头到尾读一遍程序,这一次你的注意力可以集中在程序的结构上,对某些算法也可再
次地进行剖析。
(5)对于程序中分析不清及自己没有理解的部分,可以在上机时,通过设置断点, 运用调试手
段对程序的执行过程进行跟踪,重点观察变量和对象的变化过程。
(6)这个程序只依靠本学期第4 周之前学习过的知识就可看懂。如果不顺利,一是要相信投入
必有回报,二是要意识到尽快跟上,不要将问题一直藏下去。*/
#include <iostream>
#include <iomanip>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
int monthDay[12];
public:
Date(int y=1, int m=1, int d=1):year(y),month(m),day(d)
{
monthDay[0]=monthDay[2]=monthDay[4]=monthDay[6]=monthDay[7]=monthDay[9]=monthDay[11]=31;
monthDay[1]=28;
monthDay[3]= monthDay[5]= monthDay[8]= monthDay[10]=30;
}
void SetYear(int y) { year=y; }
void SetMonth(int m) { month=m; }
void SetDay(int d) { day=d; }
int GetYear() const { return year; }
int GetMonth() const { return month; }
int GetDay() const { return day; }
int GetMonthDay(const int i)
{
if(i==2&&Isleapyear(year))
return 29;
return monthDay[i-1];
}
bool Isleapyear(int y) // 判断是否为闰年。
{
return ((y%4==0 && y%100!=0)||(y%400==0));
}
int GetYearDays(int y) // 年份y 的天数。
{
if(Isleapyear(y))
return 366;
return 365;
}
int DateToNum() // 给定日期,返回天数。
{
int sum=0;
int i=0;
for(i=1;i<year;i++)
sum+=GetYearDays(i);
if(Isleapyear(year))
monthDay[1]=29;
else
monthDay[1]=28;
for(int j=1;j<month;j++)
sum+=monthDay[j-1];
return sum+day;
}
Date NumToDay(int n) // 给定天数,返回日期。
{
Date d(1, 1, 1);
for(;n>=GetYearDays(d.year);d.year++)
n-=GetYearDays(d.year);
if(Isleapyear(d.year))
monthDay[1]=29;
else
monthDay[1]=28;
for(;n>=monthDay[d.month];d.month++)
n-=monthDay[d.month];
d.day=n;
return d;
}
void OutputYearDate(int y) // 给定年份y,输出年份y 的日历。
{
if(y<=0)
return;
int i=0;
int j=0;
Date d;
d.year=y;
d.day=1;
cout<<endl<<endl<<setw(20)<<y<<"年"<<endl;
while(i++<12)
{
cout<<endl<<endl<<setw(15)<<i<<" 月"<<endl;
cout<<endl
<<setw(5)<<"SUN"
<<setw(5)<<"MON"
<<setw(5)<<"TUE"
<<setw(5)<<"WED"
<<setw(5)<<"THU"
<<setw(5)<<"FRI"
<<setw(5)<<"SAT"
<<endl;
cout<<"-----------------------------------"<<endl;
j=0;
d.month=i;
cout<<setw(5*(d.DateToNum()%7)+5)<<1;
for(j=1;j<monthDay[i-1];j++)
{
if((j+(d.DateToNum()%7))%7==0)
cout<<endl;
cout<<setw(5)<<j+1;
}
cout<<endl;
cout<<"-----------------------------------"<<endl;
}
}
void OutputYearMonthDate(int y, int m)//给定年y,月m,输出y 年m 月的日历
{
if(y<=0)
return;
Date d(y, m, 1);
cout<<endl<<endl<<setw(15)<<m<<" 月"<<endl;
cout<<endl
<<setw(5)<<"SUN"
<<setw(5)<<"MON"
<<setw(5)<<"TUE"
<<setw(5)<<"WED"
<<setw(5)<<"THU"
<<setw(5)<<"FRI"
<<setw(5)<<"SAT"
<<endl;
cout<<"-----------------------------------"<<endl;
cout<<setw(5*(d.DateToNum()%7)+5)<<1;
for(int j=1;j<monthDay[m-1];j++)
{
if((j+(d.DateToNum()%7))%7==0)
cout<<endl;
cout<<setw(5)<<j+1;
}
cout<<endl;
cout<<"-----------------------------------"<<endl;
}
static int Week(Date d)
{
if(d.DateToNum()%7==0)
return 7;
return(d.DateToNum()%7);
}
};
int main(void)
{
Date d;
int number;
char choose;
bool flag=true;
while(flag)
{
cout<<endl<<" =======》》使用说明《《======"<<endl<<endl;
cout<<" ||"<<endl;
cout<<" ||"<<endl;
cout<<" ╲╱ "<<endl<<endl;
cout<<" 设公元年月日为第一天"<<endl<<endl;
cout<<" 输入==》1 打印某年的日历;"<<endl<<endl;
cout<<" 输入==》2 查看某年是否是闰年;"<<endl<<endl;
cout<<" 输入==》3 查看某日期是星期几;"<<endl<<endl;
cout<<" 输入==》4 查看某年某月的日历;"<<endl<<endl;
cout<<" 输入==》5 给定天数,返回日期;"<<endl<<endl;
cout<<" 输入==》6 退出;"<<endl<<endl;
cout<<"输入您的选择:";
cin>>choose;
while (getchar()!='\n');
if(!cin)
{
cin.clear();
cin.sync();
cout<<"输入错误";
system("pause");
system("cls");
continue;
}
switch(choose)
{
case '1':
cout<<"输入年:";
cin>>number;
d.SetYear(number);
if(d.GetYear()<=0)
{
cout<<"输入年份有错,返回。"<<endl;
system("pause");
system("cls");
break;
}
d.OutputYearDate(d.GetYear());
system("pause");
system("cls");
break;
case '2':
cout<<"输入年:";
cin>>number;
d.SetYear(number);
if(d.GetYear()<=0)
{
cout<<"输入年份有错,返回。"<<endl;
system("pause");
system("cls");
break;
}
if(d.Isleapyear(d.GetYear()))
cout<<"闰年。"<<endl;
else
cout<<"非闰年."<<endl;
system("pause");
system("cls");
break;
case '3':
cout<<"输入年:";
cin>>number;
d.SetYear(number);
cout<<"输入月:";
cin>>number;
d.SetMonth(number);
cout<<"输入日:";
cin>>number;
d.SetDay(number);
if(d.GetYear()<=0||d.GetMonth()>12||d.GetMonth()<1||d.GetDay()<1||d.GetDay()>d.GetMonthDay(2))
{
cout<<"输入有误,返回。"<<endl;
system("pause");
system("cls");
break;
}
cout<<"星期"<<Date::Week(d)<<endl;
system("pause");
system("cls");
break;
case '4':
cout<<"输入年:";
cin>>number;
d.SetYear(number);
cout<<"输入月:";
cin>>number;
d.SetMonth(number);
if(d.GetYear()<=0||d.GetMonth()>12||d.GetMonth()<1)
{
cout<<"输入有误,返回。"<<endl;
system("pause");
system("cls");
break;
}
d.OutputYearMonthDate(d.GetYear(),d.GetMonth());
system("pause");
system("cls");
break;
case '5':
cout<<"输入天数:";
cin>>number;
if(number<=0)
{
cout<<"输入有误,返回。"<<endl;
system("pause");
system("cls");
break;
}
cout<<"年:"<<d.NumToDay(number).GetYear()<<endl;
cout<<"月:"<<d.NumToDay(number).GetMonth()<<endl;
cout<<"日:"<<d.NumToDay(number).GetDay()<<endl;
system("pause");
system("cls");
break;
case '6':
exit(1);
default:
cout<<"输入错误,请重新输入";
system("pause");
system("cls");
}
}
return 0;
}
