上部分为日期之间的简单运算,下部分为日期类写的万年历
int month_days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
#include<iostream>
#include<cstdio>
#include<iomanip>
using namespace std;
class Date
{
public:
Date(int year=0,int month=0,int day=0)
{
_year=year;
_month=month;
_day=day;
}
Date(Date& p)
{
_year=p._year;
_month=p._month;
_day=p._day;
}
//~Date();
void Set_date(int y,int m,int d);
void Print();
int Sum_days();
void Input();//进行操作选择
void SetDays();//如果输入某年,进行'3'操作处理
void SetMonth();//如果选输入某年某月,进行'2'操作处理
void SetYear();//如果输入某年某月某日,进行'1'操作处理
bool IsLeap(int year);//判断是否是闰年
bool IsRight(int year,int month,int day);//判断输入是否合法
int GetDays(int year,int month,int day);//得到此日期前的天数
void Print(int year,int month);//输出到界面上
void operator+(const int days)
{
_day+=days;
while(_day>=365)
{
if(IsLeap(_year)&&_day>365)
{
_year++;
_day-=366;
}
else
{
_year++;
_day-=365;
}
}
while(_day>31)
{
if(_month==1 ||_month==3 ||_month==5 ||_month==7 ||_month==8 || _month==10 || _month==12)
{
_month++;
_day-=31;
}
if(_month==4||_month==6||_month==9||_month==11)
{
_month++;
_day-=30;
}
else if(_month==2&&IsLeap(_year))
{
_month++;
_day-=29;
}
else
{
_month++;
_day-=28;
}
}
}
void operator-(const int days)
{
_day-=days;
while(_day<=0)
{
if((_month-1)==1 ||(_month-1)==3 ||(_month-1)==5 ||(_month-1)==7 ||(_month-1)==8 || (_month-1)==10 || (_month-1)==12)
{
_month--;
_day+=31;
}
if((_month-1)==4||(_month-1)==6||(_month-1)==9||(_month-1)==11)
{
_month--;
_day+=30;
}
else if((_month-1)==2&&IsLeap(_year))
{
_month--;
_day+=29;
}
else
{
_month--;
_day+=28;
}
}
}
Date operator-(const Date& c)
{
_year-=c._year;
_month-=c._month;
_day-=c._day;
if(_month<0)
{
_year--;
_month+=12;
}
while(_day<=0)
{
if((_month-1)==1 ||(_month-1)==3 ||(_month-1)==5 ||(_month-1)==7 ||(_month-1)==8 || (_month-1)==10 || (_month-1)==12)
{
_month--;
_day+=31;
}
if((_month-1)==4||(_month-1)==6||(_month-1)==9||(_month-1)==11)
{
_month--;
_day+=30;
}
else if((_month-1)==2&&IsLeap(_year))
{
_month--;
_day+=29;
}
else
{
_month--;
_day+=28;
}
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
void Date::Set_date(int year,int month,int day)
{
_year=year;
_month=month;
_day=day;
}
void Date::Print()
{
cout<<"("<<_year<<","<<_month<<","<<_day<<")"<<endl;
}
int Date::Sum_days()
{
int sum=0;
for(int i=1;i<_month;i++)
sum=sum+month_days[i];
int k=_year/4;
if(IsLeap(_year)&&_month>2)
return _year*365+sum+_day+k;
else
return _year*365+sum+_day;
}
void Date::Input()
{
char choose;
bool flag=true;
while(flag)
{
cout<<endl<<endl<<endl;
cout<<" ******下面进入万年历界面****** "<<endl;
cout<<"欢迎使用万年历!请选择:"<<endl;
cout<<"1:输入某年,显示该年的年历"<<endl;
cout<<"2:输入某年某月,显示该月的万年历"<<endl;
cout<<"3:输入某年某月某日,显示该日是星期几"<<endl;
cout<<"4:退出"<<endl;
cout<<"其他数字:退出"<<endl;
cout<<"输入您的选择: ";
cin>>choose;
switch(choose)
{
case '1':SetYear();break;
case '2':SetMonth();break;
case '3':SetDays();break;
case '4':flag=false;break;
default:cout<<"输入错误,请重新输入";
}
}
}
bool Date::IsRight(int year,int month,int day) //判断日期输入是否正确
{
if(year<1 || month<1 || month>12)//年月是否正确
return false;
//日期是否正确
if(day<0)return false;
else if( day==29)
return((month==2 && IsLeap(year))|| month!=2);
else if(day==31)return(month==1 ||month==3 ||month==5 ||month==7 ||month==8 || month==10 || month==12);
else if(day>31) return false;
else return true;
}
void Date::SetDays()//如果输入某年某月某日,进行'1'操作处理
{
int weekDay;
int year,month,day;
cout<<" 请输入年_月_日:";
cin>>year>>month>>day;
while(!IsRight(year,month,day))
{
cout<<"输入错误,请重新输入年_月_日:";
cin>>year>>month>>day;
}
weekDay=GetDays(year, month,day)%7;
switch(weekDay)
{
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;
}
}
bool Date::IsLeap(int year)//判断是否是闰年
{
return ((year%4==0 && year%100!=0)||(year%400==0));
}
int Date::GetDays(int year,int month,int day)//得到此日前所有已经过的日子
{
int yearDays, monthDays,sum;
int leap_year_day=0;//闰年多出来的天数
for(int i=1;i<year;i++)
if(IsLeap(i))
leap_year_day++;
yearDays=leap_year_day+365*(year-1);
if((year%4==0 && year%100!=0)||(year%400==0))
month_days[2]=29;//如果是闰年,则2月为29天
for(int j=1;j<month;j++)
monthDays+=month_days[j];
sum=yearDays+monthDays+day;//所有已经过的日子之和
return sum;
}
void Date::SetMonth()//当输入的是年月时,处理
{
int weekDay;
int year,month,day;
cout<<" 请输入年_月:";
cin>>year>>month;
day=1;
while(!IsRight(year,month,day))//判断输入是否正确,设置day=1
{
cout<<"输入错误,请重新输入年_月:";
cin>>year>>month;
}
weekDay=GetDays(year, month,day)%7;
Print(year,month);
}
void Date::SetYear()
{
int year,month,day;
cout<<" 请输入年:";
cin>>year;
month=1;day=1;
while(!IsRight(year,month,day))//判断是否输入正确,若错误,请重新输入 .设置day=1,month=1,
{
cout<<"输入错误,请重新输入年:";
cin>>year;
}
for(int k=1;k<=12;k++)//将12个月的万年历在界面上输出
Print(year,k);
}
void Date:: Print(int year,int month)//打印到界面上
{
int weekday,day=1;
cout<<" 公元"<<year<<"年"<<month<<"月"<<endl;
cout<<" SUN MON TUE WES THU FRI SAT"<<endl;
weekday=GetDays(year, month,day)%7;//所有的日期之和取余
switch(weekday)//输出处理
{
case 0: cout<<" "<<setw(5)<<"1"; break;
case 1: cout<<" "<<setw(5)<<"1";break;
case 2: cout<<" "<<setw(5)<<"1";break;
case 3: cout<<" "<<setw(5)<<"1";break;
case 4: cout<<" "<<setw(5)<<"1";break;
case 5: cout<<" "<<setw(5)<<"1";break;
case 6: cout<<" "<<setw(5)<<"1";break;
}
for(int i=2;i<=month_days[month];i++)
{
weekday=(++weekday)%7;
if(!weekday)
{cout<<endl; cout<<" "<<setw(6)<<i;}
else
cout<<setw(6)<<i;
}
cout<<endl;
}
void test1()
{
Date d1(2016,7,19);
Date d2(2016,7,5);
cout<<"原始日期数据为"<<endl;
d1.Print();
d2.Print();
d1.operator+(100);
cout<<"加上测试数后的结果为"<<endl;
d1.Print();
d2.operator-(100);
cout<<"减去测试数字后的结果为:"<<endl;
d2.Print();
}
void test2()
{
Date d4(2013,7,31);
Date d5(2009,4,5);
Date d6;
cout<<"原始日期数据为"<<endl;
d4.Print();
d5.Print();
d6=d4-d5;
cout<<"这两天相差"<<d6.Sum_days()<<"天"<<endl;
}
int main()
{
test1();
test2();
Date data;
data.Input();
system("pause");
return 0;
}