问答题

【说明】 在一公文处理系统中,开发者定义了一个公文结构OfficeDoc,其中定义了公文应该具有的属性。当公文的内容或状态发生变化时,与之相关联的DocExplorer结构的值都需要发生改变。一个OfficeDoc结构能够关联一组DocExplorer结构。当OfficeDoc结构的内容或状态发生变化时,所有与之相关联的DocExplorer结构都将被更新,这种应用被称为观察者模式。以下代码采用C语言实现,能够正确编译通过。 【代码13-4】 # include<stdio.h> # define OBS_MAXNUM 20 /*一个OfficeDoc变量最多能够关联的DocExplorer变量的个数*/ typedef void( (1) )(struc OffieeDoc*, struct DoeExplorer*)I; struct DocExplorer{ func update;/*DocExplorer结构采用的更新函数*/ /*其它的结构字段省略*/ }; struet OffieeDoc{ (2) myObs[OBS_MAXNUM
; /*存储所有与OfficeDoc相关联的DocExplorer结构指针*/ int index;/*与OffieeDoc结构变量相关联的DoeExplorer结构变量的个数*/ }; void attaeh(struct OfficeDoc*doc, struct DocExplorer*ob){ /*关联Observer结构ob与OffieeDoe结构doe*/ int loop=0; if(doc->index>=OBS_MAXNUM||ob==NULL)return; for(loop=0, loop<doc->index; loop++) if(doc->myObs[loop]==ob)return; doc->myObs[doe->index]=ob; doc->index++; } void detaeh(struct OfficeDoc*doc, struct DocExplorer*ob){ /*解除doc结构与ob结构间的关联*/ int loop; if(ob==NULL)return; for(loop=0;loop<doc->index; loop++){ if(doe->myObs[loop]==ob){ if(loop<=doc->index-2) doc->myObs[loop]=doc->myObs[ (3) ]; doc->myObs[doc->index-1]=NULL; doc->index——; breack; } } } void updatel(struct OfficeDoe*doe, struct DoeExplorer *ob){ /*更新ob结构的值,更新代码省略*/ } void update2(struct OffieeDoc*doc,struet DocExplorer *ob){ /*更新ob结构的值,更新代码省略*/ } void notifyObs(struct OfficeDoc* doc){ /*当doc结构的值发生变化时,通知与之关联的所有DocExplorer结构变量*/ int loop; for(loop=0; loop<doc->index; loop++){ (doc->myObs[loop
)->update( (4) ); } } void main(){ struct OfficeDoc doc; /*定义一了OfficeDoe变量*/ struct DocExplorer explorer1, explorer2; /*定义两个DocExplorer变量*/ /*初始化与OfficeDoc变量相关的DocExplorer变量个数为0*/ doc.index=0; explorer1.update=update1; /*设置explorer1变量的更新函数*/ explorer2. update=update2; /*设置explorer2变量的更新函数*/ attach(&doc, &explorer1); /*关联explorer1与doc对象*/ attach(&doc, &explorer2); /*关联explorer2与doc对象*/ /*其它代码省略*/ (5) ; /*通知与OfficeDoe相关的所有DoeExploer变量*/ return; }

【参考答案】

(1)*func (2)struct DocExplorer* (3)doc->index-1,或等价形式(4)doc......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
【说明】软件设计师东方飞龙利用UML设计了一个迷你小型复数类,其类图如图13-11所示。 【代码13-l】 *___________________________________* ********* 文件 MiniComplex. h********* *___________________________________* #include<iostream>using namespace std;class MiniComplex{ (1) : 重载流插入和提取运算符(2) ostream & operator <<(ostream & osObject, const MiniComplex & complex){ osObject << ( <<complex. realPart<< + <<complex. imagPart << I << ) ;return osObject;}friend (3) operator >>(istream & isObject, MiniComplex & complex){ char ch;isObject >>complex. realPart >>ch>>complex. imagPart >>ch;return isObject;}MiniComplex(double real=0, double imag=0); 构造函数MiniComplex operator+(const MiniComplex & otherComplex)const! 重载运算符+MiniComplex operator--(const MiniComplex & otherComplex)const! 重载运算符-MiniComplex operator*(const MiniComplex& othmComplex)const; 重载运算符*MiniComplex operator (const MiniComplex & otherComplex)const; 重载运算符 bool operator==(const MiniComplex &otherComplex)const; 重载运算符==private:double realPart; 存储实部变量double imagPart; 存储虚部变量}; *_______________________________________________________* * * * * * * * * *文件 MiniComplex. cpp* * * * * * * * * * *_______________________________________________________* # include MiniComplex.h bool MiniComplex:: operator==(const MiniComplex & otherComplex)const{ (1) ;}MiniComplex:: MiniComplex(double real, double imag){realPart=real;imagPart=imag!}MiniComplex MiniComplex:: operator+(const MiniComplex & otherComplex)const{ MiniComplex temp;temp. realPart=realPart+ otherComplex. realPart;temp. imagPart=imagPart+ otherComplex. imagPart;return temp;}MiniComplex MiniComplex::operator--(const MiniComplex & otherComplex)const{ MiniComplex temp;temp.realPart=realPart-otherComplex.realPart;temp. imagPart=imagPart-otherCompler.imagPart;return temp;}MiniComplex MiniComplex:: operator*(const MiniComplex& otherComplex)const{ MiniComplex temp;temp.realPart=(realPart* otherComplex.realPart)-(imag-Part* otherComplex.imag-Part);temp imagPart=(realPart* otherComplex. imagPart)+(imag-Part *otherComplex.realPart);return temp,}MiniComplex MiniComplex:: operator (const MiniComplex& otherComplex)eonst{ MiniComplex temp;float tt;tt=1 (otherComplex. realPart *otherComplex. realPart+otherComplex. imagPart* other Complex.imagPart);temp. realPart=((realPart* otherComplex.realPart)+(imagPart* otherComplex.imagPart))*tt;temp. imagPart=((imagPart * otherComplex.realPart)-(realPart* otherComplex.imagPart))*tt;return temp;} *__________________________________________________* * * * * * * * *主函数所在文件main.cpp* * * * * * * * *_________________________________________________* # include<iostream># include (5) using namespace std;int main(void){ MiniComplex num1(23, 34), num2;cin>>num2;cout<< Initial Value of Numl= <<num1<< nInitial Value of Num2= <<num2<<end1;cout<<num1<< + <<num2<< = <<num1+num2<<end1; 使用重载的加号运算符cout<<num1<< - <<num2<< = <<num1-num2<<end1; 使用重载的减号运算符cout<<num1<< * <<num2<< - <<num1*num2<<end1; 使用重载的乘号运算符cout<<num1<< <<num2<< = <<num1 num2<<end1; 使用重载的除号运算符return 0;}