【C++入门】简单的日期类操作
生活随笔
收集整理的這篇文章主要介紹了
【C++入门】简单的日期类操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//--------------------------------------------------------------------------/***名稱:日期的簡單操作******類函數:構造函數,拷貝構造函數,析構函數,操作符重載函數****日期類操作函數:?1:計算一個日期加上多少天數后的日期???????**????????????????2:把該日期改為加上指定數目的天數后的日期**????????????????3:一個日期減上多少天數后的日期?**????????????????4:該日期加1(前置++)(后置++)**????????????????5:該日期減1(前置--)(后置--)**????????????????6:計算某日期到未來某日期間隔的天數******日期:2016/1/13*/
//---------------------------------------------------------------------------/*----------------------------------------頭文件--------------------------------------*/#?ifndef?__DATA_H__
#?define?__DATA_H__#?define?_CRT_SECURE_NO_WARNINGS?1#?include?<iostream>
#?include?<stdlib.h>
#?include?<assert.h>using?namespace?std;class?Date
{
public://打印函數void?Display();
public://構造函數Date(size_t?year,?size_t?month,?size_t?day);//拷貝構造函數Date(const?Date?&?d);//析構函數~Date();//操作符重載bool?operator==(const?Date?&?d);bool?operator>(const?Date?&?d);bool?operator<(const?Date?&?d);bool?operator>=(const?Date?&?d);bool?operator<=(const?Date?&?d);Date?operator=(const?Date?&?d);//日期類操作Date?operator+?(size_t?day);Date&?operator+=?(size_t?day);Date?operator-?(size_t?day);Date&?operator-=?(size_t?day);Date&?operator++();Date?operator++(int);Date&?operator--();Date?operator--(int);int?Date::operator-(const?Date?&?d);private:bool?_IsLeapYear(size_t?year);size_t?_GetMonthDay(size_t?year,?size_t?month);private:size_t?_year;size_t?_month;size_t?_day;
};#?endif???//__DATA_H__/*------------------------------------功能函數-----------------------------------------*/#?include?"Date.h"//打印函數
void?Date::Display()
{cout?<<?_year?<<?"年"?<<?_month?<<?"月"?<<?_day?<<?"日"?<<?endl;
}//構造函數
Date::Date(size_t?year?=?1900,?size_t?month?=?1,?size_t?day?=?1)
{if?(year?>?0?&&?month?>?0?&&?month<13?&&?day>0?&&?day?<=?_GetMonthDay(year,?month)){_year?=?year;_month?=?month;_day?=?day;}else{cout?<<?"初始化數據非法"?<<?endl;_year?=?1900;_month?=?1;_day?=?1;}
}//拷貝和構造函數
Date::Date(const?Date?&?d)
{_year?=?d._year;_month?=?d._month;_day?=?d._day;
}//析構函數
Date::~Date()
{//do_nothing
}/*操作符重載*/
bool?Date::operator==(const?Date?&?d)
{return?(_year?==?d._year)&&?(_month?==?d._month)&&?(_day?==?d._day);
}bool?Date::operator>(const?Date?&?d)
{if?(_year?>?d._year){return?true;}else?if?(_year?==?d._year?&&?_month?>?d._month){return?true;}else?if?(_year?==?d._year?&&?_month?==?d._month?&&?_day?>?d._day){return?true;}return?false;
}bool?Date::operator<(const?Date?&?d)
{return?!(*this?==?d)?&&?!(*this?>?d);
}bool?Date::operator>=(const?Date?&?d)
{return?(*this?==?d)?||?(*this?>?d);
}bool?Date::operator<=(const?Date?&?d)
{return?!(*this?>?d);
}Date?Date::operator=(const?Date?&?d)
{_year?=?d._year;_month?=?d._month;_day?=?d._day;return?*this;
}/*日期類操作*/
//判斷是否為閏年
bool?Date::_IsLeapYear(size_t?year)
{return?((0?==?year?%?4?&&?0?!=?year?%?100)?||?(0?==?year?%?400));
}//返回指定月的天數
size_t?Date::_GetMonthDay(size_t?year,?size_t?month)
{size_t?MonthArray[13]?=?{?0,?31,?28,?31,?30,?31,?30,?31,?31,?30,?31,?30,?31?};size_t?MonthDays?=?MonthArray[month];bool?ret;if?((2?==?month)?&&?(ret?=?_IsLeapYear(year))){MonthDays?=?29;}return?MonthDays;
}//一個日期加上多少天數后的日期
Date?Date::operator+?(size_t?day)
{size_t?MonthArray[13]?=?{?0,?31,?28,?31,?30,?31,?30,?31,?31,?30,?31,?30,?31?};Date?tmp(*this);size_t?SumDays?=?tmp._day?+?day;while?(true){if?(SumDays?<=?_GetMonthDay(tmp._year,?tmp._month)){tmp._day?=?SumDays;return?tmp;}else{SumDays?-=?_GetMonthDay(tmp._year,?tmp._month);tmp._month++;if?(tmp._month?>?12){tmp._year++;tmp._month?%=?12;}}}
}//把該日期改為加上指定數目的天數后的日期
Date&?Date::operator+=?(size_t?day)
{*this?=?operator+(day);return?*this;
}//一個日期減上多少天數后的日期?
Date?Date::operator-?(size_t?day)
{size_t?MonthArray[13]?=?{?0,?31,?28,?31,?30,?31,?30,?31,?31,?30,?31,?30,?31?};Date?tmp(*this);size_t?SumDays?=?tmp._day?+?day;while?(true){if?(day?<?tmp._day){tmp._day?-=?day;return?tmp;}else{day?-=?tmp._day;if?(1?==?tmp._month){tmp._year--;tmp._month?=?12;}else{tmp._month--;}tmp._day?=?_GetMonthDay(tmp._year,?tmp._month);}}
}//把該日期改為減上指定數目的天數后的日期
Date&?Date::operator-=?(size_t?day)
{*this?=?operator-(day);return?*this;
}//該日期加1(前置++)
Date&?Date::operator++()
{++_day;if?(_day?>?_GetMonthDay(_year,?_month)){_day?%=?_GetMonthDay(_year,?_month);++_month;if?(_month?>?12){++_year;_month?%=?12;}}return?*this;
}//日期加1(后置++)
Date?Date::operator++(int)
{Date?tmp(*this);*this?=?operator++();return?tmp;
}//該日期減1(前置--)
Date&?Date::operator--()
{if?(_day?>?1){--_day;}else{if?(1?==?_month){--_year;_month?=?12;_day?=?_GetMonthDay(_year,_month);}else{--_month;_day?=?_GetMonthDay(_year,?_month);}}return?*this;
}//該日期減1(后置--)
Date?Date::operator--(int)
{Date?tmp(*this);*this?=?operator--();return?tmp;
}//返回指定日期減去該日期后的天數
int?Date::operator-(const?Date?&?d)
{//異常:該日期比被減日期小if?(d._year?<?_year){return?-1;}else?if?(d._year?==?_year?&&?d._month?<?_month){return?-1;}else?if?(d._year?==?_year?&&?d._month?==?_month?&&?d._day?<?_day){return?-1;}//正常情況Date?tmp_d(d);Date?tmp_d_day(*this);int?ret?=?0;while?(!(tmp_d?==?tmp_d_day)){tmp_d_day.operator++();ret++;}return?ret;
} /*------------------------------------部分測試用例-------------------------------------*/#?include?"Date.h"//測試operator<=()
void?Test7()
{//falseDate?d1(2016,?1,?12);Date?d2(2016,?1,?11);bool?ret?=?d1?<=?d2;d1.Display();d2.Display();cout?<<?ret?<<?endl;//falseDate?d3(2016,?1,?12);Date?d4(2015,?1,?12);ret?=?d3?<=?d4;d3.Display();d4.Display();cout?<<?ret?<<?endl;//trueDate?d5(2016,?1,?12);Date?d6(2016,?1,?13);ret?=?d5?<=?d6;d5.Display();d6.Display();cout?<<?ret?<<?endl;//falseDate?d7(2016,?1,?12);Date?d8(2016,?1,?12);ret?=?d7?<=?d8;d7.Display();d8.Display();cout?<<?ret?<<?endl;}
//測試operator>=()
void?Test6()
{//trueDate?d1(2016,?1,?12);Date?d2(2016,?1,?11);bool?ret?=?d1?>=?d2;d1.Display();d2.Display();cout?<<?ret?<<?endl;//trueDate?d3(2016,?1,?12);Date?d4(2015,?1,?12);ret?=?d3?>=?d4;d3.Display();d4.Display();cout?<<?ret?<<?endl;//falseDate?d5(2016,?1,?12);Date?d6(2016,?1,?13);ret?=?d5?>=?d6;d5.Display();d6.Display();cout?<<?ret?<<?endl;//trueDate?d7(2016,?1,?12);Date?d8(2016,?1,?12);ret?=?d7?>=?d8;d7.Display();d8.Display();cout?<<?ret?<<?endl;
}
//測試operator<()
void?Test5()
{//falseDate?d1(2016,?1,?12);Date?d2(2016,?1,?11);bool?ret?=?d1?<?d2;d1.Display();d2.Display();cout?<<?ret?<<?endl;//falseDate?d3(2016,?1,?12);Date?d4(2015,?1,?12);ret?=?d3?<?d4;d3.Display();d4.Display();cout?<<?ret?<<?endl;//trueDate?d5(2016,?1,?12);Date?d6(2016,?1,?13);ret?=?d5?<?d6;d5.Display();d6.Display();cout?<<?ret?<<?endl;
}//測試operator>()
void?Test4()
{//trueDate?d1(2016,?1,?12);Date?d2(2016,?1,?11);bool?ret?=?d1?>?d2;d1.Display();d2.Display();cout?<<?ret?<<?endl;//falseDate?d3(2016,?1,?12);Date?d4(2016,?1,?12);ret?=?d3?>?d4;d3.Display();d4.Display();cout?<<?ret?<<?endl;//falseDate?d5(2016,?1,?12);Date?d6(2016,?1,?13);ret?=?d5?>?d6;d5.Display();d6.Display();cout?<<?ret?<<?endl;}//測試operator==
void?Test3()
{//falseDate?d1(2016,?1,?12);Date?d2(2016,?1,?11);bool?ret?=?d1?==?d2;d1.Display();d2.Display();cout?<<?ret?<<?endl;//trueDate?d3(2016,?2,?12);Date?d4(2016,?1,?12);ret?=?d3?==?d4;d3.Display();d4.Display();cout?<<?ret?<<?endl;//falseDate?d5(2016,?1,?12);Date?d6(2016,?1,?13);ret?=?d5?==?d6;d5.Display();d6.Display();cout?<<?ret?<<?endl;}//測試Date(const?Date?&?d)
void?Test2()
{Date?d1(2016,?1,?12);d1.Display();Date?d2(d1);d2.Display();Date?d3?=?d1;d3.Display();
}//測試Date(size_t?year?=?0,?size_t?month?=?0,?size_t?day?=?0)
void?Test1()
{Date?d(2016,?1,?12);d.Display();
}//測試operator+?(size_t?day)
void?Test()
{Date?d1(2016,?1,?13);d1.Display();cout?<<?"+366天"?<<?endl;Date?ret?=?d1.operator+?(366);ret.Display();
}//測試operator+=?(size_t?day)
void?Test8()
{Date?d1(2016,?1,?13);d1.Display();cout?<<?"+10天"?<<?endl;Date?ret?=?d1.operator+=?(18);ret.Display();
}//測試operator-?(size_t?day)
void?Test9()
{Date?d1(2016,?1,?10);d1.Display();cout?<<?"+375天"?<<?endl;Date?ret?=?d1.operator-?(375);ret.Display();
}//測試operator++()/operator++(int)
void?Test10()
{Date?d1(2016,?2,?29);d1.operator++();d1.Display();Date?d2(2016,?3,?3);d2.operator--();d2.Display();
}//測試operator--(int)/operator--()
void?Test11()
{Date?d1(2016,?12,?31);Date?ret?=?d1.operator++(int());ret.Display();Date?d2(2016,?12,?31);ret?=?d2.operator--(int());ret.Display();
}//測試operator-(const?Date?&?d)
void?Test12()
{Date?d1(2016,?1,?13);Date?d2(1970,?1,?1);int?ret?=?d2.operator-(d1);cout?<<?"起始日期"?<<?endl;d2.Display();cout?<<?"截止日期"?<<?endl;d1.Display();cout<<"間隔"<<?ret?<<?"天"?<<?endl;
}int?main()
{//Test();//Test1();//Test2();//Test3();//Test4();//Test5();//Test6();//Test7();//Test8();//Test9();//Test10();//Test11();Test12();system("pause");return?0;
}
轉載于:https://blog.51cto.com/814193594/1734719
總結
以上是生活随笔為你收集整理的【C++入门】简单的日期类操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .Net 自己写个简单的 半 ORM
- 下一篇: [MVC学习笔记]1.项目结构搭建及单个