C++课后习题答案(电大,李宁主编)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第一章C++语言概述
自测题P2
1.1-1 C
P8 1。

2-1 形式参数表 1.2—2 main 1.2—3 ;
1。

2-4 } 1.2-5 main 1.2—6 显示器输出流
1.2-7 键盘输入流1。

2-8 头文件1。

2—9 int f( );
1.2-10 124 1。

2—11 × 1.2—12 ×
1。

2—13 √ 1。

2-14 √ 1.2—15 ×
1.2-16 √ 1。

2-17 ABCFG 1.2-18 D
1。

2—19 D
P11 1.3-1 OBJ 1.3-2 source.cpp
习题一(P12)
1-1
1.C 2.类型修饰符3.main
4.语句以分号(;)结束5.{6.〈<
7.>〉8..h 9.source。

obj 10.Main 11.char *Hi(const char *);12.33
1-2
1.√2.√3.×4.×5.√6.×7.√8.×
1-3
1.ABFI 2.D 3.ABCD 4.B
1-4
#include 〈iostream。

h〉
void main( )
{
cout 〈〈”C++语言程序设计"<〈endl;
}
1—5
# include 〈iostream。

h>
void main()

int i,j,k;
cout 〈<”请输入3个整数:”;
cin >>i>〉j〉〉k;
cout 〈<endl<<i<〈”+”〈〈j<〈”+”<〈k〈<”=”〈〈i+j+k〈<endl;

第二章基本数据类型与表达式
自测题P15
2。

1—1 常量变量2。

1—2 存储格式所能实施的操作
P18 2。

2-1 √ 2.2-2 × 2.2—3 8 (常量123456789123L超过了长整型表示的
2.2—4 C 2.2-5 long x=0L; 范围,VC++自动默认为双精度型数据)
2.2—6 110
P21 2。

3-1 6 2.3-2 16 2.3-3 “C:\\TOOL\\ARJ。

EXE”
2。

3-4 ‘D’68 2。

3-5 ‘a’+c—‘A’ c 2。

3-6 “Are you ready?”
2。

3—7 131
P23 2.4-1 enum FANCY { DOG,CA T=3,COCK=5,MOUSE,SANKE,GOA T=11,HORSE};
2.4—2 Sat today—1 (说明:VC++不支持枚举数据进行算术运算)
2.4—3 (本题有错,因为C++输入流不支持枚举数据的输入)
P25 2。

5—1 543.21 3。

27863e+2 2。

5—2 3.333333 3。

333333 3
P26 2。

6-1 C
P34 2。

7-1 表达式的值变量i的值
( 6 )( 6 )
( 0 )(0 )
( 11 ) ( 11 )
(9 ) (9 )
(8 ) ( 8 )
( -11 )(-11 )
( 2 )( 2 )
(10 ) ( 10 )
( 2 ) ( 2 )
(251 )(251 )
(327 )( 327 )
(135 )( 135 )
(72 )(72 )
(41 )( 41 )
(0 )(0 )
( 10 ) ( 10 )
( 3 )( 3 )( 说明:上机结果为2 2 )
( 321 )( 320 )
( 257 )( 257 )
( 40 ) (74 )
2。

7—2 6 28 1 2。

7-3 7 2 5
2.7—4 3 4 2.7-5 6 6
2.7—6 3 14 2。

7-7 5 9
2。

7-8 f=++p;2。

7—9 f=p—-;
2.7-10 CD 2。

7-11 EHI
2.7—12 a*a+2*a*b+b*b 2.7—13 c*c*sin(A)*sin(B)/(2*sin(A+b))
2.7—14 32 34。

0 ‘D’
P41 2。

8—1 x〉y+3 2.8—2 x!=3 && x〉=-3 2.8—3 B
2.8-4 C 2.8-5 BC 2.8—6 AD
2.8—7 D 2.8—8 C 2。

8—9 A
2。

8-10 CD 2.8-11 D 2.8-12 B
2.8-13 !isMale Age〉=55 Age〉=50 &&isMale Age<20 || !isMale
P43 2.9—1 typedef unsigned char AGE;
P45 2。

10-1 ABCGI 2.10—2 y=x?0:1 2.10-3 CDG
2.10-4 赋值、复合赋值、++、—- 2.10-5 后增1和后减1
习题二(P47)
2-1 表达式的值变量i的值
(17 ) (17 )
(-9 )( -9 )
( 5 ) ( 5 )
( 1 ) ( 1 )
( 11 )( 11 )
( 2 )( 2 )( 说明:题目印刷错,把I 改为i )
( 32 )(32 )
( 13 ) (13 )
( 7 )(7 )
(251 )(250 )( 说明:题目印刷错,把b=251 改为i=251 )
(325 ) ( 325 )
( 201 )(201 )
( 2 )( 2 )
( 0 )(0 )
(10 ) (10 )
(105 ) (105 )
(50 ) ( 50 )
( 231 )( 230 )
(626 )(626 )
(25 ) ( 38 )
2—2 1. y=(x>100?100:0); 2。

7
3. 17
4. D:MyDir\MyFile。

Dat
5。

‘C’ ‘F’ 6. 48+n ‘U’ 或char(48+n) ‘U'或n+’0’ ‘U'
7. Jun thisMonth+1 (说明:VC++不支持枚举数据参加算术运算,此例无法编译)
8。

( 说明:题目有错,cin >〉m;编译出错,C++不支持枚举数据输入)
9. 12。

3456 9.87653e3 10。

3.66667 4。

0 4
11。

–57 45.0 ‘G’12。

92
13。

What's your name?14。

77
15。

2 44 3 16. 7 4 3
17。

3 1 18. 6 10
19。

3 7 20. 5 2
21。

f=++p+5; 22. f=p—-—5;
23。

x+y>=3 24。

x<3 && x〉=-5
25. a*a*a+3*a*a*a*b+3*a*b*b+b*b*b 26。

1.0/2*a*sqrt((b+2*c)/5)
27. IsMale Age<30 Age<45&&!IsMale Age〉=50||IsMale
2-3
2-4 1. B 2. A 3。

AB 4. B 5. A 6。

ABCD 7. CEFHI 8. CEFGHJ 9. C 10。

C
11. A 12。

AB 13. C
2-5 // P50 习题2-5
#include <iostream.h〉
#include <math。

h>
void main()
{
double a,r;
cout <〈endl〈<”请输入正五边形的边长:”;
cin >〉a;
r=1.0/2*a*sqrt((10+2*sqrt(5))/5);
cout 〈〈endl〈<”正五边形外接圆的半径:"<<r〈〈endl;

2—6 // P50 习题2-6
# include <iostream。

h〉
# include 〈math。

h>
void main()

double a,b,c,area,s;
cout 〈<endl〈<”请输入三角形的三条边的长度:”;
cin 〉〉a>〉b>〉c;
s=(a+b+c)/2;
area=sqrt(s*(s—a)*(s-b)*(s—c));
cout 〈<endl〈〈”三角形的面积:”〈<area<〈endl;

2—7 // P50 习题2—7
# include <iostream.h〉
#include <math.h>
void main()

double x1,y1,z1,x2,y2,z2,d;
cout 〈<endl〈<"请输入空间第一个点的座标:”;
cin 〉>x1〉〉y1>>z1;
cout 〈<endl<〈”请输入空间第二个点的座标:";
cin >>x2>〉y2〉〉z2;
d=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
cout <<endl〈〈"两点间的距离:"〈<d〈〈endl;

第三章C++程序的流程控制
自测题
P52 3.1-1 顺序条件分支循环只有一个入口和一个出口
P65 3。

2—1 false 3。

2—2 AB 3.2—3 B
3。

2-4 t=x; x=y; y=t; 3。

2-5 if (!OK) y=1; else if (READY)y=—1;else y=0;
3。

2-6 1 2 3 n—1 3。

2—7 XYZ
3.2—8 if (k==1 || k==2 || k==3)cout 〈<”A”;
else if (k==4 ||k==5)cout <<”B”;
else cout <〈”C”;
或if (k〉=1 && k<=3)cout <〈”A”;
else if (k>=4 && k〈=5) cout <〈"B”;
else cout <〈”C”;
3。

2-9 switch (x) {
case 0:
case 1:
case 2:cout <〈"X”; break;
case 20:
case 21:
case 22:cout <<”Y”;break;
default : cout <<"C”;break;
};
3。

2—10 ABD 3。

2-11 AD 3。

2-12 BC 3.2-13 BCD
P78 3.3-1 28 3.3-2 21
3。

3—3 for ( int i=10;i<100; i++ )cout 〈”P”;3。

3-4 117 3。

3—5 i=0;while (i〈10) {cout <<i;i++; }
3。

3—6 for (i=20;i>0;i-—)cout <<i;
习题三(P82)
3—1 1。

y z或z y 2. true 3. A 4。

A
5。

x+=y;y=x-y; x—=y; 6。

y=OK?(READY?—1:0):1;
7。

ABC 8. XYZ 9。

ABC 10. 22
11. 23 12. for (int i=100;i; i——) cout <<'P’;13. 46
14.if (k==1 ||k==2 )cout <〈’A’;
else if (k==3 ||k==4 || k==5 ) cout 〈<’B’;
else cout 〈<’C';

if ( k>=1 &&k〈=2 )cout 〈〈’A’;
else if (k〉=3 && k〈=5 ) cout 〈〈’B’;
else cout 〈〈’C’;
15。

switch (x) {
case 1:
case 2:
case 3:cout <〈’X’; break;
case 19:
case 20:
case 21:
case 22: cout 〈<’Y’;break;
default :cout <〈’Z';break;

16. 13
3—2 1. ABC 2。

ABD 3。

C 4. ACD 3-3 // P84 习题3—3
# include <iostream。

h>
void main()

int weekday;
cout 〈〈endl<〈”今天星期几?";
cin >>weekday;
if (weekday==0)
cout 〈〈"Sunday”<<endl;
else if (weekday==1)
cout 〈〈”Monday"<〈endl;
else if (weekday==2)
cout <<”Tuesday"〈〈endl;
else if (weekday==3)
cout <〈”Wednesday”<〈endl;
else if (weekday==4)
cout <<”Thursday"<<endl;
else if (weekday==5)
cout <<"Friday”〈〈endl;
else if (weekday==6)
cout <<”Saturday"〈〈endl;
else cout 〈<”表示星期几的数必须在0—-6之间!"<<endl;

3—4 // P84 习题3—4
#include <iostream。

h>
void main()
{
int a,b,c,d,max;
cout 〈<”请输入4个整数:”;
cin >〉a〉>b>〉c〉>d;
max=a;
if (max〈b)max=b;
if (max<c)max=c;
if (max〈d)max=d;
cout <<endl;
cout 〈〈”最大的数是:"〈〈max〈〈endl;
}
3-5 // P84 习题3-5
# include <iostream.h>
void main()

int a,b,c,d,temp;
cout <<"请输入4个整数:”;
cin 〉>a>>b〉〉c〉〉d;
cout 〈〈"排序前:"<<a<〈’’〈〈b<<' '〈〈c<<' '<〈d〈<endl;
// 求最小数存入a
if (a>b){temp=a;a=b;b=temp;}
if (a>c){temp=a; a=c;c=temp;}
if (a〉d){temp=a;a=d; d=temp;}
// 求第二小数存入b
if (b>c) {temp=b; b=c; c=temp;}
if (b>d){temp=b; b=d; d=temp;}
// 求第三小的数存入c
if (c〉d){temp=c;c=d;d=temp;}
cout 〈<”排序后:"〈<a<<' ’<〈b<〈’’〈<c〈〈' ’〈〈d〈〈endl;

3-6 // P84 习题3—6
# include 〈iomanip.h〉
void main()
{
cout <<endl<〈" |";// 以下3行显示顶部表栏
int i=1;
while (i〈10){
cout 〈<setw(3)〈〈i;
i++;
}
cout 〈<endl<〈"——+-—---————-————-—--—---——----——";
i=1;
while (i<10){// 乘数从1 循环到9
cout 〈<endl<<setw(2)〈〈i<〈"|”;
int j=1;
while (j<10){
cout 〈〈setw(3)<<i*j; // 被乘数从1 循环到9
j++;

i++;

cout〈<endl〈〈endl;
}
3-7 // P84 习题3-7
#include <iostream.h〉
void main()

int a=1,b=2;
double s=0,p;
for (int i=1;i〈=20; i++){
p=(double)b/a;
s+=p;
b+=a;// 产生分子
a=b-a; // 产生分母
}
cout 〈<”2/1+3/2+5/3+8/5+...="〈〈s<<endl;

3-8 // P84 习题3—8
#include 〈iostream.h>
#define N 15
void main()
{
int n;
long double s=0.0L,fact=1.0L;
for (n=1; n<=N; n++){
fact*=n;// 计算n!
s+=fact;

cout <〈"1!+2!+。

+”<<N<<”!="<〈s<〈endl;
}
3-9 // P84 习题3-9
// 方法一
# include <iostream.h〉
void main()
{
int n1=1,n2=1,n=n1+n2,couter=2; // n1,n2相邻二项的分母,cout统计项数
double s=2。

0,s0;
while ((s0=1.0/n)〉=0.000001){
s+=s0;
n1=n2;
n2=n;
n=n1+n2;
couter++;

cout〈〈endl<<endl;
cout <<"累加和:"<<s<<endl;
cout 〈<”累加项数:”〈<couter〈<endl;
}
// 方法二
# include <iostream.h〉
void main()

int n=2,d=1,k=2;// k统计项数
double s=2.0,s0;
while ((s0=1。

0/n)〉=0。

000001){
s+=s0;
n+=d;
d=n-d;
k++;

cout〈〈endl〈<endl;
cout <〈”累加和:"<<s〈〈endl;
cout 〈<"累加项数:"<<k〈〈endl;

3—10 // P84 习题3—10
#include 〈iostream。

h〉
void main()
{
for (int i=1;i〈=6; i++){
cout <<endl;
int j;
for (j=1;j〈i; j++)cout <〈' ';
for (j=1; j<=13—i—i;j++) cout <<'*’;
}
cout 〈〈endl〈〈endl;
}
3—11 // P81 习题3—11
# include <iomanip.h〉
void main()

cout 〈<setw(6)〈<'*’; // 输出顶‘*’
for (int i=0;i<4;i++){
cout <<endl;
int j;
for (j=0;j〈4—i; j++)// 输出载边空格
cout 〈〈' ’;
cout 〈<'*’; // 输出左‘*’
for (j=0; j<i+i+1; j++) // 输出中间空格
cout <<' ’;
cout <〈’*';// 输出右‘*' }
cout <<endl〈<"***********”;
cout 〈<endl<〈endl;

3-12 // P81 习题3-12
# include <iomanip。

h>
void main()
{
int i;
cout <<endl〈<" |”;
for (i=0; i<=9; i++)
cout <〈setw(6)〈<i;
cout <<endl〈〈”-----+”;
for (i=0;i〈=10;i++) cout 〈〈”——--——";
for (i=0; i<=9; i++){
cout 〈<endl〈<setw(5)〈〈i<<'|’;
for (int j=0;j〈=9;j++)
cout <〈setw(6)<<(i*10+j)*(i*10+j);

cout <〈endl<〈endl;
}
3—13 // P81 习题3-13
# include 〈iostream.h〉
#include 〈math.h〉
void main()

double x;
cout <<”请输入一个弧度值:”;
cin 〉>x;
int i=1;
double n=-x*x,d=1.0*2.0,s=1.0,s0;
while (fabs(s0=n/d)>=0.000001){
s+=s0;
i++;
n*=(-x*x);
d*=(i+i)*(i+i—1);

cout 〈<"累加求值COS(”<〈x〈〈")="〈<s〈〈endl;cout <<”标准函数COS("〈〈x〈<”)="<〈cos(x)〈〈endl; cout <<endl<<endl;
}
第四章数组
自测题P93
4.1-1 (3,6,9,0,0)(0)(4) 4。

1—2 (9,6,3)(0) (2)
4.1-3 (100)(0) (99) 4.1—4 (ACD)
4。

1-5 int j=9 , j〈=0 ,-T[j] 4。

1—6 0 ,j<10 ,j++ ,s+=T[j]
P99 4。

2-1 (5)(3) (15)(4)(2)4。

2-2 (2) (4) (8)(1) (3)(1,2,3,4)
4。

2-3 (3) (3)(9) (1,2,0,5,0,0,6,7,8)
4。

2—4 int g[][5]={{1},{1,2},{1,2,3},{1,2,3,4},{1,2,3,4,5}};
4。

2—5 0 ,6 ,i++ ,h[i][i]
4.2—6 0 ,5 ,i++ ,int j=0 ,j<3 ,m[i][j]
4.2-7 BCD
P105 4。

3-1 XABCD 4.3-2 net ,internet
4.3—3 strcpy(s3,s1);strcpy(s1,s2);strcpy(s2,s3);
4.3-4 strcat(s3,s1); strcat(s3,s2);或strcat(strcat(s3,s1),s2)
4。

3-5 if (!strcmp(s1,s2))
cout <<s1<〈endl〈<s2〈<endl;
else if (strcmp(s1,s2)>0)
cout <<s1<〈endl;
else
cout 〈<s2〈<endl;
或if ( strcmp(s1,s2)〉0 )
cout <<s1〈〈endl;
else if (strcmp(s1,s2)<0)
cout <〈s2<〈endl;
else
cout 〈<s1<<endl〈<s2〈〈endl;
或if ( strcmp(s1,s2)〈0 )
cout 〈〈s2<<endl;
else if (strcmp(s1,s2)>0)
cout <<s1<<endl;
else
cout 〈<s1<〈endl<〈s2<〈endl;
4.3—6 D 4.3-7 BCD
习题四(P107)
4—1 1。

( 3,6,9,12 ) ( 0 )(3 ) 2。

(9,6,3,0,0,0 ) ( 0 ) (5 )
3. ( 50 )(0 ) ( 49 )4。

( int j=0 ) ( j+=2 )(a[j])
5.( 0 )(j=0 ) (j<18 ) ( s+=s0[j])( /= )
(说明:题目的“…并用s保存…”改为“…并用s0保存…”)
6。

( 4 )(11 ) ( 44 )(3 ) ( 10 )
7. ( 3 ) (5 )( 15 ) (2 )(4 ) ( {{ 1,2,3,0,0 },{ 5,6,0,0,0 },{ 8,0,0,0,0}} )
8. (4 )( 3 ) ( 12 )({{1,2,0},{5,0,0},{6,7,8},{0,0,0}})
9。

( int g[5][5]={{1,2,3,4,5},{1,2,3,4},{1,2,3}};
10. ( int i=0 )(i<8 )(h[i][i] )
11. ( 0 )( 5 ) ( i++ ) ( int j=0 ) (j<3 ) ( m[4—i][j])
12. (“ABCD" )13。

(net ) (in )
14. ( aaabbbbccccc ,bbbbccccc , ccccc )
15. ( aaa , cccccaaa , ccccc )
16.( abc ,abf ,abcdabf )
4-2 1。

BCD
2。

AB
3。

CD
4。

AC
4—3 // P109 习题4-3
# include 〈iostream。

h>
# define SIZE 10
void main()
{
int a[SIZE];
int temp,i,max,min;
cout <〈”输入"〈<SIZE〈<”个整数:";
cin >〉a[0];
max=min=0;// 存放最大数和最小数的下标
for (i=1; i<SIZE;i++){
cin 〉〉a[i];
if (a[i]〉a[max]) max=i; // 保存当前最大数的下标
if (a[i]<a[min]) min=i;// 保存当前最小数的下标
}
cout <〈endl〈<”交换前:”;
for (i=0;i<SIZE; i++)
cout 〈〈a[i]<〈' ’;
temp=a[0];a[0]=a[max];a[max]=temp; // 最大数存首元素
temp=a[SIZE-1];a[SIZE-1]=a[min]; a[min]=temp; // 最小数存末元素
cout <<endl〈〈”交换后:”;
for (i=0;i〈SIZE; i++)
cout 〈<a[i]〈〈' ’;
cout <〈endl〈<endl;
}
4-4 // P109 习题4-4
方法一:
#include <iostream。

h>
void main()

int a[]={23,25,13,68,3,44,11,7,55,36};
const int SIZE=sizeof(a)/sizeof(a[0]); // 数组a的元素个数
int temp,i;
cout <<endl〈<”处理前:”;
for (i=0;i<SIZE;i++)
cout <〈a[i]<<' ';
for (i=0;i〈5;i++){ // 找前5大数之一
int k=i,j; // k保存大数的下标
for (j=5; j〈SIZE;j++)// 查找大数的下标
if (a[j]〉a[k])k=j;
temp=a[i];a[i]=a[k]; a[k]=temp;// 交换

cout <<endl〈〈”处理后:";
for (i=0;i〈SIZE; i++)
cout 〈<a[i]〈〈' ’;
cout <〈endl<〈endl;
}
方法二:
// P109 习题4—4
#include <iostream.h〉
void main()

int a[]={23,25,13,68,3,44,11,7,55,36};
const int SIZE=sizeof(a)/sizeof(a[0]);// 数组a的元素个数
int temp,i;
cout <<endl〈<"处理前:";
for (i=0;i<SIZE; i++)
cout <〈a[i]〈<' ’;
for (i=0;i〈5;i++){ // 用选择排序法排前5个数为大数int k=i,j; // k保存当前最大数的下标
for (j=i+1; j<SIZE; j++) // 查找最大数的下标
if (a[j]>a[k])k=j;
temp=a[i];a[i]=a[k]; a[k]=temp;// 交换
}
cout <<endl<<”处理后:";
for (i=0;i<SIZE;i++)
cout <<a[i]〈〈' ’;
cout 〈〈endl〈〈endl;
}
4-5 // P109 习题4—5
# include 〈iostream。

h〉
#define SIZE 20
void main()
{
int a[SIZE],i;
cout 〈〈endl〈<”请输入”〈<SIZE<<”个正整数:";
for ( i=0; i<SIZE; i++)
do { // 本循环控制输入的必为正整数
cin >〉a[i];
}while (a[i]〈=0);
cout <<endl〈〈”处理前:”;
for (i=0;i<SIZE;i++)
cout 〈〈a[i]〈〈’’;
cout <〈endl;
int j=0; // j为奇数存放下标
for (i=0;i〈SIZE; i++)
if ( a[i]%2 ) {// 条件成立,表示a[i]为奇数
int temp=a[i];
a[i]=a[j];
a[j]=temp;
j++; // 下一个奇数存放位置
}
cout <<endl<〈"处理后: ”;
for (i=0; i〈SIZE; i++)
cout <<a[i]<<’’;
cout <<endl<<endl;

4—6 // P109 习题4—6
#include 〈iostream.h>
void main()

int a[]={23,25,13,68,3,44,11,7,55,36};
const int SIZE=sizeof(a)/sizeof(a[0]);// 数组a的元素个数
int i,number;
cout <〈endl〈<"请输入1个整数:”;
cin 〉〉number;
cout 〈〈endl〈<endl〈<”整数序列:";
for (i=0; i<SIZE;i++) // 显示数组元素
cout <〈a[i]<〈’’;
cout 〈<endl〈<endl<〈"输入数:”<<number<<"的序号: ”;
for (i=0; i〈SIZE &&a[i]!=number;i++); // 查找
cout <〈((i<SIZE)?i+1:0)<〈endl;// 表达式中条件成立,表示找到
cout <〈endl;

4—7 // P109 习题4—7
#include <iomanip.h〉
# define SIZE 5
void main()
{
int a[SIZE][SIZE];
int i,j,sum=0;
cout 〈〈endl〈<"请输入”〈<SIZE<〈"行”<<SIZE<〈"列整型数组:”〈<endl;
for (i=0; i<SIZE;i++)
for (j=0; j〈SIZE;j++)
cin >>a[i][j];
cout 〈<endl〈〈endl<<SIZE<〈”行”〈<SIZE<〈"列整型矩阵: ";
for (i=0; i〈SIZE;i++) { // 显示二维数组元素
cout 〈<endl;
for (j=0; j〈SIZE; j++)
cout 〈<setw(4)<<a[i][j];

for (i=0; i〈SIZE; i++)
for (j=0; j〈SIZE; j++)
if (i==0 || j==0 || i==SIZE—1 || j==SIZE—1)
sum+=a[i][j];
cout <<endl〈<endl<<”矩阵四周元素的合计值:"〈〈sum<〈endl〈<endl;
}
4—8 // P109 习题4-8
#include 〈iomanip.h>
#include 〈math。

h>
# define SIZE 10
void main()
{
double data[SIZE][SIZE];
int i,j,size;
cout 〈<”请输入行列式的阶数(1——10):";
cin 〉>size;
if (size<1 ||size〉10){
cout 〈〈"行列式的阶数要求在1--10 之间。

"〈<endl;
return;
}
// 输入行列式
cout 〈<"请输入”<〈size<〈"行"<<size<<"列的行列式:”<<endl;
for (i=0; i〈size;i++)
for (j=0; j<size;j++)
cin 〉〉data[i][j];
// 输出行列式
cout 〈〈endl<〈endl〈<size<〈"行”<<size〈〈"列的行列式:”<<endl;
for (i=0;i<size; i++){
cout <<endl;
for (j=0;j〈size;j++)
cout 〈<setw(10)<〈data[i][j];
}
cout <〈endl;
// 调用函数grmdet()计算行列式的值
double grmdet(double a[][SIZE],int size);
cout 〈<endl<〈size<<”行"〈〈size<〈”列的行列式的值为:";
cout 〈〈grmdet(data,size)<<endl;

// 计算行列式的值函数。

以下函数定义参阅1982年12月第1版上海教育出版社出版// 上海计算技术研究所编《电子计算机算法手册》第295页算法
double grmdet(double a[][SIZE],int size)

int i,j,k,n;
double p,t,u;
double m[SIZE];
for (i=0; i〈size;i++){
u=0.0;
for (j=0; j<size; j++)
if ( abs(a[i][j]>u)) u=abs(a[i][j]);
if (u〈1.0E—18)return 0.0;
m[i]=u;
if (u!=1)
for (j=0;j<size;j++)
a[i][j]/=u;

p=1;
for (k=0;k<size-1;k++){
n=k;
t=abs(a[k][k]);
for (j=k+1; j<size; j++)
if (t<abs(a[k][j])) {
t=abs(a[k][j]);
n=j;

if (t<1.0E—18) return 0.0;
if (n!=k) {
p=-p;
for (i=k;i〈size; i++){
t=a[i][k];
a[i][k]=a[i][n];
a[i][n]=t;
}
}
p*=a[k][k];
for (i=k+1;i〈size; i++) {
t=a[i][k]/a[k][k];
for (j=k+1;j〈size;j++)
a[i][j]-=a[k][j]*t;
}
}
t=p*a[size—1][size-1];
for (k=0;k〈size; k++)
t*=m[k];
return t;

4-9 // P109 习题4-9
方法一:
#include <iostream.h〉
#include <string。

h〉
# define SIZE 80
void main()
{
char s[SIZE];
cout 〈<"请输入一个字符串: ”;
cin.getline(s,80);
cout 〈<endl〈<"原字符串: "〈〈s<<endl;
char str[SIZE]; // 用str存放子串
if (strstr(s,"BOY”)){// 条件成立,表示原字符串s有子串”BOY”
strcpy(str,strstr(s,"BOY”)); // 复制子串BOY到str中
int len1=strlen(s);// 原字符串s的长度
int len2=strlen(str); // 子串长度
s[len1—len2]=’\0’;// 在原字符串s中截断子串
strcat(s,”CHILD”);// 在s字符串中连接新子串
strcat(s,&str[3]); // 连接原子串BOY后的字符串
cout 〈<”替换后串: ”<〈s<〈endl;

else
cout <<”无子串\"BOY\"."<〈endl〈<endl;

方法二:
# include <iostream。

h>
# include <string。

h〉
void main()
{
cout <<”请输入一字符串:";
char s[80];
cin。

getline(s,80);
cout 〈〈”原串:”〈〈s〈<endl;
char *p=strstr(s,"BOY”); // 取子串BOY位置
if (p){// 条件成立,表示存在子串BOY
p[0]='\0’;// 在原串中截断子串BOY
char c[80];
strcpy(c,s);// 复制原串中截断子串BOY后剩下的串
strcat(c,"CHILD");// 连接替换的串CHILD
strcat(c,&p[3]); // 连接原子串BOY后的字符串
cout 〈<”新串:”〈<c;
}
else
cout 〈<”无子串\"BOY\”.";
cout <<endl;

4-10 // P109 习题4—10
#include 〈iostream。

h〉
#include 〈string。

h〉
#define SIZE 80
void main()
{
char s[SIZE];
cout <<endl〈<”请输入一个字符串: "; cin.getline(s,80);
cout 〈<endl<〈”加密前串: "〈<s<<endl;
int i;
for (i=1; i<SIZE ; i+=2) {// 加密
char temp=s[i];
s[i]=s[i—1];
s[i—1]=temp;
}
cout 〈〈”加密后串:”<〈s〈〈endl;
for (i=1;i<SIZE ;i+=2){// 解密char temp=s[i];
s[i]=s[i-1];
s[i-1]=temp;
}
cout <<endl〈<"解密后串:"〈<s<<endl;cout 〈〈endl〈<endl;

第五章C++ 函数
自测题P112
5.1-1 return 5.1-2 return x>100 && y==0
5.1—3 BDE 5。

1—4 C
5。

1—5 AC 5。

1-6 BC
P117 5。

2-1 表达式语句5。

2-2 语句5。

2—3 A 5。

2—4 BC 5.2—5 递归调用
P121 5.3—1 void ff(double); 或void ff(double d);
5。

3—2 A 5。

3-3 C 5。

3—4 D
P128 5.4-1 8 3 5.4-2 CD 5.4-3 AB
P131 5.6-1 数量类型 5.6—2 AD
P134 5.7—1 static 5。

7-2 局部5。

7-3 extern double var;
5。

7-4 C 5.7-5 D 5。

7-6 D
5。

7—7 D 5。

7—8 DE
P139 5.8-1 template 5.8—2 常规虚拟类型常规
5.8-3 template <class Type>
Type cubic(Type n)
{return n*n*n;}
5.8—4 实例化函数为:
void SHOW(char *d) {
for (int j=0;j<10; j++) cout 〈〈d[j]<〈' ’;

输出结果为:
a b c d e f g h i j
习题五(P140)
5-1 1. 无返回值或void 2。

语句 3. 语句4。

函数直接或间接地调用自已5。

7 3 6。

数量类型
7. static 8. 全局9. extern int var;
10。

template <class T > T incre(T n ){ return n+1; }
5-2 1。

A 2。

B 3。

A D 4. B
5. A B
6. B D 7。

C 8。

A C
9。

A B 10. B D 11。

C 12. C
5-3 // p142 习题5—3
# include <iostream.h〉
int f(int b,int n)// 计算b的n次方递归函数
{
if (n〈=0)return 1;
return b*f(b,n—1);

void main() // 依题意解题时主函数可以不必书写,这里给出主
{ // 函数只是方便同学们理解程序(以下相同)
int x,n;
cout 〈〈"请输入两个整数:”;
cin 〉〉x〉〉n;
cout 〈<x〈〈”的"〈〈n〈<"次方是:”;
cout <〈f(x,n)<<endl;

5—4 // p142 习题5-4
# include 〈iostream。

h〉
int ArrayMin(int d[],int size) // 求一维数组元素的最小值的递归函数
{
int j=size—1; // 默认有size个元素的数组d中,下标为size-1的元素是最小元素if (size==1) return d[j];
if (d[j]〈d[0]) {
int temp=d[j];
d[j]=d[0];
d[0]=temp;

return ArrayMin(d,size-1);
}
void main() // 依题意解题时主函数可以不必书写

#define SIZE 10
int data[SIZE],i;
cout <〈”请输入”〈〈SIZE<<"个整数存入数组中:"<〈endl;
for (i=0; i<SIZE;i++)
cin 〉>data[i];
cout 〈〈endl<〈"输入的数据是:”<〈endl;
for (i=0; i〈SIZE;i++)
cout <〈data[i]〈<’';
cout <<endl<<"最小数是:”<<ArrayMin(data,SIZE)〈〈endl;

5—5 // p142 习题5-5 头文件exe5_5.h
double distance(double,double,double,double);
double arround(double [],double [],int);
# define SIZE 100
// p142 习题5-5 程序文件exe5_5。

cpp
#include ”exe5_5.h”
# include 〈math.h〉
double distance(double x1,double y1,double x2,double y2)
{ // 计算两点坐标(x1,y1)和(x2,y2)的距离函数
return sqrt((x2-x1)*(x2—x1)+(y2-y1)*(y2-y1));

double arround(double x[],double y[],int p)
{// 计算多边形周长的函数
double s=0;
for (int j=0;j〈p-1; j++)
s+=distance(x[j],y[j],x[j+1],y[j+1]);
s+=distance(x[p-1],y[p—1],x[0],y[0]);
return s;

// p142 习题5-5 主程序文件exemain.cpp
#include 〈iostream。

h〉
# include ”exe5_5.h"
void main()
{
double x[SIZE],y[SIZE];
int maxsize;// 多边形的最大边数
cout 〈<endl〈〈”请输入多边形的边数(最大边数100): ”;
cin 〉〉maxsize;
cout <〈”请按顺时针或逆时针顺序输入多边形”;
cout 〈〈maxsize<〈”个顶点的坐标(格式(x,y)):”<〈endl;
int i;
for (i=0; i〈maxsize;i++)
cin 〉〉x[i]〉>y[i];
cout 〈〈"多边形的”〈〈maxsize<〈" 个顶点坐标为:";
for (i=0;i<maxsize;i++)
cout 〈<”(”〈〈x[i]<<”,"〈<y[i]〈<")”;
cout <<endl;
cout 〈<”多边形的周长:"<〈arround(x,y,maxsize)〈〈endl;
}
5-6 // P143 习题5-6 头文件exe5_6.h
void selsort(int [],int);
# define SIZE 100
// P143 习题5-6 程序文件sort。

cpp
# include "exe5_6。

h”
void selsort(int data[],int size)

for (int m=0;m〈size; m++){
int j=m;
for (int i=m+1; i<size;i++)
if (data[i]<data[j])j=i;
if (j〉m){
int k=data[m];
data[m]=data[j];
data[j]=k;
}

}
// P143 习题5—6 主程序文件exe5_6.cpp
# include <iostream.h>
#include "exe5_6.h"
void main()

int maxsize;
cout <〈endl〈<"请输入待排序的数据个数(最大为100个数):”;
cin 〉>maxsize;
int data[SIZE];
int i;
cout 〈〈”请输入”<〈maxsize〈〈”个待排序的数据:”<<endl;
for (i=0; i〈maxsize;i++)
cin >>data[i];
cout 〈<endl<<”排序前:”;
for (i=0; i<maxsize; i++)
cout 〈〈data[i]<〈" ";
selsort(data,maxsize);
cout 〈<endl<〈"排序后: ”;
for (i=0;i<maxsize; i++)
cout <<data[i]<<" ";
cout 〈<endl;

5—7 // P143 习题5—7
# include 〈iostream。

h>
#include <string。

h>
char * f(char * s1="Hello,”,char *s2="world!”);
char s[80];
char *f(char *s1,char *s2)
{
strcpy(s,s1);
return strcat(s,s2);
}
void main() // 依题意解题时主函数可以不必书写
{
cout 〈〈f()<<endl;
cout <〈f("Hi,”)〈〈endl;
cout <〈f(”Hi,","friend!")〈〈endl;
}
5-8 方法一:
#include 〈iostream.h〉
unsigned POWER(unsigned n,unsigned x=2)
{
unsigned s=1;
for (unsigned i=0;i<x;i++)
s*=n;
return s;
}
void main() // 依题意解题时主函数可以不必书写{
cout <〈POWER(2)<〈',';
cout 〈〈POWER(2,0)〈<’,';
cout 〈<POWER(2,1)<〈',’;
cout 〈〈POWER(2,2)<〈’,’;
cout 〈<POWER(2,3)〈〈',';
cout <<POWER(2,4)<〈’,';
cout 〈<POWER(3)<<’,’;
cout <〈POWER(3,3)〈<',';
cout <〈POWER(3,4)〈〈endl;
}
方法二:
#include 〈iostream。

h>
unsigned POWER(unsigned n,unsigned x=2)
{
if (x==0) return 1;
bool flag=n==2;
for (unsigned j=2;j〈=x;j++ )
flag ? n*=2 :n*=3;
return n;
}
方法三:
unsigned POWER(unsigned n,unsigned x=2)
{
if (x>1) {
unsigned s=n;
for (unsigned i=1;i〈x; i++)
s*=n;
return s;

else {
if (x==1) return n;
return 1;

}
5-9 // P143 习题5—9
#include <iostream.h〉
template 〈class T〉
T S(T d[],int size)

int i;
T sum=0;
for (i=0;i<size;i++)
sum+=d[i]*d[i];
return sum;
}
void main() // 依题意解题时主函数可以不必书写
{
int iarray[]={1,2,3,4,5};
double darray[]={1.1,2。

2,3.3};
cout <〈S(iarray,5)<〈endl;
cout <〈S(darray,3)<〈endl;
}
5—10 // P144 习题5-10
# include <iostream.h〉
template <class T,int R,int C〉
void aggr(T (*data)[C])
{
for (int i=0;i〈R; i++){
data[i][0]=0;
for (int j=0;j〈C;j++)data[i][0]+=data[i][j];

}
void main() // 依题意解题时主函数可以不必书写

int a[3][5]={{0,1,2,3,4},{0,2,3,4,5},{0,3,4,5,6}};
aggr〈int,3,5>(a);
for (int i=0; i〈3; i++){
cout <〈endl;
for (int j=0; j〈5; j++)
cout <〈a[i][j]<<" ";

cout <〈endl;
}
5—11 // P144 习题5-11
#include <iomanip.h>
#include <math。

h>
// 计算行列式的值函数。

以下函数定义参阅1982年12月第1版上海教育出版社出版
// 上海计算技术研究所编《电子计算机算法手册》第295页算法
template <class T,int SIZE>
T valueOf( T (*sq)[SIZE] ) // 说明:在VC++6.0下形参不允许书写成sq[][SIZE]形式
{
int i,j,k,n;
T p,t,u;
T m[SIZE];
for (i=0;i〈SIZE; i++) {
u=0;
for (j=0; j〈SIZE;j++)
if ( abs(sq[i][j]〉u) ) u=abs(sq[i][j]);
if (u<1。

0E—18)return 0;
m[i]=u;
if (u!=1)
for (j=0;j〈SIZE;j++)
sq[i][j]/=u;

p=1;
for (k=0; k〈SIZE-1;k++){
n=k;
t=abs(sq[k][k]);
for (j=k+1;j<SIZE;j++)
if (t〈abs(sq[k][j])){
t=abs(sq[k][j]);
n=j;

if (t<1。

0E-18) return 0;
if (n!=k) {
p=-p;
for (i=k; i〈SIZE;i++) {
t=sq[i][k];
sq[i][k]=sq[i][n];
sq[i][n]=t;
}

p*=sq[k][k];
for (i=k+1;i<SIZE;i++){
t=sq[i][k]/sq[k][k];
for (j=k+1; j<SIZE;j++)
sq[i][j]-=sq[k][j]*t;
}

t=p*sq[SIZE—1][SIZE-1];
for (k=0;k〈SIZE;k++)
t*=m[k];
return t;
}
void main() // 依题意解题时主函数可以不必书写

#define MAXSIZE 4
double data[MAXSIZE][MAXSIZE];
int i,j;
// 输入行列式
cout <<"请输入"〈〈MAXSIZE<<"行"<〈MAXSIZE<〈"列的行列式:”〈〈endl;
for (i=0; i〈MAXSIZE; i++)
for (j=0;j〈MAXSIZE; j++)
cin 〉〉data[i][j];
// 输出行列式
cout <〈endl<〈endl〈〈MAXSIZE<<”行”〈<MAXSIZE<〈"列的行列式:”〈<endl;
for (i=0; i〈MAXSIZE;i++){
cout 〈〈endl;
for (j=0;j<MAXSIZE; j++)
cout 〈<setw(10)<<data[i][j];

cout 〈<endl;
// 调用函数valueOf()计算行列式的值
cout 〈〈endl〈〈MAXSIZE〈<”行"<〈MAXSIZE<〈"列的行列式的值为:”;
cout <<valueOf〈double,MAXSIZE>(data)〈〈endl; // 调用函数模板求行列式值}
第六章指针、引用和动态空间管理
习题六(P187)
6-1 1、12
2、5 3
3、0x0065FE00
4、*--p
5、-—*p
6、*p—-
7、(*p)--
8、IJKLMN
9、const long * p=ld;
10、double * *p=X;
11、int ( *p1 ) [6 ];
12、int (*p2 ) [ 4 ][ 6 ];
13、char *WEEKDAY[]={”Sun","Mon”,"Tue”,"Wed","Thu”,”Fri”,”Sat”};
14、(1)void fa( double ,char *) ;
(2)void ( * pfa)( double ,char *)=fa ;
15、void (* (* pfb)( ))(const char * )=fb;
16、char &kj=j;
17、char (*&kp)[4]=p;
18、65 ,67
19、99 ,195
20、(1) int * px ;
(2) px=new int (x);
21、(1)int *py ;
(2) py=new int[5] ;
(3)for (int i=0;i<5; i++) py[i]=i+1;
或py[0]=1 ; py[1]=2 ; py[2]=3 ;py[3]=4 ;py[4]=5 ;
22、(1)double ( ** pz )[4][10] ;
(2) pz=new (double (* )[4][10]);
23、delete []p1 , p2 ,p3;
24、( 2 ) (13 )(11 )( 19 )( 11,10,10 )( 10,9,9 ) (11,11 )( 8,8 ) (8,9 )
6—2 1. D 12. B
2. B 13。

D
3. BC 1
4. BC
4。

AB 15。

C
5。

C 16。

B
6。

D 17. B
7。

C 18. A
8. C 19. C
9。

B 20。

B
10。

AC 21。

C
11。

AD 22。

C。

相关文档
最新文档