日期計算器
生活中我們可能要計算一些距某乙個時間還有多少天的時候,但是在日曆裡算起來可能要麻煩一下,在學了c++的類之後可以很好的解決這一類的問題。例如距高考還有多少天、100天之後是幾號等。
接下來就用日期類完成這些基本的日期運算功能
1.
#include
2.using
namespace
std;
3.4.
class date
5.18.
}19.
20.date (const date& d)
21.26.
27.~date()
28.30.
void
display
()31.
34.35.
intgetmonthday
(int _year, int _month)
//獲取天數
36.;
40.int days = arr[_month];
41.if(_month == 2 && isleapyear(_year))//如果是閏年的二月,就給二月的天數加一天
42.45.
return days;
46.}
47.48.
bool
isleapyear
(int year)
//判斷是否是閏年
49.52.
53.bool
isinvalid
(int year, int month, int day)
//判斷日期是否合法
54.57.
58.//d1 > d2
59.bool
operator>(const date& d) const
60.72.
}73.
return
false;
74.}
75.76.
bool
operator>=(const date& d)const
77.80.
81.bool
operator
82.85.
86.bool
operator
<=(const date& d)const
87.90.
91.bool
operator==(const date& d)const
92.95.
96.bool
operator!=(const date& d)const
97.100.
101.
//d1 + 100
102.
date operator+(int day)//日期加上天數
103.
116.
}117.
return *this;
118.
}119.
120.
date operator-(int day)//過載-
121.
133.
_day = getmonthday(_year, _month) + _day;
134.
}135.
return *this;
136.
}137.
138.
date operator++()//日期+一天
139.
150.
}151.
return *this;
152.
}153.
154.
date operator++(int)
155.
158.
159.
date operator--()//日期減一天
160.
170.
_day = getmonthday(_year, _month);
171.
}172.
if(isinvalid(_year, _month, _day) == 0)//當減到2023年的時候不用再減了
173.
178.
return *this;
179.
}180.
181.
date operator--(int)//日期--,原日期不變
182.
185.
186.
intoperator-(const date& d)//計算兩個日期之間差了多少天
187.
196.
else
197.
201.
while(small != big)
202.
206.
return count;
207.
}208.
209.
private:
210.
int _year;
211.
int _month;
212.
int _day;
213.
};
測試一下距2023年高考還剩多長時間
以下測試是在久久時間網測試所得結果
距今天100天之後是多少號
日期計算器
define crt secure no warnings include includeusing namespace std class date bool operator const date d 小於運算子的過載 bool operator const date d 等於運算子的過載 bo...
日期計算器
注意 此日曆只能用於計算1982年10月15日以及以後的日期 1.日期 天數 返回日期 處理 1.如果 乙個負的天數的情況 2.如果加之後,或減之後的日期非法 2.兩個日期之間間隔的天數,日期 日期 返回的天數 處理 返回的天數不能為負 3.列印某年某月本月的日曆 處理 1982年十月份 功能測試 ...
日期計算器
class date return this 賦值操作已經完成,為什麼有個返回值?因為賦值運算子的過載支援三個數,i j k k先賦給j後有乙個返回值j,將這個返回值賦給i,返回的是同型別物件,所以型別為date 但此時如果沒有給函式型別加引用,就是傳值返回,不會直接返回,會建立乙個臨時物件。會多一...