面向对象程序设计期末综合练习五功能与改错Word下载.docx
《面向对象程序设计期末综合练习五功能与改错Word下载.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计期末综合练习五功能与改错Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
if(rem<
10)a[i]=48+rem;
//’0’字符的ASCII码为48
elsea[i]=55+rem;
//’A’字符的ASCII码为65
i++;
}while(x!
=0);
0)cout<
a[--i];
4.#include<
doublef1(intn){
doublesign=1,s=1;
for(inti=2;
=n;
i++){
s+=sign/(i*i);
sign*=-1;
returns;
inta;
cin>
>
a;
f1(a)<
5.doublef1(doublea,doubleb,charop){
switch(op){
case’+’:
returna+b;
case’-’:
returna-b;
case’*’:
returna*b;
case’/’:
if(b==0){
cout<
dividedby0!
exit
(1);
}
elsereturna/b;
default:
operatorerror!
6.#include<
intx,y;
x;
y=int(sqrt(x));
//sqrt(x)为求x的算术平方根
for(inti=1;
=y;
i++)
if(x%i==0)cout<
”x=”<
’*’<
x/i<
7.#include<
inti,p=1,s=0;
intN;
输入一个正整数:
N;
for(i=1;
=N;
i++){
p*=i;
s+=p;
8.#include<
time.h>
constN=10;
intff(intx,inty){
intz;
x<
+'
y<
='
cin>
z;
if(x+y==z)return1;
elsereturn0;
inta,b,c=0;
srand(time(0));
//初始化随机数序列
for(inti=0;
a=rand()%20+1;
//rand()函数产生0-32767之间的一个随机数
b=rand()%20+1;
c+=ff(a,b);
得分:
c*10<
9.ints1(intn)
intx;
if(n==1)x=1;
elsex=s1(n-1)+n*n;
returnx;
10.voidfun5(char*a,constchar*b)
while(*b)*a++=*b++;
*a=0;
11.template<
classT>
boolfun8(Ta[],intn,Tkey)
n;
if(a[i]==key)returntrue;
returnfalse;
12.voidf2(doublea[],intn)
inti;
doublesum=0;
for(i=0;
i++)sum+=a[i];
sum/=n;
if(a[i]>
=sum)cout<
a[i]<
’’;
13.voidf4(chara[M][N])
intc1,c2,c3;
c1=c2=c3=0;
M;
if(strlen(a[i])<
5)c1++;
elseif(strlen(a[i])>
=5&
&
strlen(a[i])<
15)c2++;
elsec3++;
c1<
’’<
c2<
c3<
14.voidfun3(inta[][N],intm,int&
row,int&
col)
intx=a[0][0];
row=col=0;
m;
for(intj=0;
j<
j++)
if(a[i][j]>
x){
x=a[i][j];
row=i;
col=j;
15.intfun6(intm,intn,intb=2)
if(m<
b&
n<
b)returnm*n;
elseif(m%b==0&
n%b==0)returnb*fun6(m/b,n/b,b);
elsereturnfun6(m,n,++b);
16.char*f8(char*str1,constchar*str2)
inti=0,j=0;
while(str1[i])i++;
while(str2[j])str1[i++]=str2[j++];
str1[i]='
\0'
returnstr1;
17.intf8(constchar*str1,constchar*str2)
inti=0;
while(str1[i]&
str2[i])
if(str1[i]==str2[i])i++;
elseif(str1[i]>
str2[i])return1;
elsereturn-1;
if(str1[i]==str2[i])return0;
elseif(str1[i]>
elsereturn-1;
18.IntNode*FindMax(IntNode*f)
if(!
f)returnNULL;
IntNode*p=f;
f=f->
next;
while(f){
if(f->
data>
p->
data)p=f;
f=f->
returnp;
假定IntNode的类型定义为:
structIntNode{
intdata;
//结点值域
IntNode*next;
//结点指针域
};
19.intCount(IntNode*f)
f)return0;
intc=0;
c++;
returnc;
20.voidOutput(IntNode*f)
f)return;
f->
data<
21.voidInput(IntNode*&
f)
intn;
”从键盘给n输入一个整数:
”;
docin>
while(n<
0);
if(n==0){f=NULL;
return;
f=newIntNode;
IntNode*p=f;
”从键盘输入”<
n<
”个整数:
while(n--){
p=p->
next=newIntNode;
data;
p->
next=NULL;
p=f;
deletep;
22.intf(constchar*s)
while(*s++)i++;
returni;
23.char*f(char*s){
intn=strlen(s);
char*r=newchar[n+1];
i<
i++)
if(s[i]>
a'
&
s[i]<
z'
)r[i]=s[i]-'
A'
elser[i]=s[i];
r[n]=’\0’;
returnr;
程序改错
1.在下面的定义中,NODE是链表结点的结构,appendToList则是一函数,其功能是:
在list所指向的链表的末尾添加一个新的值为x的结点,并返回表头指针。
函数中有两处错误,指出错误所在行的行号并提出改正意见。
structNODE{
NODE*next;
};
NODE*appendToList(NODE*list,intx){//1行
NODE*p=newint;
//2行
data=x;
//3行
next=NULL;
//4行
if(list==NULL)returnp;
//5行
NODE*p1=list;
//6行
while(p1->
next!
=NULL)p1=p1->
//7行
p1=p;
//8行
returnlist;
错误行的行号为______和________。
分别改正为______________和______________。
2.在下面的定义中,NODE是链表结点的结构,addToList则是一函数,其功能是:
将一个值为x的新结点添加到以plist为表头指针的链表的首部(即第一个结点的前面)并返回表头指针。
NODE*adndToList(NODE*plist,intx){//1行
NODE*p;
*p=newNODE;
plist=p;
3.假定要求下面程序的输出结果为“11/15”,其主函数中存在着三行语句错误,请指出错误语句行的行号并改正错误行。
classFranction{//定义分数类
intnume;
//定义分子
intdeno;
//定义分母
public:
//把*this化简为最简分数,具体定义在另外文件中实现
voidFranSimp();
//返回两个分数*this和x之和,具体定义在另外文件中实现
FranctionFranAdd(constFranction&
x);
//置分数的分子和分母分别0和1
voidInitFranction(){nume=0;
deno=1;
//置分数的分子和分母分别n和d
voidInitFranction(intn,intd){nume=n;
deno=d;
//输出一个分数
voidFranOutput(){cout<
nume<
/'
deno<
voidmain()//1行
{//2行
Franctiona,b,c;
a.InitFranction(6,15);
b.InitFranction
(1);
c.InitFranction();
c=FranAdd(a,b);
c.nume<
’/’<
c.deno<
}//9行
错误行的行号为______、________和________。
分别改正为____________________、________________和___________________。
4.假定要求下面程序的输出结果为“23/20”,其主函数中存在着三条语句错误,请指出错误语句行的行号并改正。
Franction*a=newFranction;
Franction*b=newFranction;
a->
InitFranction(6,15);
b.InitFranction(3,4);
Franctionc;
c=a.FranAdd(b);
//9行
c.FranOutput()<
//10行
}//11行
5.下面是一个类的定义,存在着3处语法错误,请指出错误行的行号并改正。
classCE{//1行
private:
inta,b;
intgetmin(){return(a<
b?
a:
b);
}//4行
public//5行
intc;
voidSetValue(intx1,intx2,intx3){//7行
a=x1;
b=x2;
c=x3;
intGetMin();
//11行
intGetMin(){//12行
intd=getmin();
//13行
return(d<
c?
d:
c);
//14行
}//16行
6.下面程序段第4-10行中存在着三条语句的语法错误,请指出错误语句的行号并改正。
classA{//1行
constintc;
public//4行
A():
c(0);
a(0);
b(0){}//5行
A(intaa,intbb)c(aa+bb);
{a=aa;
b=bb;
}//6行
Aa,b(1,2);
A*x=&
a,&
y=b;
A*z=newA,w[10];
分别改正为____________________、_____________________
和______________________________________。
Public:
c(0),a(0),b(0){}
A(intaa,intbb):
c(aa+bb){a=aa;
7.下面程序段第4-9行中存在着三条语句错误,请指出错误语句的行号并说明原因。
A(){a=b=c=0;
}//5行
c(aa+bb){a=aa;
Aa,b(1,2,3);
Ax(2,3),y(4);
错误原因分别为___________________、__________________和__________________。
8.下面程序段第10-17行中存在着三条语句错误,请指出错误语句的行号并说明原因。
//2行
A(intaa=0):
a(aa){}//4行
classB{//6行
Ad;
B():
c(0){a=b=0;
}//11行
B(intaa,intbb):
d(aa+bb){//12行
a=aa;
c=aa-bb;
}//14行
}//15行
Ba,b(1,2);
//16行
Bx=a,y(b),z(1,2,3),;
//17行
错误原因分别为_____________________、______________________
和_____________________。
9.假定要求下面程序输出结果为“d=800,f=60”,在第4-23行中存在着三条语句错误,请指出错误语句的行号并改正。
#include<
classA{//1行
inta[10];
A(intaa[],intnn):
n(nn){//4行
i++)aa[i]=a[i];
}//6行
intGet(inti){returna[i];
}//7行
intSumA(intn);
intA:
:
SumA(intn){//10行
ints=0;
for(intj=0;
j++)s+=a[j];
//12行
}//14行
voidmain(){//15行
inta[]={2,5,8,10,15,20};
Ax(a,6);
//17行
intd=1;
//18行
4;
i++)d*=x.a[i];
//19行
intf=SumA(6);
//20行
d="
d<
’,’;
//21行
f="
f<
//22行
}//23行
51920
10.下面是分数类fract的定义及测试主程序,在类定义及其友元函数定义中有两处错误,更正错误后程序应显示41/28,请指出错误所在行的行号并给出改正意见。
classfract{
intden;
//分子
intnum;
//分母
public:
fract(intd=0,intn=1):
den(d),num(n){}//1行
friendfract&
operator+=(fract,fract&
);
voidshow(){cout<
den<
num;
}//3行
//4行
friendfract&
operat