摘要:試題六(共15分)閱讀以下說明和C++代碼,將應(yīng)填入(n)處的字句寫在答題紙的對(duì)應(yīng)欄內(nèi)。[說明]傳輸門是傳輸系統(tǒng)中的重要裝置。傳輸門具有Open(打開)、Closed(關(guān)閉)、Opening(正在打開)、Stay0pen(保持打開)、Closing(正在關(guān)閉)五種狀態(tài)。觸發(fā)傳輸門狀態(tài)轉(zhuǎn)換的事件有click,complete和timeout三種。事件與其相應(yīng)的狀態(tài)轉(zhuǎn)換如圖6-1所示。
試題六(共15分)
閱讀以下說明和C++代碼,將應(yīng)填入 (n) 處的字句寫在答題紙的對(duì)應(yīng)欄內(nèi)。
[說明]
傳輸門是傳輸系統(tǒng)中的重要裝置。傳輸門具有Open(打開)、Closed(關(guān)閉)、Opening(正在打開)、Stay0pen(保持打開)、Closing(正在關(guān)閉)五種狀態(tài)。觸發(fā)傳輸門狀態(tài)轉(zhuǎn)換的事件有click, complete和timeout三種。事件與其相應(yīng)的狀態(tài)轉(zhuǎn)換如圖6-1所示。
下面的[C++代碼1]與[C++代碼2]分別用兩種不同的設(shè)計(jì)思路對(duì)傳輸門進(jìn)行狀態(tài)模擬,請(qǐng)?zhí)钛a(bǔ)代碼中的空缺。
[C++代碼1]
const int CLOSED = 1; const int OPENING = 2;
const int OPEN = 3; const int CLOSING = 4;
const int STAYOPEN = 5;//定義狀態(tài)變量,用不同整數(shù)表示不同狀態(tài)
class Door{
private:
int state;//傳輸門當(dāng)前狀態(tài)
void setState(int state){this->state = state;}//設(shè)置當(dāng)前狀態(tài)
public:
Door():state (CLOSED){}:
void getState(){//根據(jù)當(dāng)前狀態(tài)輸出相應(yīng)的字符串
switch(state){
case OPENING: cout<<""OPENING""<<endl;break;
case CLOSED: cout<<("CLOSED"<<endl;break;
case OPEN: cout<<"OPEN"<<endl;break;
case CLOSING: cout<<"CLOSING"<<endl;break;
case STAYOPEN: cout<<"STAYOPEN"<<endl;break;
}
}
void click(){//發(fā)生click事件時(shí)進(jìn)行狀態(tài)轉(zhuǎn)換
if( (1) ) setState(OPENING);
else if( (2) )setState(CLOSING);
else if( (3) )setState(STAYOPEN);
}
void timeout(){//發(fā)生timeout事件時(shí)進(jìn)行狀態(tài)轉(zhuǎn)換
if(state == OPEN)setState(CLOSING);
}
void complete(){//發(fā)生complete事件時(shí)進(jìn)行狀態(tài)轉(zhuǎn)換
if(state == OPENING)setState(OPEN):
else if(state == CLOSING)setState(CLOSED);
}
};
int main(){
Door aDoor;
aDoor. getState(); aDoor. c1ick(); aDQor.getState(); aDoor. Complete();
aDoor.getState(); aDoor. click(); aDoor.getState();aDoor. cIick();
aDoor. getState(); return 0;
}
[C++代碼2]
class Door{
public:
DoorState *CLOSED, *OPENING,*OPEN, *CLOSING, *STAYOPEN, *state:
Door():
virtual ~Door(){……//釋放申請(qǐng)的內(nèi)存,此處代碼省略};
void setState(DoorState *state){this->state = state;}
void getState(){
//此處代碼省略,本方法輸出狀態(tài)字符串,
//例如,當(dāng)前狀態(tài)為CLOSED時(shí),輸出字符串為"CLOSED"
};
void click();
void timeout();
void complete();
};
Door::Door(){
CLOSED = new DoorClosed(this); OPENING = new DoorOpening(this);
OPEN = new DoorOpen(this); CLOSING = new DoorClosing(this);
STAYOPEN = new DoorStayOpen(this);state = CLOSED:
}
void Door::click(){ (4) ;}
void Door::timeout(){ (5) ;}
void Door::complete(){ (6) ;}
class DoorState//定義一個(gè)抽象的狀態(tài),它是所有狀態(tài)類的基類
{
protected:Door *door;
public:
DoorState(Door *door){this->door = door;}
virtual ~DoorState(void);
virtual void click(){}
virtual void complete(){}
virtual void timeout(){}
};
class DoorClosed : Public DoorState{//定義一個(gè)基本的Closed狀態(tài)
public:
DoorClosed(Door *door): DoorState(door){}
Virtual ~DoorClosed(){}
void click();
};
void DoorClosed::click(){ (7) ;}
//其它狀態(tài)類的定義與實(shí)現(xiàn)代碼省略
int main(){
Door aDoor;
aDoor.getState();aDoor.click(); aDoor.getState();aDoor.complete();
aDoor.getState();aDoor.timeout();aDoor.getState();return 0;
}
[答案討論]
軟考備考資料免費(fèi)領(lǐng)取
去領(lǐng)取
共收錄117.93萬道題
已有25.02萬小伙伴參與做題