資源簡介
C程序設(shè)計教程第二版答案錢能.zip

代碼片段和文件信息
//?Fig.?11.13:?Date.cpp
//?Date?class?member?function?definitions.
#include?
#include?“Date.h“
//?initialize?static?member?at?file?scope;?one?classwide?copy
const?int?Date::days[]?=?
{?0?31?28?31?30?31?30?31?31?30?31?30?31?};
//?Date?constructor
Date::Date(int?m?int?d?int?y)
{
setDate(?m?d?y?);
}?//?end?Date?constuctor
//?set?month?day?and?year
void?Date::setDate(?int?mm?int?dd?int?yy?)
{
month?=?(?mm?>=?1?&&?mm?<=?12?)???mm?:?1;
year?=?(?yy?>=?1900?&&?yy?<=?2100?)???yy?:?1900;
//?test?for?a?leap?year
if?(?month?==?2?&&?leapYear(?year?)?)
day?=?(?dd?>=?1?&&?dd?<=?29?)???dd?:?1;
else
day?=?(?dd?>=?1?&&?dd?<=?days[?month?]?)???dd?:?1;
}?//?end?function?setDate
//?overloaded?prefix?increment?operator
Date?&Date::operator++()
{
helpIncrement();?//?increment?date
return?*this;?//?reference?return?to?create?an?lvalue
}?//?end?function?operator++
//?overloaded?postfix?increment?operator;?note?that?the
//?dumy?integer?parameter?does?not?have?a?parameter?name
Date?Date::operator++(?int?)
{
Date?temp?=?*this;?//?hold?current?state?of?object
helpIncrement();
//?return?unincremented?saved?temporary?object
return?temp;?//?value?return;?not?a?reference?return
}?//?end?function?operator++
//?add?specified?number?of?days?to?date
const?Date?&Date::operator?+=(?int?additionalDays?)
{
for?(?int?i?=?0;?i? helpIncrement();
return?*this;?//?enables?cascading
}?//?end?function?operator+=
//?if?the?year?is?a?leap?year?return?true;?otherwise?return?false
bool?Date::leapYear(?int?testYear?)?const
{
if?(?testYear?%?400?==?0?||
(?testYear?%?100?!=?0?&&?testYear?%?4?==?0)?)
return?true;?//?a?leap?year
else
return?false;?//?not?a?leap?year
}?//?end?function?leapYear
//?determine?whether?the?day?is?the?last?day?of?the?month
bool?Date::endOfMonth(?int?testDay?)?const
{
if?(?month?==?2?&&?leapYear(?year?)?)
return?testDay?==?29;?//?last?day?of?Feb.?in?leap?year
else
return?testDay?==?days[?month?];
}?//?end?function?endOfMonth
//?function?to?help?increment?the?date
void?Date::helpIncrement()
{
//?day?is?not?end?of?month
if?(?!endOfMonth(?day?)?)
day++;?//?increment?day
else?//?last?day?of?year
{
year++;?//?increment?year
month?=?1;?//?first?month?of?new?year
day?=?1;?//?first?day?of?new?month
}?//?end?else
}?//?end?function?helpIncrement
//?overload?output?operator
ostream&?operator<<(ostream&?output?const?Date?&d)
{
static?char?*monthName[?13?]?=?{?““?“January“?“Februray“
“March“?“April“?“May“?“June“?“July“?“August“
“September“?“Oectober“?“November“?“December“?};
output?< return?output;?//?enables?cascading
}?//?end?function?operator<<
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????505??2008-05-05?20:31??ex0601.cpp
?????文件????????614??2008-05-07?06:09??prime.cpp
?????文件????????271??2008-05-07?06:09??prime.h
?????文件?????????16??2008-04-27?16:08??prime.txt
-----------?---------??----------?-----??----
?????????????????1406????????????????????4
- 上一篇:軟件項目管理進度表excel
- 下一篇:fpga峰值采樣verilog程序
評論
共有 條評論