// Calendar.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int daysOfMonth[31]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //定义全局变量
//类PerpetualCalendar开始
class PerpetualCalendar
{
public:
int getYear();
int getMonth();
int getDay();
void setYear();
void setMonth();
void setDate();
int getWeek();
void printMonth(); //按月打印万年历
void printYear(); //按年打印万年历
void printDay(); //打印日期对应的星期
void operate(); //执行各种操作的演示
PerpetualCalendar();//构造函数
PerpetualCalendar(int y,int m,int d); //构造函数重载
private:
int year;
int month;
int day;
bool isLeap(int y); //判断是否为闰年
int getDays(); //计算从0年以来有多少天
};
int main()
{
PerpetualCalendar test;
test.operate();
return 0;
}//end main
//类PerpetualCalendar的成员函数
int PerpetualCalendar::getYear()
{
return year;
}
int PerpetualCalendar::getMonth()
{
return month;
}
int PerpetualCalendar::getDay()
{
return day;
}
void PerpetualCalendar::setYear()
{
do
{
cout<<"Input year:";
cin>>year;
if(year<0||year>9999) cout<<"Input Error!"<<endl;
}while(year<0||year>9999);
month=1;
day=1;
}
void PerpetualCalendar::setMonth()
{
do
{
cout<<"Input year,month:";
cin>>year>>month;
if(year<0||year>9999||month<=0||month>12) cout<<"Input Error!"<<endl;
}while(year<0||year>9999||month<=0||month>12);
day=1;
}
void PerpetualCalendar::setDate() //改变日期
{
do
{
cout<<"Input year,month,day:";
cin>>year>>month>>day;
if(year<0||year>9999||month<=0||month>12||day<=0||day>31)
cout<<"Input Error!"<<endl;
}while(year<0||year>9999||month<=0||month>12||day<=0||day>31);
}
int PerpetualCalendar::getWeek() //用于计算所输入的年月日的星期数,返回0到6中的一个数字
{
int week=getDays()%7;
week+=5;
week%=7;
return week;
}
void PerpetualCalendar::printMonth()
{
int i;
int tempDay=day;
day=1;
int week=getWeek();
day=tempDay;
int setLine=week;
string leapYear;
if(isLeap(year)) leapYear="(闰年)";
else leapYear="(非闰年)";
cout<<endl<<" "<<"公元"<<year<<"年"<<month<<"月"<<leapYear<<endl; //输出标题XX年XX月
cout<<" SUN MON TUE WED THU FRI SAT"<<endl;
switch(week)
{
case 0:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0&&setLine!=0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 1:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 2:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 3:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags(ios::left)
<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 4:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags
(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 5:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags
(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
case 6:
for(i=1;i<=daysOfMonth[month];i++,setLine++)
{
if(setLine%7==0) cout<<endl<<" ";
if(i==1) cout<<" "<<setw(4)<<setiosflags
(ios::left)<<i;
else cout<<setw(4)<<setiosflags(ios::left)<<i;
}
cout<<endl;
break;
default:cout<<"ERROR!"<<endl;
}
}
void PerpetualCalendar::printYear()
{
int tempMonth=month;
for(int i=1;i<=12;i++)
{
month=i;
printMonth();
}
month=tempMonth;
}
void PerpetualCalendar::printDay()
{
switch(getWeek())
{
case 0: cout<<year<<"年"<<month<<"月"<<day<<"日 星期日"<<endl; break;
case 1: cout<<year<<"年"<<month<<"月"<<day<<"日 星期一"<<endl; break;
case 2: cout<<year<<"年"<<month<<"月"<<day<<"日 星期二"<<endl; break;
case 3: cout<<year<<"年"<<month<<"月"<<day<<"日 星期三"<<endl; break;
case 4: cout<<year<<"年"<<month<<"月"<<day<<"日 星期四"<<endl; break;
case 5: cout<<year<<"年"<<month<<"月"<<day<<"日 星期五"<<endl; break;
case 6: cout<<year<<"年"<<month<<"月"<<day<<"日 星期六"<<endl; break;
}
}
void PerpetualCalendar::operate()
{
int i,choose;
bool chooseFlag=false;
while(1)//while开始
{
while(!chooseFlag)
{
cout<<"……………………欢迎使用万年历……………………作者:魏洪源/n"<<endl;
cout<<"1.输入年,显示该年万年历"<<endl
<<"2.输入年,月,显示该月万年历"<<endl
<<"3.输入年月日,查询该日为星期几"<<endl
<<"4.退出"<<endl;
cout<<endl<<"请输入数字选择你要的操作:";
cin>>choose;
for(i=1;i<=4;i++)
{
if(choose==i) { chooseFlag=true; break;}
else chooseFlag=false;
}
if(!chooseFlag) cout<<"Error Input!"<<endl;
} //end while(!chooseFlag)
if(choose==1) //if 开始
{
setYear();
printYear();
}
else if(choose==2)
{
setMonth();
printMonth();
}
else if(choose==3)
{
setDate();
printDay();
}
else if(choose==4)
{
cout<<"程序结束"<<endl;
break;
}
else
{
cout<<"错误!"<<endl;
}//end if
chooseFlag=0;
}//end while(1)
}
PerpetualCalendar::PerpetualCalendar()//默认构造函数
{
year=0;
month=1;
day=1;
}
PerpetualCalendar::PerpetualCalendar(int y,int m,int d)//重载构造函数
{
year=y;
month=m;
day=d;
}
bool PerpetualCalendar::isLeap(int y) //判断是否为闰年的成员函数
{
if((y%4==0&&y%100!=0)||(y%400==0)) return true;
else return false;
}
int PerpetualCalendar::getDays() //用于计算所输入的年月日从0年以来的总天数
{
int i,sumDays=0;
for(i=0;i<year;i++)
{
if(isLeap(i)) sumDays+=366;
else sumDays+=365;
}
if(isLeap(year)) daysOfMonth[2]=29;
for(i=1;i<month;i++)
sumDays+=daysOfMonth[i];
sumDays+=day;
return sumDays;
}