【计算机类职业资格】程序员-C++试题及答案解析.doc
《【计算机类职业资格】程序员-C++试题及答案解析.doc》由会员分享,可在线阅读,更多相关《【计算机类职业资格】程序员-C++试题及答案解析.doc(24页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
5000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机 职业资格 程序员 试题 答案 解析 DOC
