c++程序设计课后练习答案.docx

上传人:b****3 文档编号:4953725 上传时间:2023-05-07 格式:DOCX 页数:72 大小:32.60KB
下载 相关 举报
c++程序设计课后练习答案.docx_第1页
第1页 / 共72页
c++程序设计课后练习答案.docx_第2页
第2页 / 共72页
c++程序设计课后练习答案.docx_第3页
第3页 / 共72页
c++程序设计课后练习答案.docx_第4页
第4页 / 共72页
c++程序设计课后练习答案.docx_第5页
第5页 / 共72页
c++程序设计课后练习答案.docx_第6页
第6页 / 共72页
c++程序设计课后练习答案.docx_第7页
第7页 / 共72页
c++程序设计课后练习答案.docx_第8页
第8页 / 共72页
c++程序设计课后练习答案.docx_第9页
第9页 / 共72页
c++程序设计课后练习答案.docx_第10页
第10页 / 共72页
c++程序设计课后练习答案.docx_第11页
第11页 / 共72页
c++程序设计课后练习答案.docx_第12页
第12页 / 共72页
c++程序设计课后练习答案.docx_第13页
第13页 / 共72页
c++程序设计课后练习答案.docx_第14页
第14页 / 共72页
c++程序设计课后练习答案.docx_第15页
第15页 / 共72页
c++程序设计课后练习答案.docx_第16页
第16页 / 共72页
c++程序设计课后练习答案.docx_第17页
第17页 / 共72页
c++程序设计课后练习答案.docx_第18页
第18页 / 共72页
c++程序设计课后练习答案.docx_第19页
第19页 / 共72页
c++程序设计课后练习答案.docx_第20页
第20页 / 共72页
亲,该文档总共72页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

c++程序设计课后练习答案.docx

《c++程序设计课后练习答案.docx》由会员分享,可在线阅读,更多相关《c++程序设计课后练习答案.docx(72页珍藏版)》请在冰点文库上搜索。

c++程序设计课后练习答案.docx

c++程序设计课后练习答案

第一章

一、选择题

1.B;(typedef,typeid,typename,都为保留字);

2.C;(标识符,应该以字母或,下划线开头);

3.C;(标识符中有的特殊符号,只能有下划线);

二、填空题

1.cin,cout

2.new,delete

3.inta(55);

三、改错题

1.没有定义变量num;

2.constint*p=&x;是声明指向常量的指针,*p不能当作“左值”,*p=65错误。

3.p为常量指针,不能吧p作为“左值”,p=&y,错误。

四、编程题

1.分别用字符和ASCII码形式输出整数值65和66.

#include

usingnamespacestd;

voidmain()

{

chara='A',b='B';

intascii_1=53,ascii_2=54;//ASCII码中的,5和6

cout<<"字符输出:

"<<(int)a<<","<<(int)b<

cout<<"ASCII码输出:

"<<(char)ascii_2<<(char)ascii_1<<",";

cout<<(char)ascii_1<<(char)ascii_1<

}

2.编写一个int型变量分配100个整形空间的程序。

#include

usingnamespacestd;

voidmain()

{

int*p;

p=newint[100];

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

{

*(p+i)=i;

}

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

{

cout<<*(p+i)<<",";

}

deletep;

}

3.编写完整的程序,它读入15个float值,用指针把它们存放在一个存储快里,然后输出这些值和以及最小值。

#include

#include//用于数组排列的头文件

usingnamespacestd;

voidmain()

{

float*p;

p=newfloat[15];

cout<<"输入15个float类型的值:

"<

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

{

cin>>*(p+i);

}

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

{

cout<<*(p+i)<<",";

}

sort(p,p+15);

cout<<"\n最小的是:

"<<*(p)<

deletep;

}

4.声明如下数组:

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

先查找4的位置,讲数组a复制给数组b,然后将数组a的内容反转,再查找4的位置,最后分别输出数组a和b的内容。

#include

#include

#include

usingnamespacestd;

voidmain()

{

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

cout<<"数组a中‘4’的位置是:

"<

copy(a,a+8,b);//将数组a复制给数组b

reverse_copy(b,b+8,a);//把数组b,逆向复制给a,完成a的逆转

cout<<"数组a反转后,‘4’的位置是:

"<

cout<<"数字a的内容:

"<

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

cout<

cout<<"\n数组b中的内容:

"<

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

cout<

}

第二章

一、单项选择

1.D;2.D;

三、编程题

1.使用多种方法编写将两个字符串连接在一起的程序。

#include

#include

usingnamespacestd;

voidmain()

