日期計算器

2021-07-12 03:24:52 字數 2411 閱讀 4789

#define _crt_secure_no_warnings

#include#includeusing namespace std;

class 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+(int  day);    //某個日期+天數

date &operator+=(int  day);   //某個日期+=天數

date operator-(int day);     //某個日期-天數

date &operator-=(int day);   //某個日期-=天數

date& operator++();        //某個日期++(前置)

date operator++(int);      //某個日期++(後置)

date& operator--();        //某個日期--(前置)

date operator--(int);      //某個日期--(後置)

int operator-( date& d);    //兩個日期相減

friend bool istruedate(date &d);   //友元函式,判斷某個日期是否是合法日期

private:

int year;

int month;

int day;

};date::date(int _year, int _month, int _day) :  // 用初始化列表初始化建構函式

year(_year),

month(_month),

day(_day)

bool date::operator<(const date& d)   //小於運算子的過載

bool date::operator>(const date& d)    //大於運算子的過載

bool date::operator<=(const date& d)    //小於等於運算子的過載

//bool date::operator<=(const date& d)   //小於等於運算子的過載

//bool date::operator>=(const date& d)     //大於等於運算子的過載

//bool date::operator>=(const date& d)    //大於等於運算子的過載

//bool isleapyear(int year)      //判斷某年是否是閏年

else

return false;

}int getmonthday(int year, int month)    //返回某個月的天數

;int day = montharray[month];

if (month == 2 && isleapyear(year))

return day;

}//日期計算器

date& date:: operator += (int day)

date date::operator+ (int day)

date tmp(*this);

if (istruedate(*this))

else}}

else

return tmp;

}date &date::operator -= (int  day)

date date::operator - (int day)

date tmp(*this);

if (istruedate(*this))

else

tmp.day += getmonthday(tmp.year, tmp.month);}}

return tmp;

}date date::operator--(int)   //後置--

date date::operator++(int)   //後置++

date& date::operator--()   //前置--

date&  date::operator++()   //前置++

bool  istruedate(date &d)

else

}int date::operator - ( date& d) 

else}}

return flag * days;

}

本文出自 「零點時光」 部落格,請務必保留此出處

日期計算器

日期計算器 生活中我們可能要計算一些距某乙個時間還有多少天的時候,但是在日曆裡算起來可能要麻煩一下,在學了c 的類之後可以很好的解決這一類的問題。例如距高考還有多少天 100天之後是幾號等。接下來就用日期類完成這些基本的日期運算功能 1.include 2.using namespace std 3...

日期計算器

注意 此日曆只能用於計算1982年10月15日以及以後的日期 1.日期 天數 返回日期 處理 1.如果 乙個負的天數的情況 2.如果加之後,或減之後的日期非法 2.兩個日期之間間隔的天數,日期 日期 返回的天數 處理 返回的天數不能為負 3.列印某年某月本月的日曆 處理 1982年十月份 功能測試 ...

日期計算器

class date return this 賦值操作已經完成,為什麼有個返回值?因為賦值運算子的過載支援三個數,i j k k先賦給j後有乙個返回值j,將這個返回值賦給i,返回的是同型別物件,所以型別為date 但此時如果沒有給函式型別加引用,就是傳值返回,不會直接返回,會建立乙個臨時物件。會多一...