C++徐孝凯习题解答.docx

上传人:b****7 文档编号:10612733 上传时间:2023-02-21 格式:DOCX 页数:25 大小:19.96KB
下载 相关 举报
C++徐孝凯习题解答.docx_第1页
第1页 / 共25页
C++徐孝凯习题解答.docx_第2页
第2页 / 共25页
C++徐孝凯习题解答.docx_第3页
第3页 / 共25页
C++徐孝凯习题解答.docx_第4页
第4页 / 共25页
C++徐孝凯习题解答.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

C++徐孝凯习题解答.docx

《C++徐孝凯习题解答.docx》由会员分享,可在线阅读,更多相关《C++徐孝凯习题解答.docx(25页珍藏版)》请在冰豆网上搜索。

C++徐孝凯习题解答.docx

C++徐孝凯习题解答

清华大学出版社出版

普通高等院校计算机专业(本科)实用教程系列之一

《C++语言基础教程》全部练习题参考解答

第一章C++语言概述

1.2填空题

1.#

2.;{}

3.空格制表回车换行

4.系统用户

5.程序

6.函数头函数体

7.main

8.函数原型

9.原型

10.复合语句

11..h.cpp

12.严重错误警告错误

13.void

14.void

15.int0

16.n

17.下一行

18.空白符

1.3写出下列程序运行结果,此题又作为上机实验题

1.x+y=11,x*y=30

2.cube(3)=27

cube(5)=125

cube(8)=512

3.averageValue:

3

averageValue:

4

4.请输入三个整数:

1059(假定输入的三个整数为10,5,9)

最大值:

10

最小值:

5

第二章数据类型和表达式

2.2填空题

1.4,1,1,4,8

2.short,int,long

3.4,2,2

4.46,123,98

5.107,10,92,42102

6.15

7.符号常量,整数,int

8.3.4E2,5.27E6

9.int,int,double,double,float

10.x,15

11.6,60

12.26,25

13.4,1

14.256,22

15.x,x

16.0,1

17.20

18.5

19.9,216

20.0,19

21.(1+x)*sin(48*3.14159/180),a*pow(x,b)*exp(x+1)

2.7写出下列每个程序运行后的输出结果并自行上机验证

1.0,1,2,3

2.p=62.8318

s=314.159

3.111

222

318

4.1124448844

5.301207

301030

302010

6.110001

7.51015

151015

15010150

1511110

15212151

8.15.721615

15.721615.7

15.721615.7

第三章流程控制语句

3.1填空题

1.选择,循环,跳转

2.goto,continue,break,return

3.结构性

4.常量表达式

5.if

6.switch

7.不停止

8.switch

9.1,n+1,n

10.循环条件,循环体

11.循环,switch,循环

12.循环体,循环体

13.do,右花括号

14.do,for,while

15.10

16.11

17.10

18.break

19.continue

20.return

3.2写出下列每个程序运行后的输出结果并上机验证

1.121

a,b,c=7,12,30

2.1114switchend.

3.36-573192844

36139

-51

738

19213

867

44171

4.362520431270663428153255

12925552

5.111

223

369

42433

6.1064

7.*****

****

***

**

*

8.15711131719232529150

  9.+**+

  k=11

10.14

26

23

35

56

c=37

11.s=63

12.请输入两个正整数x和y:

2488

x和y的最小公倍数:

264

第四章数组和字符串

4.1填空题

1.i+1

2.40a+20

3.192a+136

4.300a+94

5.97

6.380

7.12

8.01

9.8

10.11

11.n+1n+1

12.字符串字符串

13.1019

14.’1’’4’

15.”456”””(或空串)

16.51

17.”abcdef”

18.strcpy(a,”aaa”)

19.3

20.2

21.Integerintint

22.AA10int

23.BB1050500char

24.4624int

4.2写出下列程序运行后的输出结果

1.64

2.6655

3.1425

4.4314

5.43223

6.2152

7.1312

8.workercadre

9.418-1

4.4编写下列程序并上机运行

1.#include

voidmain()

{

inti,a[20];

a[0]=0;a[1]=1;

for(i=2;i<20;i++)a[i]=a[i-1]+a[i-2];

for(i=19;i>=0;i--)cout<

cout<

}

2.#include

voidmain()

{

chars[31];

intc=0;

cout<<"输入一个字符串:

";

cin>>s;

for(inti=0;s[i];i++)

if(s[i]>='0'&&s[i]<='9')c++;

cout<

}

3.#include

voidmain()

{

doublea[4][4];

inti,j;

cout<<"输入一个4*4的数值矩阵:

";

for(i=0;i<4;i++)

for(j=0;j<4;j++)cin>>a[i][j];

doublep=1;

for(i=0;i<4;i++)p*=a[i][i];

cout<

}

