//运用class例题
#include<iostream>
using namespace std;
class Date
{
private:
int Year;
int Month;
int Day;
public:
Date(){}//默认构造函数
Date(int y,int m,int d);//构造函数
void ShowDate();
void SetYear(int y);
void SetMonth(int m);
void SetDay(int d);
Date NextDay();//定义Date型的函数,将Date型的值返回
bool IsWeekend();
//~Date(){cout<<"执行析构函数\n";};//析构函数
};
Date::Date(int y,int m,int d)
{
Year=y;
Month=m;
Day=d;
}
void Date::ShowDate()//显示日期
{
cout<<Year<<"-"<<Month<<"-"<<Day<<endl;
}
void Date::SetYear(int y)//设置年份
{
Year=y;
}
void Date::SetMonth(int m)//设置月份
{
Month=m;
}
void Date::SetDay(int d)//设置日期
{
Day=d;
}
Date Date::NextDay() //设置后的第二天的日期
{
bool IsLeapyear(int);
Date today; //定义一个将要返回的Date类的值
today.Year=Year; //将刚定义的today 初始化
today.Month=Month;
today.Day=Day;
int mon[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
if(IsLeapyear(today.Year)==true)
{
today.Day++;
if(today.Day>mon[Month])
{
today.Day=1;
today.Month++;
if(today.Month>12)
{
today.Month=1;
today.Year++;
}
}
}
else
{
mon[2]=28;
today.Day++;
if(today.Day>mon[Month])
{
today.Day=1;
today.Month++;
if(today.Month>12)
{
today.Month=1;
today.Year++;
}
}
}
//cout<<Year<<"-"<<Month<<"-"<<Day<<endl;
return today;
}
bool Date::IsWeekend() //判断是否为周末
{
bool IsLeapyear(int);
int days,i;
int mon[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
days=0;
for(i=1;i<Year;i++)
if(IsLeapyear(i)==true)
days+=366;
else
days+=365;
if(Month<2)
days=days+31+Day;
else
{
if(IsLeapyear(Year)==true)
{
for(i=1;i<Month;i++)
days+=mon[i];
days+=Day;
}
else
{
for(i=1;i<Month;i++)
days+=mon[i];
days+=Day;
days--;
}
}
if(days%7==0||days%7==6)
return true;
else
return false;
}
bool IsLeapyear(int Year) //判断是否为闰年
{
if(Year%4==0&&Year%100!=0||Year%400==0)
return true;
else
return false;
}
int main()
{
int y,m,d;
Date tomorrow;
//t.ShowDate();
Date today(2012,2,11);
today.ShowDate();
cout<<"please input year:";
cin>>y;
cout<<"after set time :\n";
today.SetYear(y);
today.ShowDate();
cout<<"please input month:";
cin>>m;
cout<<"after set time :\n";
today.SetMonth(m);
today.ShowDate();
cout<<"please input day:";
cin>>d;
today.SetDay(d);
cout<<"after set time :\n";
today.ShowDate();
if(today.IsWeekend()==true)
cout<<"today is weekend !\n\n";
else
cout<<"today is not weekend .\n\n";
//cout<<"Today is Arbor Day !\n";
cout<<"Tomorrow's Date is:\n";
tomorrow=today.NextDay(); //执行函数,将today的下一天给tomorrow
tomorrow.ShowDate();
if(tomorrow.IsWeekend()==true)
cout<<"tomorrow is weekend !\n\n";
else
cout<<"tomorrow is not weekend .\n\n";
system("PAUSE");
return 0;
}