网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

●试题八

阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。

【说明】

设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。

在Date类中设计如下重载运算符函数:

Date operator+(int days):返回某日期加上天数得到的日期。

Date operator-(int days):返回某日期减去天数得到的日期。

int operator-(Date&b):返回两日期相差的天数。

【程序】

#include<iostream.h>

int day tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},

{31,29,31,30,31,30,31,31,30,31,30,31}};

∥day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年class Date

{

int year,month,day;∥年,月,日

int leap(int);∥判断是否为闰年

int dton(Date&);

Date ntod(int);

public:

Date(){}

Date(int y,int mint d){year=y;month=m;day=d;}

void setday(intd){day=d;}

void setmonth(int m){month=m;}

void setyear(int y){year=y;}

int getday(){return day;}

int getmonth(){return month:}

int getyear(){return year;)

Date operator+(int days)∥+运算符重载函数

{

static Date date;

int number= (1) ;

date=ntod(number);

return date;

}

Date operator-(int days)∥-运算符重载函数

{

staffs Date date;

int number= (2) ;

number-=days;

date=ntod(number);

return date;

}

int operator-(Date &b)∥-运算符重载函数

{

int days= (3) ;

return days;

}

void disp()

{

cout<<year<<"."<<month<<"."<<day<<endl;

}

};

int Date::leap(int year)

{if( (4) )∥是闰年

return 1;∥不是闰年

else

return0:

}

int Date::dton(Date &d)∥求从公元0年0月0日到d日期的天数

{

inty,m,days=0;

for(y=1;y<=d.year;y++)

if( (5) )days+=366;∥闰年时加366天

else days+=365;∥非闰年时加365天

for(m=0;m<d.month-1;m++)

if( (6) )

days+=day_tab[1][m];

else

days+=day_tab[0][m];

days+=D.day;

return days;

}

Date Date::ntod(intn)∥将从公元0年0月0日的天数转换成日期

{

int y=1,m=1,d,rest=n,lp;

while (1)

{if(leap(y))

if(rest<=366)break;

else rest-=366;

else∥非闰年

if(rest=365)break;

else rest-=365;

y++;

}

y--;

Ip=Ieap(y);

while (1)

{

if(Ip)∥闰年

if(rest>day_tab[1][m-1])rest-=day_tab[1][m-1];

else break;

else∥非闰年

if(rest>day_tab[0][m-1])rest-=day_tab[0][m-1];

else break;

m++;

}

d=rest;

return Date(y;m,d);

}

void main()

{

Date now(2003,10,1),then(2005,6,5);

cout<<"now:";now.disp();

cout<<"then:";then.disp();

cout<<"相差天数:"<<(then-now)<<endl;

Date dl=now+1000,d2=now-1000;

cout<<"now+1000:";d1.disp();

cout<<"now-1000:":d2.disp();

}


参考答案