4.#include

voidmain()

{

inta[3][4]={{3,8,2,9},{4,7,3,6},{5,2,8,4}};

intb[4][3];

inti,j;

for(i=0;i<3;i++)

for(j=0;j<4;j++)

b[j][i]=a[i][j];

for(i=0;i<4;i++){

for(j=0;j<3;j++)

cout<

cout<

}

}

5.#include

voidmain()

{

inta[3][4]={{3,0,4,5},{6,2,1,7},{4,1,5,8}};

intb[4][4]={{1,4,0,3},{2,5,1,6},{0,7,4,4},{9,3,6,0}};

intc[3][4]={{0}};

inti,j,k;

for(i=0;i<3;i++)

for(j=0;j<4;j++)

for(k=0;k<4;k++)

c[i][j]+=a[i][k]*b[k][j];

for(i=0;i<3;i++){

for(j=0;j<4;j++)

cout<

cout<

}

}

6.#include

#include

voidSelectSort(intb[],intn)

{

inti,j,k;

for(i=1;i

k=i-1;

for(j=i;j

if(b[j]

intx=b[i-1];b[i-1]=b[k];b[k]=x;

}

}

voidmain()

{

inta[10];

inti;

for(i=0;i<10;i++)

a[i]=rand()%90+10;

SelectSort(a,10);

for(i=0;i<10;i++)cout<

cout<

}

7.#include

voidmain()

{

chara[80];

intb[26]={0};

cout<<"输入一个字符串:

";

cin>>a;

inti,x;

for(i=0;a[i];i++){

if(a[i]>='A'&&a[i]<='Z'){

x=a[i]-'A';b[x]++;

}

if(a[i]>='a'&&a[i]<='z'){

x=a[i]-'a';b[x]++;

}

}

for(i=0;i<26;i++){

cout<

'<

if((i+1)%4==0)cout<

}

}

8.#include

#include

voidSelectSort(chara[12][4],doubleb[],intn)

{

inti,j,k;

for(i=1;i

k=i-1;

for(j=i;j

if(b[j]

charx[4];doubley;

strcpy(x,a[i-1]);strcpy(a[i-1],a[k]);strcpy(a[k],x);

y=b[i-1];b[i-1]=b[k];b[k]=y;

}

}

voidmain()

{

chara[12][4]={"001","002","010","011","023","025",

"031","036","037","102","325","438"};

doubleb[12]={13.6,14.8,12.0,12.7,15.6,13.4,14.9,

12.6,13.4,12.5,15.3,12.7};

SelectSort(a,b,12);

for(inti=0;i<12;i++)

cout<

}

第五章指针

5.1填空题

1.4

2.地址DataType*

3.(char*)p

4.int**

5.*p&p

6.*pp

7.*p*p

8.25

9.42

10.26

11.42

12.4*i

13.*(a+i)

14.a+i*sizeof(a[i])

15.第一个修改

16.b[7]b[2]

17.int*p=&x;

18.*p

19.int[n]int*

20.c[3][0]c[3][2]

21.int(*)[6]

22.char[20]char*f[i][0]

23.*(a[i]+j)或*(*(a+i)+j)或*(a+i)[j]

24.int&y=x;

25.x

26.相等x

27.*p

28.0p[0]*P

29.’a’

30.char(*)[n]

31.deletep

32.delete[]p

5.2写出下列每个程序运行后的输出结果并上机验证

1.123

2.3579

11131517

3.2294545.8

4.7224

5.3691215

1512963

6.4122028

7.2040

3060

8.1717

5252

9.computer

typewriter

telephone

10.aremaccamera

11.745103

12.2424

4949

13.515

155

14.011235

81321345589

第六章函数

6.1填空题

1.引用

2.char*a

3.int(*w)[N]

4.不能够

5.不能够能够

6.实参

7.函数声明

8.实参的值地址

9.实参

10.returnreturn

11.数组指针

12.全局文件函数局部

13.能够

14.无关

15.有关不同

16.不同相同

17.不能够相同

18.可以

19.内层外层

20.原型语句

21.自己

22.递归递归

23.相同不同

24.不是不可以

25.函数调用表达式

26.普通函数

27.模板函数

28.int&(*ff)(int[],int)

6.2给出下列程序运行后的输出结果

1.1020

3065

3020

2.1020

15355

1020

3.x=13,y=21

x=5,y=8,z=34

4.x,y=10,26

x,y=26,10

x,y=10,26

x,y=25,11

5.66

6.25

37

8

7.motiontelephone

8.124

9.6543210

91

10.v1=4

v2=5.55

11.操作成功!

操作成功!

操作成功!

25485082

664375

stud

entw

FileEditInsertProject

Build

12.输入矩阵的行数和列数:

34

1234

2468

36912

第七章结构与联合

7.4编写下列程序或函数。

1.voidFindOut(Persona[],intn,intx)

{

for(inti=0;i

if(a[i].age>=x){

cout<

if(a[i].sex==true)

cout<

else

cout<

cout<

cout<

}

}

}

2.Franction&FranCut(Franction&a,constFranction&b)

{

a.nume=a.nume*b.deno-b.nume*a.deno;

a.deno=a.deno*b.deno;

FranSimp(a);

returna;

}

Franction&operator-(Franction&a,constFranction&b)

{

a.nume=a.nume*b.deno-b.nume*a.deno;

a.deno=a.deno*b.deno;

FranSimp(a);

returna;

}

3.template

TypeDelete(Typea[],int&n,inti)

{

if(i<0||i>=n||n<1){

cerr<<"参数值无效!

"<

exit

(1);

}

Typex=a[i];

for(intj=i+1;j<=n-1;j++)

a[j-1]=a[j];

n--;

returnx;

}

4.intFind(Workersa[],intn,intk,char*x)

{

for(inti=k;k

if(a[i].kind==2&&strcmp(a[i].teacher,x)==0)

returni;

return-1;

}

习题八类与对象

8.1单选题

1.C2.C3.A4.A5.B6.C

7.C8.A9.A10.D11.B

8.2填空题

1.构造函数

2.析构函数

3.初始化表函数体

4.析构函数

5.析构函数

6.N

7.10

8.AB(){}

9.~AB(){}

10.friendvoidF();

11.friendclassAB;

8.3写出下列每个程序运行后的输出结果

1.23

45

2.Constructor!

0

Constructor!

5

3.Constructor!

0

Constructor!

0

Constructor!

5

4.Destructor!

10

Destructor!

5

5.Constructor!

3

Constructor!

5

Destructor!

Destructor!

6.a=31

7.119

-51

8.2yuan,8jiao,5fen

4yuan,5jiao,9fen

7yuan,4jiao,4fen

1yuan,7jiao,4fen

5yuan,2jiao,2fen

9.releasedynamicmemoryspace1

releasedynamicmemoryspace1

C++languageprograming.

C++language

programing.

s1>s2

releasedynamicmemoryspace1

releasedynamicmemoryspace1

releasedynamicmemoryspace1

8.4按题目要求编写出程序、函数或类

1.各成员函数的体外定义如下

Quadratic:

:

Quadratic(doubleaa,doublebb,doublecc){

a=aa;b=bb;c=cc;

}

QuadraticQuadratic:

:

operator+(Quadratic&x){

Quadraticq;

q.a=a+x.a;

q.b=b+x.b;

q.c=c+x.c;

returnq;

}

QuadraticQuadratic:

:

operator-(Quadratic&x){

Quadraticq;

q.a=a-x.a;

q.b=b-x.b;

q.c=c-x.c;

returnq;

}

doubleQuadratic:

:

Compute(doublex){

returna*x*x+b*x+c;

}

intQuadratic:

:

Root(double&r1,double&r2){

if(a==0)return-1;

doubled=b*b-4*a*c;

if(d>=0){

r1=(-b+sqrt(d))/(2*a);

r2=(-b-sqrt(d))/(2*a);

return1;

}

elsereturn0;

}

voidQuadratic:

:

Print(){

if(a)cout<

if(b)

if(b>0)cout<<"+"<

elsecout<

if(c)

if(c>0)cout<<"+"<

elsecout<

cout<

}

2.定义的矩形类如下:

classRectangle{//矩形类

doublelen,wid;

public:

Rectangle(){len=wid=0;}

Rectangle(doublelength,doublewidth){

len=length;

wid=width;

}

doublePerimeter(){

return2*(len+wid);

}

doubleArea(){

returnlen*wid;

}

doubleGetLength(){returnlen;}

doubleGetWidth(){returnwid;}

voidModify(doublelength,doublewidth){

len=length;

wid=width;

}

voidOutput(){

cout<<"length:

"<

"<

}

};

第九章类的继承与多态性

9.1填空题

1.初始化表

2.基类成员类对象成员非类对象成员

3.派生类类对象所属类基类

4.intAB:

:

bb=0;

5.AB:

:

bb

9.2写出下列程序运行后的输出结果。

1.ConstructorA!

2

ConstructorB!

3

ConstructorA!

4

ConstructorB!

5

2.DestructorB!

7

DestructorA!

6

DestructorB!

0

DestructorA!

5

3.AXconstructor.

AXconstructor.

BXconstructor.

AXconstructor.

AXconstr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1