niusouti.com

阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】设计一个日期类Date包括年、月、日等私有数据成员。要求实现日期的基本运算,如某日期加上天数、某日期减去天数、两日期相差的天数等。在Date类中设计如下重载运算符函数:Date operator + (int days) : 返回某日期加上天数得到的日期。Date operator - (int days) : 返回某日期减去天数得到的日期。int operator - (Date&b): 返回两日期相差的天数。【程序】include<

题目

阅读以下说明和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) I 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 yea;}

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;


相似考题
更多“阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】 设计一个日期类Date包括年、月、 ”相关问题
  • 第1题:

    试题三(共 15 分)

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


    正确答案:

  • 第2题:

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

    【C++代码】 #include using namespace std; class invoice{ public: (1){ cout<<"This is the content of the invoice!"<

    答案:
    解析:
    (1) virtual void printInvoice() (2) ticket->printInvoice() (3) Decorator::printInvoice() (4) Decorator::printInvoice() (5) &a
    【解析】

    试题分析
    1.Invoice类下,义虛函数,按类图,函数名是printInvoice
    2.前面定义对象名是ticket,那么在ticket不为空的时候调用函数printInvoice
    3.这部分填写发票的抬头,看类图应该实现函数printInvoice ,Decorator装饰模式使用该方法
    4.这部分是发票的脚注,看类图应该实现函数printlnvoice,Decorator装饰模式使用该方法
    5.FootDecorator a(NULL) ;脚步的装饰参数是a,调用a参数,

  • 第3题:

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



    答案:
    解析:
    1: void update()2: Observer3: obs.update()4: Subject5: Attach(this)

  • 第4题:

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

    【C++ 代码】#includeusing namespace std;class BrakeBehavior{public:(1) ; /*其余代码省略*/};class LongWheelBrake : public BrakeBehavior{public:void stop(){cout <<"模拟长轮胎刹车痕迹! "<< end1;} /*其余代码省略*/};class ShortWheelBrake : public BrakeBehavior{public:void stop(){cout<"模拟短轮胎刹车痕迹! "<< end1;} /*其余代码省略*/};class Car{protected: (2) wheel;public:void brake(){ (3) ; } /*其余代码省略*/};class ShortWheelCar : public Car{public:ShortWheelCar(BrakeBehavior* behavior){ (4);} /*其余代码省略*/};int main(){BrakeBehavior* brake= new ShortWheelBrake();ShortWheelCar car1(brake):car1. (5) ;return 0;}


    答案:
    解析:
    1. virtual void stop( )=02. BrakeBehavior*3. Wheel->stop( )4. wheel=behavior5. brake( )

  • 第5题:

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




    【问题1】(15分)阅读上述说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。


    答案:
    解析:
    注意:原版的题目应该是Cbasic、CSilve。(1) virtual double travel(int miles,FrequentFlyer* context)=0(2)context->setState(context->Cbasic)(3)context->setState(context->CSilve)(4)context->setState(context->Cbasic)(5)context->setState(context->CSilve)

  • 第6题:

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






    答案:
    解析: