60304C++程序设计案例教程习题答案.docx

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

60304C++程序设计案例教程习题答案.docx

《60304C++程序设计案例教程习题答案.docx》由会员分享,可在线阅读,更多相关《60304C++程序设计案例教程习题答案.docx(52页珍藏版)》请在冰点文库上搜索。

60304C++程序设计案例教程习题答案.docx

60304C++程序设计案例教程习题答案

C++程序设计案例教程部分习题答案

习题一

1.运行结果:

enterij:

98(从键盘输入9和8)

i=9,j=8

i+j=17

i-j=1

i*j=72

2.运行结果:

5634(从键盘输入)

max(56,34)=56

3.错误:

(1)没有包含头文件;

(2)main()函数没有返回值

4.错误:

(1)缺少语句:

usingnamespacestd;

(2)main()函数没有返回值;(3)变量a没有声明;(4)语句:

cout<<"b=<

cout<<"b="<

5.错误:

(1)缺少语句:

usingnamespacestd;

(2)main()函数应该包含return语句;(3)COUT不应该大写;(4)变量j没有初始化

6.运行结果:

您好!

在哪儿呢?

在沈阳.

一会儿见!

习题二

一、程序阅读

1.运行结果:

Enterab:

158(从键盘输入15和8)

d=-7

2.运行结果:

A=8

CH+2=m

D-5.8=2.7

3.运行结果:

0,1

1,0

注意此题书中有错误:

将最后一行语句改为:

return0;

4.运行结果:

32

5.运行结果:

3,74

6.运行结果:

13898$

7.运行结果:

9

8

7

8.运行结果:

7

9

11

13

15

ok!

9.运行结果:

7

5

3

1

-1

10.运行结果:

10

二、编程题

1.程序如下:

#include

usingnamespacestd;

intmain()

{

doublea,b,c,d,e,sum,avg;

cin>>a>>b>>c>>d>>e;

sum=a+b+c+d+e;

avg=sum/5;

cout<<"sum="<

cout<<"avg="<

return0;

}

2.程序如下:

#include

usingnamespacestd;

intmain()

{

inta,b,x,y;

cin>>a>>b;

x=(a+b)*(a+b);

y=a*a+2*a*b+b*b;

if(x=y)cout<<"yes"<

elsecout<<"no"<

return0;

}

3.程序如下:

#include

usingnamespacestd;

intmain()

{

chara[100],*p;

cout<<"Input:

";

cin>>a;

cout<<"Output:

";

for(p=strlen(a)+a-1;p>=a;--p)

putchar(*p);

cout<

return0;

}

4.程序如下:

#include

usingnamespacestd;

intmain()

{

intn,sum=0;

for(n=1;n<=100;n=n+2)

sum=sum+n;

cout<<"sum="<

return0;

}

5.程序如下:

#include

usingnamespacestd;

intmain()

{

inta,b,num1,num2,temp;

cout<<"pleaseinputtwonumbers"<

cin>>num1>>num2;

if(num1)

{

temp=num1;

num1=num2;

num2=temp;

}

a=num1;

b=num2;

while(b!

=0)

{

temp=a%b;

a=b;

b=temp;

}

cout<<"最大公约数="<

cout<<"最小公倍数="<

return0;

}

6.程序如下:

#include

usingnamespacestd;

intmain()

{

intn,a;

intresult=0;

cout<<"请输入n的值:

";

cin>>n;

for(inti=n;i>=1;i--)

{

a=0;

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

a+=j;

result+=a;

}

cout<<"最终结果为:

"<

return0;

}

7.程序如下:

#include

usingnamespacestd;

intmain()

{

intn,t,number=15;

doublea=2,b=1,s=0;

for(n=1;n<=number;n++)

{

s=s+a/b;

t=a;

a=a+b;

b=t;

}

cout<<"sumis:

"<

return0;

}

8.程序如下:

#include

usingnamespacestd;

intmain()

{

doublee=1;

doublejc=1;

inti=1;

while(1/jc>=1e-6)

{

e=e+1/jc;

i++;

jc=jc*i;

}

cout<<"e="<

return0;

}

习题三

一、填空题

1.x

2.p=newint(10)

3.10、19

4.new、delete

5.b[9]、b[1]

二、选择题

1.D2.D3.B4.B5.B6.A

三、程序阅读

1.运行结果:

1

11

121

1331

14641

15101051

1615201561

172135352171

2.运行结果:

intone:

5

rsomeref:

5

intone:

7

rsomeref:

7

3.运行结果:

