C++语言程序设计课后答案.docx

上传人:b****3 文档编号:5061715 上传时间:2022-12-12 格式:DOCX 页数:38 大小:23.98KB
下载 相关 举报
C++语言程序设计课后答案.docx_第1页
第1页 / 共38页
C++语言程序设计课后答案.docx_第2页
第2页 / 共38页
C++语言程序设计课后答案.docx_第3页
第3页 / 共38页
C++语言程序设计课后答案.docx_第4页
第4页 / 共38页
C++语言程序设计课后答案.docx_第5页
第5页 / 共38页
点击查看更多>>
下载资源
资源描述

C++语言程序设计课后答案.docx

《C++语言程序设计课后答案.docx》由会员分享,可在线阅读,更多相关《C++语言程序设计课后答案.docx(38页珍藏版)》请在冰豆网上搜索。

C++语言程序设计课后答案.docx

C++语言程序设计课后答案

第二章C++简单程序设计

2-10执行完下列语句后,a、b、c三个变量的值为多少?

a=30;

b=a++;

c=++a;

解:

a:

32;b:

30;c:

32;

2-13写一条for语句,计数条件为n从100到200,步长为2;然后用while和do…while语句完成同样的循环。

解:

for循环:

for(intn=100;n<=200;n+=2);

while循环:

intx=100;

while(n<=200)

n+=2;

do…while循环:

intn=100;

do

{

n+=2;

}while(n<=200);

2-17修改下面这个程序中的错误,改正后它的运行结果是什么?

#include

voidmain()

inti

intj;

i=10;/*给i赋值

j=20;/*给j赋值*/

cout<<"i+j=<

return0;

}

解:

改正:

#include

intmain()

{

inti;

intj;

i=10;//给i赋值

j=20;/*给j赋值*/

cout<<"i+j="<

return0;

}

程序运行输出:

i+j=30

2-18编写一个程序,运行时提示输入一个数字,再把这个数字显示出来。

解:

源程序:

#include

intmain()

{

inti;

cout<<"请输入一个数字:

";

cin>>i;

cout<<"您输入一个数字是"<

return0;

}

程序运行输出:

请输入一个数字:

5

您输入一个数字是5

2-20打印ASCII码为32~127的字符。

解:

#include

intmain()

{

for(inti=32;i<128;i++)

cout<<(char)i;

return0;

}

程序运行输出:

!

"#$%G'()*+,./0123456789:

;<>?

@ABCDEFGHIJKLMNOP_QRSTUVWXYZ[\]^'abcdefghijklmnopqrstuvwxyz<|>~s

2-21运行下面的程序,观察其输出,与你的设想是否相同?

#include

intmain()

{

unsignedintx;

unsignedinty=100;

unsignedintz=50;

x=y-z;

cout<<"Differenceis:

"<

x=z-y;

cout<<"\nNowdifferenceis:

"<

return0;

}

解:

程序运行输出:

Differenceis:

50

Nowdifferenceis:

4294967246

注意,第二行的输出并非-50,注意x、y、z的数据类型。

2-22运行下面的程序,观察其输出,体会i++与++i的差别。

#include

intmain()

{

intmyAge=39;//initializetwointegers

intyourAge=39;

cout<<"Iam:

"<

cout<<"Youare:

"<

myAge++;//postfixincrement

++yourAge;//prefixincrement

cout<<"Oneyearpasses...\n";

cout<<"Iam:

"<

cout<<"Youare:

"<

cout<<"Anotheryearpasses\n";

cout<<"Iam:

"<

cout<<"Youare:

"<<++yourAge<<"yearsold\n";

cout<<"Let'sprintitagain.\n";

cout<<"Iam:

"<

cout<<"Youare:

"<

return0;

}

解:

程序运行输出:

Iam39yearsold

Youare39yearsold

Oneyearpasses

Iam40yearsold

Youare40yearsold

Anotheryearpasses

Iam40yearsold

Youare41yearsold

Let'sprintitagain

Iam41yearsold

Youare41yearsold

2-28编写一个完整的程序,实现功能:

向用户提问"现在正在下雨吗?

",提示用户输入Y或N。

若输入为Y,显示"现在正在下雨。

";若输入为N,显示"现在没有下雨。

";否则继续提问"现在正在下雨吗?

"

解:

源程序:

#include

#include

voidmain()

{

charflag;

while

(1)

{

cout<<"现在正在下雨吗?

(YesorNo):

";

cin>>flag;

if(toupper(flag)=='Y')

{

cout<<"现在正在下雨。

";

break;

}

if(toupper(flag)=='N')

{

cout<<"现在没有下雨。

";

break;

}

}

}

程序运行输出:

现在正在下雨吗?

(YesorNo):

x

现在正在下雨吗?

(YesorNo):

l

现在正在下雨吗?

(YesorNo):

q

现在正在下雨吗?

(YesorNo):

n

现在没有下雨。

