东南大学C++第二学期期末复习二含问题详解文档格式.docx
《东南大学C++第二学期期末复习二含问题详解文档格式.docx》由会员分享,可在线阅读,更多相关《东南大学C++第二学期期末复习二含问题详解文档格式.docx(20页珍藏版)》请在冰豆网上搜索。
\dir\test.txt,下列语句中能以”读文本文件”的方式打开该文件的是 (B) 。
A.ifstreamfile(“d:
\dir\test.txt”);
B.fstreamfile(“d:
\\dir\\test.txt”);
C.fstreamfile(ios:
:
in);
file.open(“d:
D.ifstream*file=newifstream(“d:
6.定义如下变量和数组:
inti;
intx[3][3]={1,2,3,4,5,6,7,8,9};
则以下语句的输出结果是(C)。
for(i=0;
i<
3;
i++)
cout<
x[i][2-i];
A.159B.147C.357D.369
7.voidg((void*)(int,int,int),int,int)的参数个数是(A)。
A.3个B.5个C.1个D.无法确定
8.现需要对list类对象使用的逻辑运算符“==”重载,以下函数声明 (28) 是正确的。
A.list&
list:
operator==(constlist&
a);
B.listlist:
C.bool&
D.boollist:
9.若有说明:
intt[3][2],*p=t[0];
能正确表示t[2][1]的一组表达式是_(D)_
A)**(t+2)+1,*(p[0]+5),**t+3
B)**(t+2+1),*p[2][1],**(t[2]+1)
C)*(t+2)+1,*p[5],*(t[0]+2)+1
D)*(*(t+2)+1),p[5],*(t[2]+1)
10.若有说明:
能正确表示t[0]的一组表达式是_(27)_
A)*(t+0),pB)**t,*p
C)p[0],**tD)**t+0,*p[0]
11.若有说明:
intt[3][2];
则表达式:
&
t[0]+1表示_(B)_
A)第1行的行指针,一级指针B)第1行的行指针,二级指针
C)元素t[0][1]的地址D)元素t[1][0]的地址
12.设有说明:
int*p,*q,*t,x=5;
运行以下程序段,输出结果是_(C)_
A)5随机数B)随机数5C)出错D)55
q=&
x;
t=p;
p=q;
q=t;
deletep;
'
\t'
*q<
二、阅读程序题
1.以下程序运行后,输出结果是
(1)
(2)(3)(4)。
(8分)
4*
24*
#include<
intf(inta,intb){
if(a>
b)returnb+a;
elsecout<
b*f(++a,--b)<
*'
\n'
;
returna*f(a,b);
voidmain(){
intn;
n=f(1,3);
n<
2.程序
#include"
iostream.h"
voidfun(int*s,intn){
intf=10;
1
5
x=25
if(n==1||n==2)*s=1;
else{
fun(&
f,n-2);
*s=f*5;
f<
}
intx=20;
fun(&
x,5);
"
x="
程序输出的第一行是4,第二行是5,第三行是6。
3.程序
iomanip.h>
Doublex,u,v,f(double),g(double),t(double,double(*)(double));
x=4.0;
u=t(x,f);
v=t(x,g);
u="
setw(4)<
u<
v="
v<
doublet(doublea,double(*f)(double)){return(*f)(a*a);
doublef(doublex){returnx+2.0;
doubleg(doublex){returnx-2.0;
程序输出的前2行分别是:
_7,8u=18v=14
4.程序
classbase{
inti;
base(intI=0):
i(I){}
virtualintsum(){returni;
};
classD:
publicbase{
intj;
D(intI=0,intJ=0):
base(I),j(J){}
intsum(){returnbase:
sum()+j;
voidcall(baseb){cout<
sum="
b.sum()<
voidmain()
sum=10
{baseb(10),*pb;
Dd(10,47);
pb=&
d;
call(b);
call(d);
call(*pb);
程度输出的第一行9,是第二行是10第三行是11。
5.
3
7
程序
#include<
intf(intx){
ints=0;
for(intj=2;
j<
=x;
j++)
if(x%j==0){
x/=j;
s+=j;
}
returns;
voidmain(void){
for(inti=3;
10;
if(i==f(i))cout<
程序运行后输出的第一行是12第二行是13第三行是14。
6.程度
voidf(intj){
if(!
j){cout<
j;
return;
if(j%2){f(j/10);
j%10;
else{cout<
f(j/10);
f(0);
cout<
f(1357);
f(2468);
程序运行后输出的第一行是第二行是第三行是
7.程序
classX{
voidvirtualf(){cout<
X:
11"
voidvirtualg(){cout<
22"
B:
55
44
22
classA:
publicX{
voidf(){cout<
A:
33"
classB:
publicA{
44"
voidg(inti=10){cout<
55"
voidmain(void)
{
Bb;
X*px=&
b;
b.g()
px->
f();
px->
g();
程序输出的第一行是,第二行是,第三行是_。
8.程序
classA
x=1A_1
A_2
2~A
3~A
4~A
{intx;
public:
A(inta)
{x=a;
A_1"
A(A&
s){
x=s.x+1;
A_2"
~A(){cout<
~A"
Af1
(1),f2(f1);
f2=A(f1);
f1=A(f2);
程序共输出行,第一行是,第四行是,第六行是。
9.程序
x=4,y=10
x=4,y=16
x=4,y=6
i=34
x=8,y=7
classA{
inty;
staticintx;
operatorint(){returnx+y;
}
Aoperator++(int){returnA(x++,y++);
A(intx=2,inty=3){A:
x=x+x;
y=y+y;
voidprint(){cout<
\t"
y="
intA:
x=23;
{Aa(30,5),b(10,8),c;
a.print();
b.print();
c.print();
inti=a+b;
i="
c++;
程序输出的第一行是,第二行是,第三行是,第四行是,第五行是。
10.#include<
inti=1;
intfac(intn)
{staticintf=1;
6
6,6
f=f*n;
return(f);
{inti,p=0;
for(i=1;
=3;
i++)p=fac(i);
p<
p=0;
i++)p=p+:
i++;
//A
'
p=1;
i++)p=p*--:
i;
//B
问题1:
该程序的第一行输出为(,第二行输出为
11.下列程序的输出结果是。
inta=100;
voidfun(inti,intj,int*k){
j+=i;
*k=j-i;
voidmain(){
inta,b,c;
fun(17,18,&
fun(19,a,&
b);
18,18,18
fun(a,b,&
c);
a<
b<
c<
12.以下程序运行后,输出结果是。
insertA\n"
virtualvoidprint(){cout<
insertB\n"
classC:
publicB{
3insertC
3insertA:
i
10insertA
C(){A:
i=10;
voidprint()
{
insertC\n"
insertA:
i\n"
Aa;
A*pa;
Bb,*pb;
Cc,*pc;
c.i=1+(b.i=1+(a.i=1));
pc=&
c;
pc->
print();
pb->
pa=&
pa->
三、完善程序
1.
1.下面程序的功能是:
先输入一行字符串,并从输入的字符串中找到最小的字符(其ASCII值最小),用min保存该字符。
然后将该字符前的所有字符顺序向后移动一个字符的位置。
最后,将保存的字符放在该字符串的第0个位置上。
例如:
设输入的字符串为“bcdfae”,其中字母a为最小字符,经移动处理后,输出的字符串为“abcdfe”。
voidfun(){//char*str
charmun,*q,*p;
;
//p=q=str
min=*p++;
while(*p!
=0){
if(){min=*p;
q=p;
}//*p<
min
p++;
p=q;
While(){*p=*(p-1);
;
}//q>
str//p--,q--
*q=min;
charstr[80];
cin.getline(str,80);
fun(str);
str<
’\n’;
2.本程序被完善后输出以下结果。
C++langugeprogramming
Theendatthistimeofday!
string.h>
staticinti;
char*ps;
A(char){//*s
ps=newchar[strlen(s)+1];
strcpy(,s);
//ps
~A(){
if()cout<
Theend"
//i==0
atthistimeofday!
\n"
if(ps)delete[]ps;
i++;
A&
operator=(A&
b){
if(b.ps){
ps=newchar[strlen()+1];
//b.ps
strcpy(ps,b.ps);
}
elseps=0;
return;
//*this
As1("
programming"
),s2("
C++languge"
);
s2.ps<
s2=s1;
3.以下程序功能是:
从一个字符串str中删除或添加一个指定的字符,若指定的字符c出现在str中,则从str中删除第一个值为c的字符;
否则把字符c添加到str的尾部。
在程序中,函数dele()从字符串中删除一个字符;
函数add()添加一个字符到字符尾部;
函数search()用于查找指定的字符是否在字符串中,若在,则返回所在位置,否则返回0。
char*sercher(char*s,charch){
while(*s)
if(*s++==ch)return;
//s-1
return0;
voiddele(char*s,charch){
char*p1=search(s,ch),*p2=p1+1;
while(*p2)*p1++=//*p2++
*p1='
\0'
voidadd(char*s,charch){
while(*s)s++;
=ch;
*s='
//*s++
charstr[80]="
abc12123"
c;
输入一个字符"
cin>
>
void;
if(search(str,c))fp=dele;
//(*p)(char*,char)
elsefp=add;
fp();
//(str,c)
4.班主任根据全班n个学生某课程的考试成绩建立一个链表,每个节点包括学号、成绩和该成绩在全班的名次。
最后按排名顺序输出学号和成绩。
[程序10分]
structnode{
intgrade,n;
longnum;
node*point;
node*trans(node*h,intn){//建立含报名者的档案链表
node*p;
p=newnode;
p->
num>
grade;
p->
n=0;
//node*h=p
for(inti=0;
n-1;
i++){
point=newnode;
;
//p=p->
point;
cin>
p->
point=NULL;
returnh;
node*sort(node*head,intn){//按成绩排名次并输出
node*p,*q;
intt=0,k=1;
p=head;
while(k<
=n){
p=head;
while(p!
=NULL)
if(&
grade>
t){//p->
n==0
t=p->
q=p;
p=p->
elsep=p->
q->
n=k;
;
//k++;
//t=0;
q->
num<
grade<
returnhead;
node*student;
intn;
student=NULL;
n;
student=trans(student,n);
\nnumber:
grade:
排名:
\n"
student=sort(student,n);
5.以下程序定义了一个二维坐标点类Point,派生矩形类Square。
矩形左下角坐标从基类继承,矩形类只定义右上角坐标,还定义表示颜色的字符串。
执行下面的主程序将得到结果:
矩形s1:
x=1y=3width=5,high=6,color=red
矩形s2:
x=1y=3width=5,high=6,color=redYellow
classPoint{
private:
doublex,y;
Point(doublexv=0,doubleyv=0){x=xv;
y=yv;
doublegetx(){returnx;
doublegety(){returny;
voidShow(){cout<
y="
y;
}//输出对象信息
classSquare:
publicPoint{//带颜色的矩形(square)类
doublehx,hy;
char*color;
Square(){hx=0;
hy=0;
Square(doublexv,doubleyv,doublehxv,doublehyv,char*s):
(11){
hx=hxv;
hy=hyv;
color=newchar[9];
strcpy(color,s);
Square():
Point(rr){//拷贝构造函数//Square&
rr
hx=rr.hx;
hy=rr.hy;
color=newchar[strlen()+6];
//rr.color
strcat(strcpy(),"
"
//color,rr.color
//Yellow
voidShow();
voidSquare:
Show(){//输出矩形的左下角坐标、宽度、高度和颜色
Point: