1、程序员-C+试题及答案解析(总分:119.00,做题时间:90 分钟)一、B试题一/B(总题数:1,分数:15.00)阅读以下说明和 C+程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序实现了二叉树的结点删除算法,若树中存在要删除的结点,则删除它,否则返回。 FindNode ()函数能够在二叉树中找到给定值的结点,并返回其地址和父结点。【C+程序】template class T void BinSTree T : :Delete( const Tif( DelNodePtr = FindNode (item,ParNodePtr) = = NULL)U(1) /U if(DelN
2、odePtrright = = NULL) /被删除结点只有一个子结点的情况RepNodePtr = DelNodePtrleft;else if( DelNodePtrleft = = NULL)U(2) /U;else / 被删除结点有两个子结点的情况TreeNode T * PofRNodePtr = DelNodePtr;RepNodePtr = DelNodePtrleft;while(RepNodePtrright ! = NULL) /定位左子树的最右结点PofRNodePtr =RepNodePtr;RepNodePtr = RepNodePtrright;if(PofRNod
3、ePtr = = DelNodePtr) /左子树没有右子结点U(3) /U;else /用左子顷的最右结点替换删除的结点U(4) /U RepNodePtrleft = DelNodePtrleft;RepNodePtrright = DelNodePtrright;ifU (5) /U/要删除结点是要结点的情况root = RepNodePtr;else if ( DelNodePtrdata ParNodePtrData)ParNodePtrleft = RepNodePtr;elseParNodePtrright =RepNodePtr;FirstTreeNode ( DelNodeP
4、tr ) ;/释放内存资源size; (分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_二、B试题二/B(总题数:1,分数:15.00)阅读下列说明和 C+程序,将应填入(n)处的字句写在对应栏内。【程序 1说明】程序 1中定义了数组的类模板,该模板使得对于任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息(C+语言本身不提供对下标越界的判断)。【程序 1】#include iostream. h template class T clas
5、s Array2D;template class T class Array2DBody friend U(1) /U;T * tempBody;int iRows, iColumns, iCurrentRow;Array2DBody(int Rows,int Cols) tempBody =U (2) /U;iRows = Rows;iColumns = Cols;iCurrentRow = -1;public:Trow_ error = column_ error = false;try if ( iCurrentRow 0iCurrentRow = iRows)row_ error =
6、true;if( j 0j = iColumns)column_error = true;if( row_error = = true column_ error = = true)U(3) /U;catch(char) if (row_error = = true)cerr “行下标越界“ iCurrentRow “;if( colmnn_error = = true)cerr “列下标越界“ j “;cout “/n“;return tempBody iCurrentRow * iColumns + j ; Array2 DBody ( ) delete tempBody; ; templ
7、ate class T class Array2D Array2DBody T tBody;public:Array2DBody T U(4) /U;Array2D(int Rows,int Cols):U (5) /U ;void main( )Array2D int al ( 10,20 );Array2D double a2(3,5);int bl;double b2;b1=a1-510;/有越界提示:行下标越界-5b1=a11015;/有越界提示:行下标越界10b1=a114;/没有越界提示b2=a226;/有越界提示:列下标越界6b2=a21020;/有越界提示:行下标越界10列下标
8、越界20b2=a214;/没有越界提示(分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_三、B试题三/B(总题数:1,分数:15.00)阅读以下说明和 C+程序,将应填入(n)处的字句写在对应栏内。【C+程序】#include stdio. h #include string. h #define Max 1000class Bankint index;char date Max 10; / 记录交易日iht amountMax; / 记录每次交易金额,以符号区分存钱和取钱int re
9、st Max; / 记录每次交易后余额static iht sum; / 账户累计余额public:Bank( ) index =0;void deposit( char d , int m) /存入交易strcpy ( date index , d);amount index = m;U(1) /U;rest index = sum;index+;void withdraw (char d , int m) /取出交易strcpy( date index ,d);U (2) /U;U (3) /U;rest index = sum;index+;void display( );int Ban
10、k: sum = 0;void Bank: display ( ) /输出流水int i;printf(“日期 存入 取出 余额/n“);forU (4) /Uprintf(“ %8s“ ,datei );if U(5) /Uprintf(“ %6d“ , -amounti );elseprintf( “%6d “,amounti );printf( “% 6d/n“ ,resti ); void main( )Bank object;object. deposit ( “2006.2.5“, 1 00 );object. deposit( “2006.3.2“ , 200);object.
11、withdraw( “2006.4.1“, 50);object. withdraw( “2006.4.5“, 80);object. display ( );本程序的执行结果如下:日期 存入 取出 余额 2006.2.5 100 1002006.3.2 200 3002006.4.1 50 2502006.4.5 80 170(分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_四、B试题四/B(总题数:1,分数:15.00)阅读以下说明和 C+程序,将应填入(n)处的字句写在对应栏内。
12、【说明】以下程序的功能是计算三角形、矩形和正方形的周长并输出。程序由 4个类组成:类 Triangle、Rectangle 和 Square分别表示三角形、矩形和正方形;抽象类 Figure提供了一个纯虚拟函数 getGirth(),作为计算上述 3种图形周长的通用接口。【C+程序】# include iostream. h # include math. h class Figure public:virtual double getGirth() =0; /纯虚拟函数 ;class Rectangle: U(1) /Uprotected:double height;double width
13、;public:Rectangle();Rectangle( double height, double width) thisheight = height;thiswidth = width;double getGirth ( ) return U(2) /U;; class Square: U(3) /U public:Square( double width) U (4) /U; ;class Triangle: U(5) /Udouble la;double lb;double lc;public:Triangle( double la,double lb,double lc)thi
14、sla = la; thisLb = lb; thislc = lc;double getGirth ( ) return la + lb + lc;void main( ) Figure * figures 3 = new Triangle ( 2,3,3 ),new Rectangle (5,8) , new Square U(5) /U;for (inti =0;i3;i+)cout “figures “ i “ girth =“ ( figures i ) getGirth ( ) end1;(分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3
15、.00)_(4).(分数:3.00)_(5).(分数:3.00)_五、B试题五/B(总题数:1,分数:15.00)阅读下列程序说明和 C+程序,把应填入其中(n)处的字句,写对应栏内。【说明】下面的程序实现了类 String的构造函数、析构函数和赋值函数。已知类 String的原型为:class Stringpublic:String(coust char * str = NULL); /普通构造函数String( const String /拷贝构造函数String(void); /析构函数String /赋值函数private:char * m_data; / 用于保存字符串;/Strin
16、g 的析构函数String: String (void)U (1) /U;/String 的普通构造函数String: :String( const char * str)ifU (2) /Um_data = new char1;*m_data = /0;elseint length = strlen(str);m_data = new ehar length + 1 ;strepy(m_data, str);/拷贝的构造函数String: String( const String m_data = new char length + 1 ;strepy(m_data, other, m_da
17、ta); /赋值函数String delete m_clara; /释放原有的内存资源int length = strlen( other, m_data);m_data = new chart length + 1 ;U (4) /U;return U(5) /U;(分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_六、B试题六/B(总题数:1,分数:15.00)阅读以下说明和 C+程序,将应填入(n)处的字句写在对应栏内。【说明】下面程序的功能是计算并输出某年某月的天数,函数 IsL
18、eap Year()能够判断是否是闰年。【C+程序】# include iostream using namespace std;U(1) /U Month Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec ;class Date public:Date( int year, Month m_ month) thisyear = year;if( U(2) /U) month: Jan;else month = m_ month;Date();bool IsLeap Year( ) return (year%4= =0 ;in
19、t CaculateDays( ) switch(m_month ) case U(3) /U;if U(4) /Ureturn 29;else return 28;case Jan: case Mar: case May: case Jul: case Aug: case Oct: case Dec: return 31;case Apr: case Jun: case Sop: case Nov: return 30;private:int year;Month month;void main( ) Date day(2000,Feb);cout day.U (5) /U( );(分数:1
20、5.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_七、B试题七/B(总题数:1,分数:14.00)阅读下列程序说明和 C+程序,把应填入其中(n)处的字句,写在对应栏内。【说明】阅读下面几段 C+程序回答相应问题。(分数:14.00)(1).【问题 1】比较下面两段程序的优缺点。for (i=0; iN; i+ )if (condition)/DoSomethingelse/DoOtherthingif (condition) for (i =0; iN; i+ )/DoSomethingels
21、e for (i=0; i N; i+ )/DoOtherthing(分数:7.00)_(2).【问题 2】 下面的程序各自独立,请问执行下面的四个 TestMemory 函数各有什么样的结果?void GetMemory(char * p)p = (char * )malloc(100);void TestMemory (void)char *str = NULL;GetMemory (str);strcpy(str, “hello world“);prinff(str); char * GetMemory (void)char p = “hello world“;return p;void
22、 TestMemory (void)char * str = NULL;str = GetMemory( );printf(str);void GetMemory(char * * p, int num)* p = (char * )malloc(num);void TestMemory (void)char * str = NULL;GetMemory(strcpy( str, “hello“ );printf(sir);void TestMemory (void)char *str = (char * )malloe(100);strepy (str, “hello“ );free ( s
23、tr );if(str ! = NULL)strepy( str, “world“ );printf(str);(分数:7.00)_八、B试题八/B(总题数:1,分数:15.00)阅读以下说明和 C+代码,将应填入(n)处的字句写在对应栏内。【说明】某网络游戏存在战士(Fighter)、野蛮人(Savage)、白法师(White Witch)三种角色,它们具有 Role接口,角色的类图关系如图 1.1所示。现要将黑法师(BlackWitch)角色加入此网络游戏以实现功能扩充。已知某第三方库已经提供了法师(Witch)类,且完全满足系统新增的黑法师角色所需的功能,但法师 (Witch)不是由 R
24、ole派生而来,它提供的接口不能被系统直接使用。代码 8-1既使用了法师(Witch),又遵循了Role规定的接口,既避免了从头开发一个新的黑法师类,又可以不修改游戏中已经定义的接口。代码 8-2根据用户指定的参数生成特定的角色实例,并对之进行显示操作。游戏中定义的接口与黑法师(BlackWitch)提供的显示接口及其功能见表 1.1。(分数:15.00)(1).(分数:3.00)_(2).(分数:3.00)_(3).(分数:3.00)_(4).(分数:3.00)_(5).(分数:3.00)_程序员-C+试题答案解析(总分:119.00,做题时间:90 分钟)一、B试题一/B(总题数:1,分数
25、:15.00)阅读以下说明和 C+程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序实现了二叉树的结点删除算法,若树中存在要删除的结点,则删除它,否则返回。 FindNode ()函数能够在二叉树中找到给定值的结点,并返回其地址和父结点。【C+程序】template class T void BinSTree T : :Delete( const Tif( DelNodePtr = FindNode (item,ParNodePtr) = = NULL)U(1) /U if(DelNodePtrright = = NULL) /被删除结点只有一个子结点的情况RepNodePtr = D
26、elNodePtrleft;else if( DelNodePtrleft = = NULL)U(2) /U;else / 被删除结点有两个子结点的情况TreeNode T * PofRNodePtr = DelNodePtr;RepNodePtr = DelNodePtrleft;while(RepNodePtrright ! = NULL) /定位左子树的最右结点PofRNodePtr =RepNodePtr;RepNodePtr = RepNodePtrright;if(PofRNodePtr = = DelNodePtr) /左子树没有右子结点U(3) /U;else /用左子顷的最右
27、结点替换删除的结点U(4) /U RepNodePtrleft = DelNodePtrleft;RepNodePtrright = DelNodePtrright;ifU (5) /U/要删除结点是要结点的情况root = RepNodePtr;else if ( DelNodePtrdata ParNodePtrData)ParNodePtrleft = RepNodePtr;elseParNodePtrright =RepNodePtr;FirstTreeNode ( DelNodePtr ) ;/释放内存资源size; (分数:15.00)(1).(分数:3.00)_正确答案:()解析
28、:(2).(分数:3.00)_正确答案:()解析:RepNodePtr=DelNodcPtrright 解析 当要删除结点只有右子结点时,则替代结点就是右子结点。(3).(分数:3.00)_正确答案:()解析:RepNodcPtrright=DelNodePtrright 解析 左子树没有右子结点时,则用要删除结点左子结点就是替代结点,这里将替代结点的右指针指向要删除结点的右子树,完成子树处理。(4).(分数:3.00)_正确答案:()解析:PofRNodePtrright=RcpNodePtrleft 解析 这里将替代结点变成自由结点,即将其父结点的右指针指向它的左子树。(5).(分数:3.
29、00)_正确答案:()解析:ParNOdePtr=NULL 解析 要删除结点的父结点为空,则该结点是根结点。二、B试题二/B(总题数:1,分数:15.00)阅读下列说明和 C+程序,将应填入(n)处的字句写在对应栏内。【程序 1说明】程序 1中定义了数组的类模板,该模板使得对于任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息(C+语言本身不提供对下标越界的判断)。【程序 1】#include iostream. h template class T class Array2D;template class T class Array2DBody
30、friend U(1) /U;T * tempBody;int iRows, iColumns, iCurrentRow;Array2DBody(int Rows,int Cols) tempBody =U (2) /U;iRows = Rows;iColumns = Cols;iCurrentRow = -1;public:Trow_ error = column_ error = false;try if ( iCurrentRow 0iCurrentRow = iRows)row_ error = true;if( j 0j = iColumns)column_error = true;
31、if( row_error = = true column_ error = = true)U(3) /U;catch(char) if (row_error = = true)cerr “行下标越界“ iCurrentRow “;if( colmnn_error = = true)cerr “列下标越界“ j “;cout “/n“;return tempBody iCurrentRow * iColumns + j ; Array2 DBody ( ) delete tempBody; ; template class T class Array2D Array2DBody T tBody
32、;public:Array2DBody T U(4) /U;Array2D(int Rows,int Cols):U (5) /U ;void main( )Array2D int al ( 10,20 );Array2D double a2(3,5);int bl;double b2;b1=a1-510;/有越界提示:行下标越界-5b1=a11015;/有越界提示:行下标越界10b1=a114;/没有越界提示b2=a226;/有越界提示:列下标越界6b2=a21020;/有越界提示:行下标越界10列下标越界20b2=a214;/没有越界提示(分数:15.00)(1).(分数:3.00)_正确答案:()解析:(2).(分数:3.00)_正确答案:()解析:new T Rows,Cols 解析 使用 new对 tempBody进行初始化。(3).(分数:3.00)_正确答案:()解析:throwe 解析 数组出现错误,抛出异常。throw 后面可以填写任意字符常数(4).(分数:3.00)_正确答案:()解析:return tBody 解析 完成初始化后,返回 tBody。(5).(分数:3.00)_