这次日期类的实现呢,主要具备查某月的日历,日期加减天数后是哪一天,两个日期之间相差的天数。比如说:你想知道离开学的日子还有多少天啊,离我们找工作还有多少天呐,这个月的日历是什么样的啊都可以它来查询!
下面给出代码实现:
Date.h
#ifndef _DATE__H_
#define _DATE__H_
#include<cstdio>
#include<iostream>
using namespace std;
class Date
{
friend void PrintDate(int year,int month);
public:
Date(int year,int month,int day);
Date(const Date& d);
bool JudgeLeapyear(int year)const;
int SetDay(const Date& d); //设置每月的天数并返回是哪个月
Date operator+(int day); //日期加天数
Date operator-(int day); //日期减天数
int operator-(const Date& date); //日期减日期(相差天数)
int Week(int year,int month); //计算当前月的第一天是星期几
void Display();
bool operator==(const Date& d);
bool operator>(const Date& d);
Date& operator=(const Date& d);
private:
int _year;
int _month;
int _day;
};
void PrintDate(int year,int month);//打印该月日历
#endif //_DATE__H_
Date.cpp
#include"Date.h"
Date::Date(int year=1990,int month=1,int day=1)
:_year(year),
_month(month),
_day(day)
{
if(month>12 ||day>SetDay(*this))
{
cout<<"输入日期不合法!"<<endl;
}
}
Date::Date(const Date& d)
{
_year=d._year;
_month=d._month;
_day=d._day;
}
bool Date::JudgeLeapyear(int year)const
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
return true;
}
else
{
return false;
}
}
int Date::SetDay(const Date& d)
{
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(JudgeLeapyear(d._year))
{
month[2]=29;
}
return month[d._month]; //返回是哪个月
}
bool Date::operator>(const Date& d)
{
if(_year>d._year)
{
return true;
}
else if(_year==d._year)
{
if(_month>d._month)
{
return true;
}
else if(_month==d._month)
{
if(_day>d._day)
{
return true;
}
else
return false;
}
else
return false;
}
else
return false;
}
bool Date::operator==(const Date& d)
{
if(_year==d._year && _month==d._month && _day==d._day)
{
return true;
}
else
{
return false;
}
}
Date& Date::operator=(const Date& d)
{
if(*this==d)
{
return *this;
}
_year=d._year;
_month=d._month;
_day=d._day;
return *this;
}
Date Date::operator+(int day) //日期加天数
{
_day=_day+day;
while(_day > SetDay(*this))
{
_day=_day-SetDay(*this);
_month++;
if(_month>12)
{
_month=_month-12;
_year++;
}
}
return *this;
}
Date Date::operator-(int day) //日期减天数
{
_day=_day-day;
while(_day<=0)
{
_month--;
if(_month<=0)
{
_month=_month+12;
_year--;
}
_day=_day+SetDay(*this);
}
return *this;
}
int Date::operator-(const Date& date) //日期减日期
{
int days=0; //统计相差几天
Date small(*this);
Date big(date);
if(*this>date)
{
big=*this;
small=date;
}
while(big._year>small._year)
{
if(JudgeLeapyear(small._year))
{
days=days+366;
}
else
{
days=days+365;
}
small._year++;
}
while(small._month>big._month)
{
days=days-SetDay(small);
small._month--;
}
while(big._month>small._month)
{
days=days+SetDay(small);
small._month++;
}
days=days+(big._day-small._day);
if(*this>date)
{
return -days;
}
else
return days;
}
int Date::Week(int year,int month)
{
if(year>=1600 && month>=1)
{
int week=6; //日历从1600.1.1开始该天是周六
int day=0;
Date d1(1600,1,1);
Date d2(year,month,1);
day=d1-d2; //统计目标月份与起始月份相差的天数
week=day%7+week;
if(week>7)
{
week=week-7;
}
return week;
}
return -1;
}
void PrintDate(int year,int month)
{
int i=0; //控制最多输出行
int j=0; //控制列
int day=1;
Date d(year,month,1);
int week=d.Week(year,month);
if(week!=-1)
{
cout<<year<<"年"<<month<<"月"<<endl;
cout<<"日"<<" "<<"一"<<" "<<"二"<<" "<<"三"<<" "<<"四"<<" "<<"五"<<" "<<"六"<<" "<<endl;
for(j=0;j<week;j++)
{
cout<<" ";
}
for(i=0;i<6;i++)
{
for(j=week;j<7;j++)
{
if(day<=d.SetDay(d))
{
printf("%3d",day);
day++;
}
else
break;
}
cout<<endl;
week=0;
}
}
else
return;
}
void Date::Display()
{
cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
test.cpp
#include"Date.h"
void test1() //加减天数
{
Date d1(2014,9,1);
d1.Display();
Date d2=d1+21;
d2.Display();
Date d3=d1+60;
d3.Display();
//Date d4=d1-50;
//d4.Display();
}
void test2()
{
int ret;
Date d1(2011,1,2);
d1.Display();
Date d2(2016,1,1);
d2.Display();
ret=d1-d2;
cout<< "相差天数:"<<ret<<endl;
}
void test3()
{
int year=0;
int month=0;
printf("%d%d",&year,&month);
PrintDate(year,month);
}
int main()
{
//test1();
//test2();
test3();
system("pause");
return 0;
}