C++實現日期類函式
阿新 • • 發佈:2018-12-22
今天做了一個專案,其中涉及到了日期的處理,通過一些查詢,和自己的修改,寫了一個工具類,給大家共享一下,有需要的可以直接用啦,每個方法我都基本
上寫了註釋,希望大家能好好的理解,共同進步吧。
下邊就把程式碼貼出來了。
/***標頭檔案。 只進行了一些定義 Date.h author:wallwind time 2011/8/29 **/ #pragma once #include <string> #include <iostream> class Date //建立一個Date類, { int year,month,day;// private: int DayOfMonth(int y,int m)const;//返回一個月的天數。 int ToInt()const;// public: Date() //建構函式,初始化預設的日期 :year(1900),month(1),day(1) { } Date(int y,int m,int d) :year(y),month(m),day(d)//對日期賦值,判斷有效的日期。 { if((y<=0)||(m<=0 || m>12)||(d<=0 || d>DayOfMonth(y,m))) { year = 1900; month = day = 1; } } virtual ~Date()//虛解構函式。 { } int GetYear()const//返回年份 { return year; } int GetMonth()const //返回月份 { return month; } int GetDay()const //返回日期 { return day; } bool IsLeapyear() const //判斷是否為平年。 { return year%400?(year%100?(year%4?(false):(true)):(false)):true; } bool IsLeapyear(const int y) const //判斷是否為平年。 { return y%400?(y%100?(y%4?false:true):false):true; } void Display() const //輸出日期值 { std::cout<<year<<"-"<<month<<"-"<<day<<std::endl; } //過載操作符 friend Date operator + (const int v,const Date a); friend Date operator + (const Date a,const int v); friend Date operator +=(Date &a,const int v); friend Date operator ++(Date &a); friend Date operator ++(Date &a,int); friend Date operator - (const Date a,const int v); friend int operator - (const Date a,const Date b); friend Date operator -=(Date &a,const int v); friend Date operator --(Date &a); friend Date operator --(Date &a,int); friend bool operator > (const Date a,const Date b); friend bool operator >=(const Date a,const Date b); friend bool operator < (const Date a,const Date b); friend bool operator <=(const Date a,const Date b); friend bool operator ==(const Date a,const Date b); friend bool operator !=(const Date a,const Date b); //friend std::ostream & operator <<(std::ostream &os,const Date &d); //friend std::istream & operator >>(std::istream &is,Date &d); };
下面是實現cpp檔案
/*** Date 日期類實現。//Date.cpp 2011/8/29 author:wallwind time 2011/8/29 ***/ #include "Date.h" //得到每個月的天數。。。。 int Date::DayOfMonth(int y,int m)const { int d = 0; switch(m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: d = 31; break; case 4: case 6: case 9: case 11: d = 30; break; case 2: d = 28 + IsLeapyear(y); break; } return d; } int Date::ToInt()const { int sum =0; for(int y=1;y<year;++y) { sum += 365+IsLeapyear(y); } for(int m=1;m<month;++m) sum += DayOfMonth(year,m); sum += day; return sum; } /////日期加天數。。const int v,const Date a。。 Date operator + (const int v,const Date a) { Date d = a; if(v==0) return d;//如果天數為0,返回當個月 if(v>0)//如果大於零。 { d.day += v; 將當天日期加上要增加的數 while(d.day>d.DayOfMonth(d.year,d.month))//////當加所加天數超過當月天數 { d.day -= d.DayOfMonth(d.year,d.month); d.month++; //////月份加一 if(d.month>12) ///如果月份大於12,那麼就增加一年。 { ++d.year; d.month = 1; } } return d; } else return d-(-v); } /////日期加天數。。const Date a,const int v 同上,方法的過載。 Date operator +(const Date a,const int v) { Date d = a; if(v==0) return d; if(v>0) { d.day += v; while(d.day>d.DayOfMonth(d.year,d.month)) { d.day -= d.DayOfMonth(d.year,d.month); d.month++; if(d.month>12) { ++d.year; d.month = 1; } } return d; } else return d-(-v); } //////////////加操作~同上。 Date operator +=(Date &a,const int v) { if(v==0) return a; if(v>0) { a.day+=v; while(a.day>a.DayOfMonth(a.year,a.month)) { a.day -= a.DayOfMonth(a.year,a.month); a.month++; if(a.month>12) { ++a.year; a.month = 1; } } return a; } else return a-=(-v); } //////日期自增操作。。。。。。同上,,,,, Date operator ++(Date &a) { ++a.day; if(a.day>a.DayOfMonth(a.year,a.month)) { a.day-=a.DayOfMonth(a.year,a.month); a.month++; if(a.month>12) { ++a.year; a.month = 1; } } return a; } ////自增操作。 Date operator ++(Date &a,int) { Date d = a; ++a.day; if(a.day>a.DayOfMonth(a.year,a.month)) { a.day-=a.DayOfMonth(a.year,a.month); a.month++; if(a.month>12) { ++a.year; a.month = 1; } } return d; } /////減操作。。。。。 Date operator - (const Date a,const int v) { Date d = a; if(v==0) return d; if(v>0) { d.day -= v; while(d.day<=0) { --d.month; if(d.month==0) { d.month=12; --d.year; } d.day+=d.DayOfMonth(d.year,d.month); } return d; } else return d+(-v); } int operator - (const Date a,const Date b) { return a.ToInt()-b.ToInt(); } Date operator -=(Date &a,const int v) { if(v==0) return a; if(v>0) { a.day -= v; while(a.day<=0) { --a.month; if(a.month==0) { a.month=12; --a.year; } a.day+=a.DayOfMonth(a.year,a.month); } return a; } else return a+=(-v); } Date operator --(Date &a) { --a.day; while(a.day<=0) { --a.month; if(a.month==0) { a.month = 12; --a.year; } a.day += a.DayOfMonth(a.year,a.month); } return a; } Date operator --(Date &a,int) { Date d = a; --a.day; while(a.day<=0) { --a.month; if(a.month==0) { a.month = 12; --a.year; } a.day += a.DayOfMonth(a.year,a.month); } return d; } /////日期的比較。。 bool operator > (const Date a,const Date b) { return a.ToInt()>b.ToInt(); } bool operator >=(const Date a,const Date b) { return a.ToInt()>=b.ToInt(); } bool operator < (const Date a,const Date b) { return a.ToInt()<b.ToInt(); } bool operator <=(const Date a,const Date b) { return a.ToInt()<=b.ToInt(); } bool operator ==(const Date a,const Date b) { return a.ToInt()==b.ToInt(); } bool operator !=(const Date a,const Date b) { return a.ToInt()!=b.ToInt(); } /*std::ostream & operator <<(std::ostream &os,const Date &d) { os<<d.year<<"-"<<d.month<<"-"<<d.day; return os; } std::istream & operator >>(std::istream &is,Date &d) { Date old = d; is>>d.year>>d.month>>d.day; if((d.year<=0) ||(d.month>12||d.month<=0) || (d.day<=0||d.day>d.DayOfMonth(d.year,d.month))) { d = old; throw std::exception("Invalid number of date."); } return is; } */