日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日期类Date

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日期类Date 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>


using namespace std;


//日期是否合法
//日期比較
//兩個日期中間差的天數
//日期加上或減去一定的天數后的日期


class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1)
{
if(year>0 && month>0 && month<13 && day>0 && day<32)
{
_year = year;
_month = month;
_day = day;
}
else
cout<<"日期不合法"<<endl;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}


? ? void Display() ?
? ? { ?
? ? ? ? cout << _year << "-" << _month << "-" << _day << endl; ?
? ? }


~Date()
{
}

bool operator==(const Date& d)
{
if( _year == d._year && _month == d._month && _day == d._day)
return true;
else
return false;
}


bool operator>(const Date& d)
{
if(_year > d._year)
return false;
else if(_year==d._year && _month>d._month )
return false;
else if(_year==d._year && _month==d._month && _day>d._day)
return false;
else
return true;
}
bool operator>=(const Date& d)
{
return (*this > d) || (*this == d);
}
bool operator<(const Date& d)
{
return !(*this >= d);
}
bool operator<=(const Date& d)
{
return !(*this > d);
}


Date& operator=(const Date &d) ? ? ? ?
? ? { ?
? ? ? ? if (this != &d) ?
? ? ? ? { ?
? ? ? ? ? ? this->_year = d._year; ?
? ? ? ? ? ? this->_month = d._month; ?
? ? ? ? ? ? this->_day = d._day; ?
? ? ? ? } ?
? ? ? ? return *this; ?
? ? }?


Date operator+(int days)
{
if(days < 0)
{
return operator-(-days);
}
int a = 0;
int sumDays = _day + days;?
while(sumDays > (a = GetDays(_year, _month)))
{
sumDays -= GetDays(_year, _month);
_month++;
if(_month > 12)
{
_year++;
_month -= 12;
}
else
{
_day = sumDays;
}
}
_day = sumDays;
return *this;
}


? ? Date& operator+=(int day) ? ? ? ? ??
? ? { ?
? ? ? ? *this = operator+(day); ?
? ? ? ? return *this; ?
? ? }?


Date operator-(int days)
{
if(days < 0)
{
return operator+(-days);
}


while(days >= _day)
{
days -= _day;
if(_month == 1)
{
_year--;
_month = 12;
}
else
{
_month--;
}
_day = GetDays(_year, _month);
}
_day -= days;
return *this;
}


Date& operator-=(int day) ? ? ? ? ??
? ? { ?
? ? ? ? *this = operator-(day); ?
? ? ? ? return *this; ?
? ? }
Date& operator++()
{
++_day;
if(_day > GetDays(_year, _month))
{
_day -= GetDays(_year, _month);
++_month;
if(_month > 12)
{
_month = 1;
_year++;
}
}
return *this;
}
Date operator++(int)
{
Date temp = *this;
*this = operator++();
return temp;
}


Date& operator--()
{
if (_day > 1) ?
? ? ? ? { ?
? ? ? ? ? ? --_day; ?
? ? ? ? } ?
? ? ? ? else ?
? ? ? ? { ?
? ? ? ? ? ? if (_month == 1) ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? --_year; ?
? ? ? ? ? ? ? ? _month = 12; ?
? ? ? ? ? ? ? ? _day = GetDays(_year, _month); ?
? ? ? ? ? ? } ?
? ? ? ? ? ? else ?
? ? ? ? ? ? { ?
? ? ? ? ? ? ? ? --_month; ?
? ? ? ? ? ? ? ? _day = GetDays(_year, _month); ?
? ? ? ? ? ? } ?
? ? ? ? } ?
? ? ? ? return *this;?


}
Date operator--(int)
{
Date temp = *this;
*this = operator--();
return temp;
}


int Day_day(Date& d)
{
int days = 0;
int balance = 0;
if(*this > d)
{
Date temp = *this;
*this = d;
d = temp;
}


? ?if(_year==d._year && _month==d._month)
{
balance = _day - d._day;


}
else if(_year==d._year)
{
balance = (GetDays(d._year, d._month) - d._day) + _day;
}
else
{
d._day = GetDays(d._year, d._month) - d._day;//計算大的日期距離當年年底的天數
while((++d._month)<=12)
d._day += GetDays(d._year, d._month);
while(--_month)//計算小的年份距離年初的天數
_day += GetDays(_year, _month);

while( (++d._year)<_year )//計算兩個日期間隔的年份的天數
{
if(IsLeapYear(d._year))
days += 366;
else
days += 365;
}


balance = d._day + _day + days; //間隔天數
}
}
return balance;
}
private:
bool IsLeapYear(int year) ? ? ? ? ? ? ??
? ? { ?
? ? ? ? if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ?
? ? ? ? ? ? return true; ?
? ? ? ? else ?
? ? ? ? ? ? return false; ?
? ? } ?
??
? ? int GetDays(int year, int month) ? ? ? ? ? ??
? ? { ?
? ? ? ? int month_Array[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; ?
??
? ? ? ? int day = month_Array[month - 1]; ?
??
? ? ? ? if (month == 2 && IsLeapYear(year)) ?
? ? ? ? { ?
? ? ? ? ? ? day += 1; ?
? ? ? ? } ?
??
? ? ? ? return day; ?
? ? }


int _year;
int _month;
int _day;
};


ostream& operator<<(ostream& _cout, const Date& d)
{
_cout<<d._year<<"-"<<d._month<<"-"<<d._day;
return _cout;
}


istream& operator>>(istream& _cin, Date& d)
{
_cin>>d._year>>d._month>>d._day;
return _cin;
}


void FunTest()
{


//Date d1(2016, 0, 23 );
//d1.Display();


//Date d2;
//cin>>d2;
//cout<<d2<<endl;
/*
? ? Date d1(2016, 10, 23 );
Date d2(2016, 10, 1 );
bool b = d1 == d2;
b = d1>d2;
b = d1<=d2;
cout<<b<<endl;


Date d3;
d3 = d1;
d3.Display();
*/


/*Date d4(2016, 10, 23 );
Date d5 = d4+10;
d5 = d4++;
d5 = ++d4;
d5.Display();*/
/*
Date d4(2016, 10, 23 );
Date d5 = d4-30;
d5 = d4--;
d5 = --d4;
d5.Display();
*/
/*
Date d4(2016, 10, 23 );
d4 += 10;
d4 -= 20;
d4.Display();
*/
? ? Date d6(2016, 10, 24 );
Date d7(2015, 10, 24 );
cout<<d6.Day_day(d7)<<endl;


}


int main()
{
FunTest();


system("pause");
return 0;


}

總結

以上是生活随笔為你收集整理的日期类Date的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。