{

//使用string类定义字符串,完成字符串连接

stringstr1("C++"),str2("程序设计");

stringstr3;

str3=str1+str2;//连接方式1

cout<

//使用char数组定义字符串,完成连接

charc1[]={"c++"},c2[]={"program"};

charc3[20];

inti=0,k=0;

for(i=0;i<20;i++)//初始化c3

c3[i]='\0';

i=0;

while(c1[i]!

='\0')

{

c3[k]=c1[i];

i++;

k++;

}

i=0;

while(c2[i]!

='\0')

{

c3[k]=c2[i];

i++;

k++;

}

cout<

}

2.已知一个string的对象str的内容为“Wearehere!

”,使用多种方法输出“h”。

#include

#include

#include

#include

usingnamespacestd;

voidmain()

{

stringstr1("Wearehere!

");

cout<

stringstr2=str1.substr(7,1);//通过得到子字符串

cout<

char*p=find(str1.begin(),str1.end(),'h');//通过find函数

if(p)

cout<<*p<

}

第三章

一、填空题

1.函数原型声明;

2.inline

3.对象、对象指针、引用

4.函数func返回引用

5.int*fun(char,int&);

二、单项选择题

1.A;2.C;3.C;

三、改错题

1.y=x*x-T;错误,T是类型,不是变量,不能参加运算;

2.y没有类型,并且x的类型和template中的不一样。

#include

template

Typemax(Typex,Typey)

{

return(x>y)?

(x):

(y);

}

3.函数change的参数定义成了常量,只能使用参数,而无权修改他。

voidchange(string&s)

{

s=s+"pig!

";

}

四、编程题

1.编写一个求方程ax2+bx+c=0的根的程序,用3个函数分别求当b2-4ac大于零、等于零、和小于零时的方程的根。

要求从主函数输入a,b,c的值并输出结果。

#include

#include

voidequation_1(inta,intb,intc)

{

doublex1,x2,temp;

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

x1=(-b+sqrt(temp))/(2*a*1.0);

x2=(-b-sqrt(temp))/(2*a*1.0);

cout<<"两个不相等的实根"<

cout<<"x1="<

}

voidequation_2(inta,intb,intc)

{

doublex1,x2,temp;

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

x1=(-b+sqrt(temp))/(2*a*1.0);

x2=x1;

cout<<"两个相等的实根"<

cout<<"x1="<

}

voidequation_3(inta,intb,intc)

{

doubletemp,real1,real2,image1,image2;

temp=-(b*b-4*a*c);

real1=-b/(2*a*1.0);

real2=real1;

image1=sqrt(temp);

image2=-image1;

cout<<"两个虚根"<

cout<<"x1="<

cout<<"x2="<

}

voidmain()

{

inta,b,c;

doubletemp;

cout<<"输入a,b,c的值"<

cin>>a>>b>>c;

cout<<"方程为:

"<

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

if(temp>0)

equation_1(a,b,c);

if(temp==0)

equation_2(a,b,c);

if(temp<0)

equation_3(a,b,c);

}

2.定义函数up(ch),如字符变量ch是小写字母就转换成大写字母并通过up返回,否则字符ch不改变。

要求在短小而完全的程序中显示这个程序是怎样被调用的。

#include

usingnamespacestd;

charup(charc)

{

if(c>=97&&c<=122)

return(c-32);

else

returnc;

}

voidmain()

{

inti;

charc[15]={'A','v','e','t','E','T','%','&','4','Y','e','i','@','9','^'};

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

cout<

cout<

}

3.编写主程序条用带实数r和整数n两个参数的函数并输出r的n次幂。

#include

#include

doublepower(doublea,intb)

{

inti;

doubleresult=1.0;

for(i=0;i

result=result*a;

returnresult;

}

voidmain()

{

doubler;

intn;

cout<<"r=";

cin>>r;

cout<<"n=";

cin>>n;

cout<

"<

}

4.编写有字符型参数C和整形参数N的函数,让他们显示出由字符C组成的三角形。

其方式为第1行有1个字符C,第2行有2个字符C,等等。

#include

usingnamespacestd;

voidprint_triangle(charc,intn)

{

inti,j;

for(i=0;i

{

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

{

cout<

}

cout<

}

}

voidmain()

{

print_triangle('a',10);

}

5.编写一个ieqiu字符串长度的函数,strlen(),再用strlen()函数编写一个函数revers(s)的倒序递归程序,使字符串s逆序。

#include

#include

usingnamespacestd;

intstrlen(char*str)

{

intlen=0;

while(str[len]!

='\0')

{

len++;

}

returnlen;

}

voidrevers(char*b)

{

charc;

intj,len;

len=strlen(b);

j=len/2-1;

while(j>=0)

{

c=*(b+j);

*(b+j)=*(b+len-j-1);

*(b+len-j-1)=c;

j--;

}

b[len]='\0';

}

voidmain()

{

charstr[]={"1234567890"};

cout<

"<

cout<

revers(str);//

cout<

}

6.用函数模板实现3个数值中按最小值到最大值排序的程序。

#include

usingnamespacestd;

template

voidsort(Ta,Tb,Tc)

{

Tarray[3],temp;

inti,j;

array[0]=a;

array[1]=b;

array[2]=c;

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

{

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

if(array[j]>array[j+1])

{

temp=array[j];

array[j]=array[j+1];

array[j+1]=temp;

}

}

cout<

}

voidmain()

{

sort(5,1,9);

}

7.利用函数模板设计一个求数组元素中和的函数,并检验之。

#include

usingnamespacestd;

template

Tsum(Ta[],intn)

{

inti;

Ts=0;

for(i=0;i

s=s+a[i];

returns;

}

voidmain()

{

inta[5]={1,2,3,4,5};

ints=sum(a,5);

cout<

}

8.重载上题中的函数模板,使他能够进行两个数组的求和。

#include

usingnamespacestd;

template

Tsum(Ta[],intn)

{

inti;

Ts=0;

for(i=0;i

s=s+a[i];

returns;

}

template//重载上面的模板

Tsum(Ta[],intn,Tb[],intm)

{

returnsum(a,n)+sum(b,m);

}

voidmain()

{

inta[5]={1,2,3,4,5};

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

ints1=sum(a,5);

ints2=sum(b,10);

ints3=sum(a,5,b,10);

cout<

cout<

cout<

}

第四章

一、填空题

1.数据成员、成员函数;

2.类、析构函数不允许有参数和返回类型(可是显示地说明参数为void)、一个类有1个析构函数;

3.fun:

:

fun(fun&)、fun:

:

fun(constfun&);

二、单项选择题

1.C。

2.C。

3.没又答案,应该是A:

:

~A(void)、或A:

:

~A()。

4.B。

5.C。

6.C。

7.D

三、改错题

1.returnm;---错误,没又定义变量m;

2.A.init(24,56);---错误,没有声明对象A,init函数参数不对应;

Setx函数是int型但是没有返回值

四、完成程序题

1.

#include

usingnamespacestd;

classbase

{

private:

//私有数据成员

inta,b;

public:

voidinit(intx,inty)//公有函数

{

a=x;

b=y;

}

voidprint()

{

cout<<"2*"<

}

};

voidmain()

{

basea;

a.init(68,55);

a.print();

}

2.

#include

usingnamespacestd;

classPoint

{

private:

intm,n;

public:

Point(int,int);//整型变量,为参数的构造函数

Point(Point&);//复制构造函数的原型

print()

{

cout<<"m="<

}

};

Point:

:

Point(inta,intb)

{

m=a;

n=b;

}

Point:

:

Point(Point&t)//复制构造函数的定义

{

m=t.m;

n=t.n;

}

voidmain()

{

Pointa(10,89);

Pointb(a);

a.print();

b.print();

}

五、程序分析题

1.没有结果,因为没有main函数

如果加main函数

voidmain()

{

baseb(10,20);

}

输出:

初始化...10,20

Destory...10,20

2.

输出:

55

六、编程题

1.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。

并可以输出4个坐标值和面积。

使用测试程序验证程序。

#include

usingnamespacestd;

classPoint//点类

{

private:

intx,y;//私有成员变量,坐标

public:

Point()//无参数的构造方法,对xy初始化

{

x=0;

y=0;

}

Point(inta,intb)//又参数的构造方法,对xy赋值

{

x=a;

y=b;

}

voidsetXY(inta,intb)//设置坐标的函数

{

x=a;

y=b;

}

intgetX()//得到x的方法

{

returnx;

}

intgetY()//得到有的函数

{

returny;

}

};

classRectangle//矩形类

{

private:

Pointpoint1,point2,point3,point4;//私有成员变量,4个点的对象

public:

Rectangle();//类Point的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了

Rectangle(Pointone,Pointtwo)//用点对象做初始化的,构造函数,1和4为对角顶点

{

point1=one;

point4=two;

init();

}

Rectangle(intx1,inty1,intx2,inty2)//用两对坐标做初始化,构造函数,1和4为对角顶点

{

point1.setXY(x1,y1);

point4.setXY(x2,y2);

init();

}

voidinit()//给另外两个点做初始化的函数

{

point2.setXY(point4.getX(),point1.getY());

point3.setXY(point1.getX(),point4.getY());

}

voidprintPoint()//打印四个点的函数

{

cout<<"A:

("<

cout<<"B:

("<

cout<<"C:

("<

cout<<"D:

("<

}

intgetArea()//计算面积的函数

{

intheight,width,area;

height=point1.getY()-point3.getY();

width

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

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2