或:

现在正在下雨吗?

(YesorNo):

y

现在正在下雨。

2-29编写一个完整的程序,运行时向用户提问"你考试考了多少分?

(0~100)",接收输入后判断其等级,显示出来。

规则如下:

解:

#include

voidmain()

{

inti,score;

cout<<"你考试考了多少分?

(0~100):

";

cin>>score;

if(score>100||score<0)

cout<<"分数值必须在0到100之间!

";

else

{

i=score/10;

switch(i)

{

case10:

case9:

cout<<"你的成绩为优!

";

break;

case8:

cout<<"你的成绩为良!

";

break;

case7:

case6:

cout<<"你的成绩为中!

";

break;

default:

cout<<"你的成绩为差!

";

}

}

}

程序运行输出:

你考试考了多少分?

(0~100):

85

你的成绩为良!

2-31用穷举法找出1~100间的质数,显示出来。

分别使用while,do-while,for循环语句实现。

解:

源程序:

使用while循环语句:

#include

#include

voidmain()

{

inti,j,k,flag;

i=2;

while(i<=100)

{

flag=1;

k=sqrt(i);

j=2;

while(j<=k)

{

if(i%j==0)

{

flag=0;

break;

}

j++;

}

if(flag)

cout<

i++;

}

}

使用do…while循环语句:

#include

#include

voidmain()

{

inti,j,k,flag;

i=2;

do{

flag=1;

k=sqrt(i);

j=2;

do{

if(i%j==0)

{

flag=0;

break;

}

j++;

}while(j<=k);

if(flag)

cout<

i++;

}while(i<=100);

}

使用for循环语句:

#include

#include

voidmain()

{

inti,j,k,flag;

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

{

flag=1;

k=sqrt(i);

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

{

if(i%j==0)

{

flag=0;

break;

}

}

if(flag)

cout<

}

}

程序运行输出:

2是质数.

3是质数.

5是质数.

7是质数.

11是质数.

13是质数.

17是质数.

19是质数.

23是质数.

29是质数.

31是质数.

37是质数.

41是质数.

43是质数.

47是质数.

53是质数.

59是质数.

61是质数.

67是质数.

71是质数.

73是质数.

79是质数.

83是质数.

89是质数.

97是质数.

2-33定义一个表示时间的结构体,可以精确表示年、月、日、小时、分、秒;提示用户输入年、月、日、小时、分、秒的值,然后完整地显示出来。

解:

源程序见"实验指导"部分实验二

2-34在程序中定义一个整型变量,赋以1~100的值,要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

分别使用while、do…while语句实现循环。

解:

//使用while语句

#include

