日期操作(面向对象)具体实现

将专栏中上一篇文章用面向对象的方式实现
代码如下:
#include 

using namespace std;
class Date
{
private:
    int year,month,day;
public:
    Date(int year=0,int month=0,int day=0)
    {
        this->year=year,this->month=month,this->day=day;
    }
    void AddDays(int days)
    {
        for(int i=days; i; --i)
        {
            if(day==31)
            {
                if(month-12)
                {
                    month++;
                    day=1;
                }
                else
                {
                    year++;
                    month=1;
                    day=1;
                }
                continue;
            }
            if(day==30)
            {
                if(month==4||month==6||month==9||month==11)
                {
                    month++;
                    day=1;
                }
                else
                    day++;
                continue;
            }
            if(day==29&&month==2)
            {
                month++;
                day=1;
                continue;
            }
            if(day==28&&month==2)
            {
                if(((year%4==0)&&(year%100!=0))||(year%400==0))
                    day++;
                else
                {
                    month++;
                    day=1;
                }
                continue;
            }
            day++;
        }
    }
    void AddMonth(int months)
    {
        year+=(month+months)/12;
        month=(month+months)%12;
        if(day==31)
        {
            if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
                return;
            if(month==4||month==6||month==9||month==11)
            {
                day=30;
                return;
            }
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
            {
                day=29;
                return;
            }
            day=28;
            return;
        }
        if(day==30&&month==2)
        {
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
            {
                day=29;
                return;
            }
            day=28;
            return;
        }
        if(day==29&&month==2)
        {
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
                return;
            day=28;
            return;
        }
    }
    void AddYear(int years)
    {
        year+=years;
        if(day==29&&month==2)
        {
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
                return;
            day=28;
            return;
        }
    }
    int DayInYear(int Year,int Month)
    {
        int Long=0;
        int DAY[12]= {31,28,31,30,31,30,31,31,30,31,30,31};
        if(((Year%4==0)&&(Year%100!=0))||(Year%400==0))
            DAY[1]=29;
        for(int i=0; id.year)
            {
                temp1=year;
                year=d.year;
                d.year=temp1;
                temp2=month;
                month=d.month;
                d.month=temp2;
                temp3=day;
                day=d.day;
                d.day=temp3;
                flag=1;
            }
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
                a=366-DayInYear(year,month);
            else
                a=365-DayInYear(year,month);
            b=DayInYear(d.year,d.month);
            for(int i=year+1; i>op)
    {
        int n;
        int year1,month1,day1,year2,month2,day2;
        if(op!=4)
        {
            cin>>year1>>month1>>day1>>n;
            Date d1(year1,month1,day1);
            if(op==1)
            {
                d1.AddDays(n);
                cout<>year1>>month1>>day1>>year2>>month2>>day2;
            Date d1(year1,month1,day1);
            Date d2(year2,month2,day2);
            n=d1.Subtract(d2);
            cout<

你可能感兴趣的:(专栏,c/c++浅层基础问题讲解)