日期計算器

2021-09-27 23:31:25 字數 2605 閱讀 6150

{} date operator+(uint delay) const;//日期加法過載

date operator-(uint delay) const;//日期減法過載

bool operator < (const date & d)const;

bool operator > (const date & d)const;

bool operator <= (const date & d)const;

bool operator >= (const date & d)const;

bool operator == (const date & d)const;

bool operator != (const date & d)const;

friend ostream & operator <

};static uint getmonthday(int y, uint m);//求乙個月大天數

int getleapnum(int startyear, int endyear);//求閏年個數

void test();

date.cpp

#include"date.h"

date date::operator+(uint delay) const //日期加重載

int leapnum = getleapnum(res.m_year+flag, res.m_year + numy);//求閏年個數

while(numd < leapnum)

numd -= leapnum;

res.m_year += numy;

delay = numd;//優化

tmp = getmonthday(res.m_year, res.m_month);

while (delay >= tmp)

tmp = getmonthday(res.m_year, res.m_month);

} res.m_day += delay;

if (res.m_day > getmonthday(res.m_year, res.m_month)) }

return res;

}date date::operator-(uint delay) const//日期減法過載

tmp = getmonthday(res.m_year, res.m_month);

} int count = res.m_day - delay;

if (count < 1)

delay -= res.m_day;

res.m_day = getmonthday(res.m_year, res.m_month);

res.m_day -= delay;

res.m_month--;

} else

return res;

}int getleapnum(int startyear, int endyear)//求閏年個數

int ret1 = (endyear - startyear)/400;

int ret2 = (endyear - startyear) / 4 + 1 - (endyear - startyear) / 100;

return ret1 + ret2;

}static uint getmonthday(int y, uint m)//求乙個月的天數

if (m == 4 || m == 6 || m == 9 || m == 11)

else if (m == 2)

else }

ostream & operator <

void test()

bool date::operator < (const date & d)const

else if (m_year == d.m_year)

else if (m_month == d.m_month)

}} return false;

}bool date::operator > (const date & d)const

else if (m_year == d.m_year)

else if (m_month == d.m_month)

}} return false;

}bool date::operator <= (const date & d)const

bool date::operator >= (const date & d)const

bool date::operator == (const date & d)const

return false;

}bool date::operator != (const date & d)const

日期計算器

define crt secure no warnings include includeusing namespace std class date bool operator const date d 小於運算子的過載 bool operator const date d 等於運算子的過載 bo...

日期計算器

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

日期計算器

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