voidmain(){

intn=18;

intm=0;

while(m!

=n)

{

cout<<"请猜这个数的值为多少?

(0~~100):

";

cin>>m;

if(n>m)

cout<<"你猜的值太小了!

"<

elseif(n

cout<<"你猜的值太大了!

"<

else

cout<<"你猜对了!

"<

}

}

//使用do…while语句

#include

voidmain(){

intn=18;

intm=0;

do{

cout<<"请猜这个数的值为多少?

(0~~100):

";

cin>>m;

if(n>m)

cout<<"你猜的值太小了!

"<

elseif(n

cout<<"你猜的值太大了!

"<

else

cout<<"你猜对了!

"<

}while(n!

=m);

}

程序运行输出:

请猜这个数的值为多少?

(0~~100):

50

你猜的值太大了!

请猜这个数的值为多少?

(0~~100):

25

你猜的值太大了!

请猜这个数的值为多少?

(0~~100):

10

你猜的值太小了!

请猜这个数的值为多少?

(0~~100):

15

你猜的值太小了!

请猜这个数的值为多少?

(0~~100):

18

你猜对了!

第三章函数

3-2观察下面程序的运行输出,与你设想的有何不同?

仔细体会引用的用法。

源程序:

#include

intmain()

{

intintOne;

int&rSomeRef=intOne;

intOne=5;

cout<<"intOne:

\t\t"<

cout<<"rSomeRef:

\t"<

intintTwo=8;

rSomeRef=intTwo;//notwhatyouthink!

cout<<"\nintOne:

\t\t"<

cout<<"intTwo:

\t\t"<

cout<<"rSomeRef:

\t"<

return0;

}

程序运行输出:

intOne:

5

rSomeRef:

5

intOne:

8

intTwo:

8

rSomeRef:

8

3-7编写函数,参数为两个unsignedshortint型数,返回值为第一个参数除以第二个参数的结果,数据类型为shortint;如果第二个参数为0,则返回值为-1。

在主程序中实现输入输出。

解:

源程序:

#include

shortintDivider(unsignedshortinta,unsignedshortintb)

{

if(b==0)

return-1;

else

returna/b;

}

typedefunsignedshortintUSHORT;

typedefunsignedlongintULONG;

intmain()

{

USHORTone,two;

shortintanswer;

cout<<"Entertwonumbers.\nNumberone:

";

cin>>one;

cout<<"Numbertwo:

";

cin>>two;

answer=Divider(one,two);

if(answer>-1)

cout<<"Answer:

"<

else

cout<<"Error,can'tdividebyzero!

";

return0;

}

程序运行输出:

Entertwonumbers.

Numberone:

8

Numbertwo:

2

Answer:

4

3-8编写函数把华氏温度转换为摄氏温度,公式为:

C=(F-32)*5/9;在主程序中提示用户输入一个华氏温度,转化后输出相应的摄氏温度。

解:

源程序见"实验指导"部分实验三

3-10编写函数求两个整数的最大公约数和最小公倍数。

解:

源程序:

#include

#include

intfn1(inti,intj);//求最大公约数的函数

voidmain()

{

inti,j,x,y;

cout<<"请输入一个正整数:

";

cin>>i;

cout<<"请输入另一个正整数:

";

cin>>j;

x=fn1(i,j);

y=i*j/x;

cout<

"<

cout<

"<

}

intfn1(inti,intj)

{

inttemp;

if(i

{

temp=i;

i=j;

j=i;

}

while(j!

=0)

{

temp=i%j;

i=j;

j=temp;

}

returni;

}

程序运行输出:

请输入一个正整数:

120

请输入另一个正整数:

72

120和72的最大公约数是:

24

120和72的最小公倍数是:

360

3-12在主程序中提示输入整数n,编写函数用递归的方法求1+2+…+n的值。

解:

#include

#include

intfn1(inti);

voidmain()

{

inti;

cout<<"请输入一个正整数:

";

cin>>i;

cout<<"从1累加到"<

"<

}

intfn1(inti)

{

if(i==1)

return1;

else

returni+fn1(i-1);

}

程序运行输出:

请输入一个正整数:

100

从1累加到100的和为:

5050

3-14用递归的方法编写函数求Fibonacci级数,公式为fib(n)=fib(n-1)+fib(n-2),n>2;

fib

(1)=fib

(2)=1;观察递归调用的过程。

解:

源程序见"实验指导"部分实验三

3-15用递归的方法编写函数求n阶勒让德多项式的值,在主程序中实现输入、输出;

解:

#include

floatp(intn,intx);

voidmain()

{

intn,x;

cout<<"请输入正整数n:

";

cin>>n;

cout<<"请输入正整数x:

";

cin>>x;

cout<<"n="<

cout<<"x="<

cout<<"P"<

}

floatp(intn,intx)

{

if(n==0)

return1;

elseif(n==1)

returnx;

else

return((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;

}

程序运行输出:

请输入正整数n:

1

请输入正整数x:

2

n=1

x=2

P1

(2)=2

请输入正整数n:

3

请输入正整数x:

4

n=3

x=4

P3(4)=154

第四章类

4-9设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。

解:

源程序:

#include

classRectangle

{

public:

Rectangle(inttop,intleft,intbottom,intright);

~Rectangle(){}

intGetTop()const{returnitsTop;}

intGetLeft()const{returnitsLeft;}

intGetBottom()const{returnitsBottom;}

intGetRight()const{returnitsRight;}

voidSetTop(inttop){itsTop=top;}

voidSetLeft(intleft){itsLeft=left;}

voidSetBottom(intbottom){itsBottom=bottom;}

voidSetRight(intright){itsRight=right;}

intGetArea()const;

private:

intitsTop;

intitsLeft;

intitsBottom;

intitsRight;

};

Rectangle:

:

Rectangle(inttop,intleft,intbottom,intright)

{

itsTop=top;

itsLeft=left;

itsBottom=bottom;

itsRight=right;

}

intRectangle:

:

GetArea()const

{

intWidth=itsRight-itsLeft;

intHeight=itsTop-itsBottom;

return(Width*Height);

}

intmain()

{

RectangleMyRectangle(100,20,50,80);

intArea=MyRectangle.GetArea();

cout<<"Area:

"<

return0;

}

程序运行输出:

Area:

3000

UpperLeftXCoordinate:

20

4-11定义一个矩形类,有长、宽两个属性,有成员函数计算矩形的面积

解:

#include

classRectangle

{

public:

Rectangle(floatlen,floatwidth)

{

Length=len;

Width=width;

}

~Rectangle(){};

floatGetArea(){returnLength*Width;}

floatGetLength(){returnLength;}

floatGetWidth(){returnWidth;}

private:

floatLength;

floatWidth;

};

voidmain()

{

floatlength,width;

cout<<"请输入矩形的长度:

";

cin>>length;

cout<<"请输入矩形的宽度:

";

cin>>width;

Rectangler(length,width);

cout<<"长为"<

"

<

}

程序运行输出:

请输入矩形的长度:

5

请输入矩形的宽度:

4

长为5

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

当前位置:首页 > 法律文书 > 调解书

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

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