C期末考试试题及答案.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《C期末考试试题及答案.pdf》由会员分享,可在线阅读,更多相关《C期末考试试题及答案.pdf(12页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、C+期末考试试题及答案20 0 2年春季软件学院C+期末考试试题及答案考试时间:1 20 分钟1.(1 0)分现有两种实体:“矩 形 (Re ct an g le)和“旗杆”(F lag p o le)其中,“矩形”的属性为长度与宽度,“旗杆”的属性为高度(指地面以上)和深度(指地面以下),且均为整数。若有:s t r u ct Re ct an g le in t le n g t h,w id t h;s t r u ct F lag p o le in t he ig ht,d e p t h;in t r e ct an g le A r e a(in t x,in t y)r e t
2、 u r n (x =0&y =0)?x*y:0;计算矩形的面积in t f lag p o le L e n g t h(in t x,in t y)r e t u r n (x =0&y =0)?x+y:0;计算旗杆的高度v o id f ()(s t r u ct Re ct an g le r e ct;s t r u ct F lag p o le f I g p;r e ct.le n g t h=20;r e ct.w id t h=5;f lg p.he ig ht=20;f lg p.d e p t h=5;in t ar e a=r e ct an g le A r e a
3、(r e ct.le n g t h,r e ct.w id t h);in t le n g t h=f lag p o le L e n g t h(f lg p.he ig ht,f lg p.d e p t h);)试续写函数f,以各举一例说明,对函数r e ct an g le A r e a和 f lag p o le L e n g t h的调用都可能出现语法上合法,但结果与函数的语义不一致的情况,并指出其原因。答案:续写的二句如下:ar e a=r e ct an g le A r e a(f I g p.he ig ht,f lg p.d e p t h);le n g t
4、h=f lag p o le L e n g t h(r e ct.le n g t h,r e ct.w id t h);原因:在函数调用时,只需实参和形参类型相容即可,而不管语义如何?如上例二句语法上是合法的,但结果与函数的语义不一致。若欲使该情况不发生,必须将数据及其上的操作绑定在一起,这必须用类来定义。2.(1 5 分)阅读下面的程序,写钳m ain 的输出。#in clu d e s t d io.hin t f(in t i,co n s t in t*j,in t&k)if(i%3=0)if(*j=6)k*=i;k+=i;k-=i;r e t u r n *j+k;)in t m
5、 ain()(in t i,j=l,k=2,m;f o r(i=0;i 1 0;i+,j*=2)m=f (i,&j,k);1p r in t f C i=%2d,j=%2d,k=%2d,m=%2d.n ,i,j,k,m);r e t u r n 0;答案:i=0,j=l,k=2,m=3.i=l,j=2,k=2,m=4.i=2,j=4,k=2,m=6.i=3,j=8,k=6,m=1 4.i=4,j=1 6,k=6,m=22.i=5,j=32,k=6,m=38.i=6,j=64,k=36,m=1 0 0.i=7,j=1 28,k=36,m=1 64.i=8,j=256,k=36,m=29 2.i=
6、9,j=51 2,k=324,m=8 36.3.(1 5 分)根据m ain 程序的输出结果,定义并实现m ax 函数。in t m ain()(co u t m ax=m ax(l.0,67.3)e n d l;/m ax=67.3 co u t “m ax=m ax(1 0 2,9 0)e n d l;/m axz:1 0 2 co u t “m ax=m ax(a,z )e n d l;/m ax 二 zco u t m ax=m ax(“abc”,bd )e n d l;/m ax=bd co u t “m ax=m ax(50,8 2.3)e n d l;/m ax=8 2.3 co
7、 u t “m ax 二 m ax (1.0,21.5,8 7.4)e n d l;/m ax=8 7.4co u t m ax=”m ax(“g h ,t y e ,z s )e n d l;/m ax=z sco u t m ax=m ax(23,7,62)e n d l;/m ax=62 r e t u r n 0;)(假定所有的赋值、比较运算符都一定义)答案:t e m p lat e T m ax (T x,T y)r e t u r n x =y?x:y;t e m p lat e T m ax (T x,T y,T z)T t e m p;t e m p=m ax(x,y);r
8、e t u r n t e m p =z?t e m p:z;)f lo at m ax(in t x,f lo at y)2 r e t u r n x =y?x:y;4.(20 分)定义堆栈类模板St ack,栈的大小由使用者确定。要求该类模板对外提供如下二种基本操作:(1)p u s h(2)p o p(假定赋值操作已定义)答案:#in clu d e t e m p lat e clas s St ackT s s iz e;in t iC u r r e n t E le m;p u blic:St ack();bo o l p u s h(T x);T p o p ();v o i
9、d p r in t()f o r(in t i=0;i s iz e;i+)co u t s i e n d l;);t e m p lat e St ack:St ack()f o r(in t i=0;i s iz e;i+)s i=0;iC u r r e n t E le m=-l;)t e m p lat e bo o l St ack:p u s h(T x)if (iC u r r e n t E le m=(s iz e-1)co u t z/Tt,s f u ll.z,e n d l;r e t u r n f als e;e ls e s+iC u r r e n t E
10、 le m=x;r e t u r n t r u e;)t e m p lat e T St ack:p o p()T t e m p;if (iC u r r e n t E le m=-l)co u t I t s e m p t y.?z e n d l;r e t u r n -1;e ls e t e m p=s iC u r r e n t E le m;s iC u r r e n t E le m=0;iC u r r e n t E le m-;r e t u r n t e m p;v o id m ain()(f lo at t;St ack s i;s i.p u s
11、 h(1 0);s i.p u s h(20);s i.p u s h(30);3s i.p u s h(40);s i.p r in t 0;t=s l.p o p();s i.p r in t ();co u t t e n d l;)5.(25 分)用C+语言定义M y St r in g (包括成员函数的实现代码),使之能符合下面程序及在注释中描述的运行结果的要求:m ain ()M y St r in g s i=0 1 234567 8 9”,s 2(5),s 3,s 4(s l);s i.d is p lay ();/此时显示出:s 2.d is p lay ();/此时显示出(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 期末考试 试题 答案
![提示](https://www.deliwenku.com/images/bang_tan.gif)
限制150内