软件设计师案例分析当天每日一练试题地址:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4
往期软件设计师每日一练试题汇总:www.cnitpm.com/class/27/e4_1.html
软件设计师案例分析每日一练试题(2024/2/4)在线测试:www.cnitpm.com/exam/ExamDayAL.aspx?t1=4&day=2024/2/4
点击查看:更多软件设计师习题与指导
软件设计师案例分析每日一练试题内容(2024/2/4)
试题六
阅读以下说明和C++代码。
【说明】
传输门是传输系统中的重要装置。传输门具有Open(打开)、Closed(关闭)、Opening (正在打开)、StayOpen(保持打开)和Closing(正在关闭)五种状态。触发传输门状态转换的事件有click、complete和timeout三种。事件与其相应的状态转换如下图所示。
下面的C++代码1与C++代码2分别用两种不同的设计思路对传输门进行状态模拟,请填补代码中的空缺。
【C++代码1】
const int CLOSED=1; const int OPENING=2;
const int OPEN=3; const int CLOSING=4;
const int STAYOPEN=5; //定义状态变量,用不同整数表示不同状态
class Door {
Private:
int state; //传输门当前状态
void setState(int state){ this->state=state;} //设置当前状态
public:
Door():state(CLOSED.{};
void getState(){ //根据当前状态输出相应的字符串
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() { //发生click事件时进行状态转换
if ( (1) ) setState(OPENING.;
else if ( (2) ) setState(CLOSING.;
else if ( (3) ) setState(STAYOPEN);
}
void timeout(){ //发生timeout事件时进行状态转换
if (state == OPEN) setState(CLOSING.;
}
void complete(){ //发生complete事件时进行状态转换
if (state == OPENING. setState(OPEN);
else if (state == CLOSING. setState(CLOSED.;
}
};
int main(){
Door aDoor;
aDoor.getState();aDoor.click(); aDoor.getState();
aDoor.complete();aDoor.getState(); aDoor.click();
aDoor.getState();aDoor.click(); aDoor.getState(); return 0;
}
【C++代码2】
class Door {
public:
DoorState *CLOSED,*OPENING,*OPEN,*CLOSING,*STAYOPEN,*state;
Door();
virtual~Door(){……//释放申请的内存,此处代码省略);
void setState(DoorState *state) { this->state = state;}
void getState(){
//此处代码省略,本方法输出状态字符串,
//例如,当前状态为CLOSED时,输出字符串为“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//定义一个抽象的状态,它是所有状态类的基类
{
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{//定义一个基本的Closed状态
public:
DoorClosed(Door *door) :DoorState(door) {}
virtual ~ DoorClosed(){}
void click();
};
void DoorClosed :: click() { (7) ;}
//其他状态类的定义与实现代码省略
int main() {
Door aDoor;
aDoor.getState();aDoor.click();aDoor.getState();aDoor.complete();
aDoor.getState();aDoor.timeout();aDoor.getState();return 0;
}
信管网考友试题答案分享:
信管网cnitpm488202624972:
1.state == closed || state == closing
2.state == opening || state == stayopen
3.state == open
信管网cnitpm475547856609:
state==closed
state==stayopen‖state==opening
state==open
state->click()
state->timeout()
state->complete()
信管网订阅号
信管网视频号
信管网抖音号
温馨提示:因考试政策、内容不断变化与调整,信管网网站提供的以上信息仅供参考,如有异议,请以权威部门公布的内容为准!
信管网致力于为广大信管从业人员、爱好者、大学生提供专业、高质量的课程和服务,解决其考试证书、技能提升和就业的需求。
信管网软考课程由信管网依托10年专业软考教研倾力打造,教材和资料参编作者和资深讲师坐镇,通过深研历年考试出题规律与考试大纲,深挖核心知识与高频考点,为学员考试保驾护航。面授、直播&录播,多种班型灵活学习,满足不同学员考证需求,降低课程学习难度,使学习效果事半功倍。
发表评论 查看完整评论 | |