BCDEFGHIJKLMNOPQRST

DEFGHIJKLMNOPQRST

T

T

R

RST

P

PQRST

N

NOPQRST

4.运行结果:

9

19

19

3

01231516171819

1918171615

5.运行结果:

2

四、编程题

1.程序如下:

#include

usingnamespacestd;

intmain()

{

intarray[10];

inti,max,min;

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

cin>>array[i];

max=array[0];min=array[0];

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

{

if(array[i]>max)

max=array[i];

if(array[i]

min=array[i];

}

cout<<"Thelargestnumberis"<

cout<<"Thesmallestnumberis"<

return0;

}

2.程序如下:

#include

usingnamespacestd;

intmain()

{

inta[3][5]={0};

intb[5][4]={0};

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

inti,j,k;

cout<<"请输入一个3行5列的矩阵:

"<

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

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

cin>>a[i][j];

cout<<"请输入一个5行4列的矩阵:

"<

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

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

cin>>b[i][j];

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

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

{

c[i][j]=0;

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

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

}

cout<<"矩阵的乘积:

"<

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

{

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

{

cout<

}

cout<

}

return0;

}

3.程序如下:

#include

usingnamespacestd;

voidalter(float&x,float&y)

{

floatm,n;

m=x;n=y;

x=m*n;

y=m+n;

}

intmain()

{

floata,b;

cout<<"Pleaseinputtwonumbers:

"<

cin>>a>>b;

alter(a,b);

cout<<"Afterchanged:

"<

cout<

return0;

}

习题四

一、填空题

1.参数传递、执行函数体、返回调用位置

2.常量、const

3.Fun(5,100)

4.inline

5.程序域、文件域、函数域和块域

二、选择题

1.A2.D3.C4.C5.D6.A7.A8.A9.D10.A

三、程序阅读

1.运行结果:

a=3b=2

2.运行结果:

x=50,y=30

3.运行结果:

b=16.0988

inputa=2(从键盘输入2)

F(a)=-0.1

4.运行结果:

Sumof4powersofintegersfrom1to6=2275

5.运行结果:

6,6,6

6.运行结果:

10

7.运行结果:

InMain—x=5,y=1,n=1

InFun—x=6,y=14,n=4

InMain—x=5,y=1,n=4

四、编程题

1.程序如下:

#include

usingnamespacestd;

intmain()

{

intidec;

intibase;

charstrdr[20],t;

inti,idr,p=0;

cout<<"请输入一个进制数及要转换的进制数:

"<

cin>>idec>>ibase;

while(idec!

=0)

{

idr=idec%ibase;

if(idr>=10)strdr[p++]=idr-10+65;

elsestrdr[p++]=idr+48;

idec/=ibase;

}

for(i=0;i

{

t=strdr[i];

strdr[i]=strdr[p-i-1];

strdr[p-i-1]=t;

}

strdr[p]='\0';

cout<

return0;

}

2.程序如下:

#include

usingnamespacestd;

intmain()

{

intnum,i=0,temp,flag=0;

cin>>num;

while

(1)

{temp=num%10;

if(temp%2==0)

{flag++;break;}

num/=10;

if(num==0)

break;

}

if(flag)

cout<<"NO"<

else

cout<<"YES"<

return0;

}

3.程序如下:

#include

#include

usingnamespacestd;

intmain()

{

intn,i;

doublesum=0;

cout<<"Pleaseinputanumber(1,2,3,4or5):

"<

cin>>n;

for(i=1;i<=20;i++)

sum+=pow(n,i);

cout<

return0;

}

4.程序如下:

#include

usingnamespacestd;

intmain()

{

intnum,digit;

cout<<"输入一个整数:

";

cin>>num;

cout<<"整数"<

";

do

{

digit=num%10;

cout<

num/=10;

}while(num);

cout<

return0;

}

5.程序如下:

#include

usingnamespacestd;

#include

intmain()

{

constintN=5;

chara[N][100]={0};

chart[100]={0};

inti,j;

for(i=0;i

{

cin>>a[i];

}

for(i=0;i

for(j=N-1;j>i;j--)

{

if(strcmp(a[j],a[j-1])<0)

{

strcpy(t,a[j]);

strcpy(a[j],a[j-1]);

strcpy(a[j-1],t);

}

}

for(i=0;i

{

cout<

}

return0;

}

习题五

一、填空题

1.数据成员、成员函数

2.private、protected、public

3.private、public

4.自动、创建并初始化对象

5.构造函数

6.析构函数

7.友元函数

8.堆对象

9.this、正在被成员函数操作的对象

10.10、10

二、选择题

1.D2.C3.D4.A5.C6.C7.C8.C

9.A10.C11.B12.B13.C14.A15.D

三、程序阅读

1.有四处错误:

(1)inti=0;数据成员在类的定义中不能直接初始化

(2)voidSample();构造函数没有返回值类型;

(3)~Sample(intvalue);析构函数无参数;

(4)析构函数不能重载。

2.有四处错误:

(1)Sample(inta=0,b=1);参数b没有说明类型;

(2)disp();没有给出返回值类型;

(3)void~Sample(inta);析构函数不能有返回值类型,也不能有参数;

(4)Sample:

:

Sample(inta=0,intb=1)默认值重复定义,只需在函数说明中给出即可。

3.有二处错误:

(1)Sample(inta){X=a;}常数据成员只能用初始化列表的形式进行初始化;

(2)a.Print();常对象只能调用常成员函数。

4.无错误,运行结果如下:

Const:

X=10

X=20

5.运行结果:

2,5

10,20

6.运行结果:

2234

7.运行结果:

3

8.运行结果:

E2-C16-

四、编程题

1.程序如下:

#include

usingnamespacestd;

classDate

{

public:

voidSetDate(inty,intm,intd)

{

year=y;

month=m;

day=d;

}

intAddDay()

{

returnday++;

}

voidDisplay()

{

cout<

}

private:

intyear,month,day;

};

intmain()

{

Datetoday,tomorrow;

today.SetDate(2010,7,20);

today.AddDay();

tomorrow=today;

tomorrow.Display();

return0;

}

2.程序如下:

#include

usingnamespacestd;

classRectangle

{

public:

Rectangle(doublea=1,doubleb=1)

{

length=a;

width=b;

}

voidSetValue(doublem,doublen)

{

length=m;

width=n;

}

doublePerimeter()

{

return2*(length+width);

}

doubleArea()

{

returnlength*width;

}

private:

doublelength,width;

};

intmain()

{

Rectangler;

r.SetValue(10,20);

cout<

cout<

return0;

}

3.程序如下:

#include

usingnamespacestd;

classTime

{

public:

Time();

Time(inth,intm);

voidSetTime(inth1,intm1)

{

hour=h1;

minute=m1;

}

friendvoidGetTime12(Time&t);

friendvoidGetTime24(Time&t);

private:

inthour,minute;

};

Time:

:

Time()

{

hour=0;

minute=0;

}

Time:

:

Time(inth,intm)

{

hour=h;

minute=m;

}

voidGetTime12(Time&t)

{

boolpm;

inthours;

if(t.hour>12)

{

hours=t.hour-12;

pm=true;

}

else

{

hours=t.hour;

pm=false;

}

if(pm)

cout<<"Timeis"<

"<

else

cout<<"Timeis"<

"<

}

voidGetTime24(Time&t)

{

cout<<"Timeis"<

"<

}

intmain()

{

Timet1,t2;

t2.SetTime(16,20);

GetTime12(t1);

GetTime24(t1);

GetTime12(t2);

GetTime24(t2);

return0;

}

4.程序如下:

#include

#include

usingnamespacestd;

classStudent

{

public:

Student(){};

voidGetName(stringn);

voidGetGender(charg);

voidGetAge(inta);

voidDisplay()

{

cout<<"Name:

"<

cout<<"Gender:

"<

cout<<"Age:

"<

}

private:

stringname;

chargender;

intage;

};

voidStudent:

:

GetName(stringn)

{

name=n;

}

voidStudent:

:

GetGender(charg)

{

gender=g;

}

voidStudent:

:

GetAge(inta)

{

age=a;

}

intmain()

{

Students;

stringsname;

charsgender;

intsage;

cout<<"Pleaseinputname:

"<

cin>>sname;

s.GetName(sname);

cout<<"Pleaseinputgender:

"<

cin>>sgender;

s.GetGender(sgender);

cout<<"Pleaseinputage:

"<

cin>>sage;

s.GetAge(sage);

s.Display();

return0;

}

5.程序如下:

#include

usingnamespacestd;

classStudent

{

public:

Student()

{

score=0;

}

Student(doubles)

{

score=s;

totalscore+=score;

count++;

}

staticdoubleTotalSum()

{

returntotalscore;

}

staticdoubleAverage()

{

returntotalscore/count;

}

private:

doublescore;

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

当前位置:首页 > 小学教育 > 语文

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

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