更多 “ ●试题八阅读以下说明和C++程序,将应填入(n)处的字句写在答题纸的对应栏内。【说明】设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。在Date类中设计如下重载运算符函数:Date operator+(int days):返回某日期加上天数得到的日期。Date operator-(int days):返回某日期减去天数得到的日期。int operator-(Date&b):返回两日期相差的天数。【程序】#includeiostream.hint day tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};∥day_tab二维数组存放各月天数,第一行对应非闰年,第二行对应闰年class Date{int year,month,day;∥年,月,日int leap(int);∥判断是否为闰年int dton(Date&);Date ntod(int);public:Date(){}Date(int y,int mint d){year=y;month=m;day=d;}void setday(intd){day=d;}void setmonth(int m){month=m;}void setyear(int y){year=y;}int getday(){return day;}int getmonth(){return month:}int getyear(){return year;)Date operator+(int days)∥+运算符重载函数{static Date date;int number= (1) ;date=ntod(number);return date;}Date operator-(int days)∥-运算符重载函数{staffs Date date;int number= (2) ;number-=days;date=ntod(number);return date;}int operator-(Date &b)∥-运算符重载函数{int days= (3) ;return days;}void disp(){coutyear"."month"."dayendl;}};int Date::leap(int year){if( (4) )∥是闰年return 1;∥不是闰年elsereturn0:}int Date::dton(Date &d)∥求从公元0年0月0日到d日期的天数{inty,m,days=0;for(y=1;y=d.year;y++)if( (5) )days+=366;∥闰年时加366天else days+=365;∥非闰年时加365天for(m=0;md.month-1;m++)if( (6) )days+=day_tab[1][m];elsedays+=day_tab[0][m];days+=D.day;return days;}Date Date::ntod(intn)∥将从公元0年0月0日的天数转换成日期{int y=1,m=1,d,rest=n,lp;while (1){if(leap(y))if(rest=366)break;else rest-=366;else∥非闰年if(rest=365)break;else rest-=365;y++;}y--;Ip=Ieap(y);while (1){if(Ip)∥闰年if(restday_tab[1][m-1])rest-=day_tab[1][m-1];else break;else∥非闰年if(restday_tab[0][m-1])rest-=day_tab[0][m-1];else break;m++;}d=rest;return Date(y;m,d);}void main(){Date now(2003,10,1),then(2005,6,5);cout"now:";now.disp();cout"then:";then.disp();cout"相差天数:"(then-now)endl;Date dl=now+1000,d2=now-1000;cout"now+1000:";d1.disp();cout"now-1000:":d2.disp();} ” 相关考题
考题 阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写在对应栏内。【说明】阅读下面几段C++程序回答相应问题。比较下面两段程序的优缺点。①for (i=0; i<N; i++ ){if (condition)//DoSomething…else//DoOtherthing…}②if (condition) {for (i =0; i<N; i++ )//DoSomething}else {for (i=0; i <N; i++ )//DoOtherthing…}

考题 ●试题二阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】该程序运行后,输出下面的数字金字塔【程序】includestdio.hmain (){char max,next;int i;for(max=′1′;max=′9′;max++){for(i=1;i=20- (1) ;++i)printf(" ");for(next= (2) ;next= (3) ;next++)printf("%c",next);for(next= (4) ;next= (5) ;next--)printf("%c",next);printf("\n");}}

考题 试题三(共 15 分)阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。

考题 图2-1是基于软交换的网络分层模型。请将选项应填入(n)处的字句写在答题纸对应的解答栏内。

考题 (a)智能网概念模型中分布功能平面模型如下图所示,请根据此图将应填入(n)处的 字句写在答题纸的对应栏内。

考题 阅读下列说明和 C ++代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】某软件公司欲开发一款汽车竞速类游戏,需要模拟长轮胎和短轮胎急刹车时在路面上留 下的不同痕迹,并考虑后续能模拟更多种轮胎急刹车时的痕迹。现采用策略(Strategy)设计模式来实现该需求,所设计的类图如图 6-1 所示。 【C++ 代码】#includeusing namespace std;class BrakeBehavior{public:(1) ; /*其余代码省略*/};class LongWheelBrake : public BrakeBehavior{public:void stop(){cout

考题 阅读下列说明和C++-代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。 【C++代码】 #include using namespace std; class invoice{ public: (1){ cout

考题 阅读下列说明和C++代码,回答问题,将解答填入答题纸的对应栏内。 【说明】某航空公司的会员积分系统将其会员划分为:普卡 (Basic)、银卡(Silver)和金卡 (Gold) 三个等级。非会员 (NonMember) 可以申请成为普卡会员。会员的等级根据其一年内累积 的里程数进行调整。描述会员等级调整的状态图如图 5-1 所示。现采用状态 (State) 模式实现上述场景,得到如图 5-2 所示的类图。 【问题1】(15分)阅读上述说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

考题 阅读下列说明和Java代码,将应填入(n)处的字句写在答题纸的对应栏内。【说明】 某文件管理系统中定义了类OfficeDoc和DocExplorer,当类OfficeDoc发生变化时,类DocExplorer的所有对象都要更新其自身的状态,现采用观察者(Observer)设计模式来实现该需求,所设计的类图如图6-1所示。

考题 阅读下列说明和?C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 阅读下列说明和?Java代码,将应填入?(n)?处的字句写在答题纸的对应栏内。 【说明】 某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种 类可能不同,但其制作过程相同。前台服务员?(Waiter)?调度厨师制作套餐。现采用生成器?(Builder)?模式实现制作过程,得到如图?6-1?所示的类图。