C_primer_plus(第五版)课后编程练习答案(完整).docx
《C_primer_plus(第五版)课后编程练习答案(完整).docx》由会员分享,可在线阅读,更多相关《C_primer_plus(第五版)课后编程练习答案(完整).docx(145页珍藏版)》请在课桌文档上搜索。
1、第一章概览编程练习1.您刚刚被MaCroMUSCIe(SoftwareforHardBodies)聘用。该公司要进入欧洲市场,需要一个将英寸转换为厘米(1英寸=2.54Cm)的程序。他们希望建设的该程序可提示用户输入英寸值。您的工作是定义程序目标并设计该程序(编程过程的第1步和第2步)。1.将英寸值转化为厘米值2.显示“输入英寸值得到该值-转换为厘米值-存储告知用户已完毕第二章C语言概述编程练习1 .编写一个程序,调用Printf()函数在一行上输出您的名和姓,再调用一次PrintfO函数在两个单独的行上输出您的名和姓,然后调用一对Printf()函数在一行上输出您的名和姓。输出应如下所示(当
2、然里面要换成您的姓名:AntonBrucknerAntonBrucknerAntonBruckner第一个输出语句第二个输出语句仍然是第二个输出语句第三个和第四个输出语句8includeintmain(void)(printf(*HeJinn*);printf(*Hen*);printf(*Jinn*);printf(*HeJinn*);return(0);2 .编写一个程序输出您的姓名及地址。Aincludeintmain(void)(Printf(Name:HeJinn*);Printf(Address:CAUCn);return(0);3 .编写一个程序,把您的年龄转换成天数并显示二者的
3、值。不用考虑平年(fractionalyear)和闰年(leapyear)的问题。ftincludeintmain(void)Iintage=22;printf(*Age:%dn*,age);printf(*Day:%dn*,age*356);return(O);4 .编写一个能够产生下面输出的程序:Forhe,sajollygoodfellow!Forhe,sajollygoodfellow!Forhe,sajollygoodfellow!Whichnobodycandeny!程序中除了main。函数之外,要使用两个用户定义的函数:一个用于把上面的夸奖消息输出一次:另一个用于把最后一行输出一
4、次。8includevoidprintfl(void);voidprintf2(void);intmain(void)(printfl();printfl();printfl();printf2();return(0);voidprintfl(void)(printf(*Forhe,sajollygoodfellow!n*);voidprintf2(void)(printf(*Whichnobodycandeny!n*);5 .编写一个程序,创立一个名为toes的整数变量。让程序把toes设置为10。再让程序计算两个toes的和以及toes的平方。程序应该输出所有的3个值,并分别标识它们。#i
5、ncludeintmain(void)(inttoes=10;inttoesadd;inttoessquare;toes_add=toes+toes;toes_square=toes*toes;printf(*toes=%dntoes_add=%dntoes_square=%dn*,toes,toes_add,toes_square);return(0);)6 .编写一个能够产生以下输出的程序:Smile!Smile!SmileSmile!Smile!Smile!在程序中定义一个能显示字符串Smile卜一次的函数,并在需要时使用该函数。ttincludevoiddisplay(void);in
6、tmain(void)(display();display();display();printf(*n*);display();display();printf(*n*);display();printf(*n*);return(0);voiddisplay(void)(printf(*SmiIe!*);7 .编写一个程序,程序中要调用名为OnJthree()的函数。该函数要在一行中显示单词one,再调用two()函数,然后再在另一行中显示单词three。函数two。应该能在一行中显示单词twomain()函数应该在调用。nejhree()函数之前显示短语startingnow::函数调用之后
7、要显示done!。这样,最后的输出结果应如下所示:startingnowthreedone!AincludeCstdio.hvoidonethree(void);voidtwo(void);intmain(void)Printf(startingnow:n*);one_three();printf(*done!n*);return(0);voidone_three(void)printf(*0nen*);two();printf(*Threen*);voidtwo(void)(printf(*Twon*);第三章数据和C编程练习8 .通过试验的方法(即编写带有此类问题的程序)观察系统假设何处理
8、整数上溢、浮点数上溢和浮点数下溢的情况。#includeintmain(void)(unsignedinta=4294967295;floatb=3.4E38;floatc=b*10:floatd=0.1234E-2;printf(*%u+l=%un*,a,a+l);printf(*%e*10=%en*,b,c);printf(*%f10=%fn*,d,d10);return(0);9 .编写一个程序,要求输入一个ASCH码值(如66),然后输出相应的字符。8includeintmain(void)(chara;scanf(*%d*,&a);printf(*%cn*,a);return(0):
9、10 编写一个程序,发出警报声,并打印以下文字:Startledbythesuddensound,Sallyshouted,*BytheGreatPumpkin,whatwasthat!”Aincludeintmain(void)(printf(*aStartledbythesuddensound,Sallyshouted,VBytheGreatpumpkin,whatwasthat!z,n*);return(0);11 编写一个程序,读入一个浮点数,并分别以小数形式和指数形式打印。输出应如同下面格式(实际显示的指数位数也许因系统而不同):Theinputis21.290000or2.1290
10、00e+001.8includeintmain(void)(floata;scanf(*%f*,&a);printf(*Theinputis%for%en*,a,a);return(0):12 一年约有3.156X107S。编写一个程序,要求输入您的年龄,然后显示该年龄合多少秒。ftincludeintmain(void)(floata;printf(*Pleaseinputyourage:*);scanf(*%f*,&a);printf(*Yourageis%esecondsn*,a*3.156E7);return(0);)13 1个水分子的质量约为3.OX1(-23g,1夸脱水大约有950
11、g。编写一个程序,要求输入水的夸脱数,然后显示这么多水中包含多少个水分子。ftincludeintmain(void)(floata;printf(*Pleaseinputhowmuchquartsthewateris:*);scanf(*%f*,&a);printf(*%fquartswaterhas%emolecules,n*,a,a*9503E-23);return(0);)14 1英寸等于2.54cm。编写一个程序,要求输入您的身高(以英寸为单位),然后显示该身高值等于多少厘米。如果您愿意,也可以要求以厘米为单位输入身高,然后以英寸为单位进展显示。ftincludeintmain(vo
12、id)(floata;printf(*Pleaseinputyourheightbyinches:*);scanf(*%f*,&a);printf(*Yourheightis%fcm.n*,a*2.54);return(0);)第4章字符串和格式化输入/输出编程练习15 编写一个程序,要求输入名字和姓氏,然后以“名字,姓氏”的格式打印。ftincludeintmain(void)(charsurname20,firstname20;printf(*Pleaseinputyourfirstnameandsurname:*);scanf(*%s*,firstname);scanf(*%s*lsur
13、name);printf(*Youare%s,%s.n”,firstname,surname);return(0);16 编写一个程序,要求输入名字,并执行以下操作:a.把名字引在双引号中打印出来。b.在宽度为20个字符的字段内打印名字,并且整个字段引在引号内。c.在宽度为20个字符的字段的左端打印名字,并且整个字段引在引号内。d.在比名字宽3个字符的字段内打印它。8includeftincludeintmain(void)(charname20;intwidth:printf(*Pleaseinputyourname:*);scanf(*%s*,name);printf(*:*%s*n*,n
14、ame);printf(*B:*%20s*n*,name);printf(*C:*%-20s*n*,name):width=strlen(name)+3:printf(*D:*%*s*n*,width,name);*对应widthreturn(0);17 编写一个程序,读取一个浮点数,并且首先以小数点记数法,然后以指数记数法打印之。输出使用以下形式(在指数位置显示的数字的位数可能会随系统而不同):a. Theinputis21.3or2.le+001.b. Theinputis+21.290or2.129E+001.#includeintmain(void)Ifloata;rintf(*Ple
15、aseinputafloat:*);scanf(*%f*,a);printf(*a.Theinputis%.Ifor%,len*,a,a);rintf(*b.Theinputis%+.3for%.3En”,a,a);return(0);18 编写一个程序,要求输入身高(以英寸为单位)和名字,然后以如下形式显示:Dabney,youare6.208feettall使用float类型,使用/作为除号。如果您愿意,可以要求以厘米为单位输入身高,并以米为单位进展显不。ftincludeintmain(void)(floatheight;charname20;printf(*Pleaseinputyou
16、rheightbyinches:*);scanf(*%f*,&height);printf(*Pleaseinputyourname:);scanf(*%s*,name);printf(*%s,youare%ffeettalln”,name,height/12);return(0);19 编写一个程序,首先要求用户输入名字,然后要求用户输入姓氏。在一行打印输入的姓名,在下行打印每个名字中字母的个数。把字母个数与相应名字的结尾对齐,如下所示:MeIiSSaHoneybee78然后打印一样的信息,但是字母个数与相应单词的开场对齐。8includeftincludeintmain(void)(cha
17、rsurname20,firstname20;printf(*Pleaseinputyourfirstnameandsurname:*);scanf(*%s*,firstname);scanf(*%s*,surname);printf(*%s%s.n*,firstname,surname);printf(*%*d%*dn*,strlen(firstname),strlen(firstname),strIen(surname),strlen(surname);/*对应宽度变量printf(*%s%s.n”,firstname,surname);printf(*%-*d%-*dn*,strlen(
18、firstname),strlen(firstname),strlen(surname),strlen(surname);/*对应宽度变量return(0);20 编写一个程序,设置一个值为1.0/3.0的double类型变量和一个值为1.0/3.0的float类型变量。每个变量的值显示三次:一次在小数点右侧显示4个数字,一次在小数点右侧显示12个数字,另一次在小数点右侧显示16个数字。同时要让程序包括float文件,并显示FLT-DIG和DBiJ)IG的值。L0/3.0的显示值与这些值一致吗ftincludeftincludeintmain(void)(doublea=l.0/3.0;flo
19、atb=l.0/3.0;printf(*doublea=l.0/3.0:n*);printf(*%.4en*,a);printf(*%.12en*,a);printf(*%.16enn*,a);printf(*floatb=l.0/3.0:n*);printf(*%.4fn*,b);printf(*%.12fn*,b);printf(*%.16fnn*,b);printf(*FLT_DIG:%dn*,FLT_DIG);printf(*DBL_DIG:%dn*,DBLDIG);return(0);21 编写一个程序,要求用户输入行驶的英里数和消耗汽油的加仑数。接着应该计算和显示消耗每加仑汽油行驶
20、的英里数,显示方式是在小数点右侧显示一个数字。然后,根据1加仑约等于3.785升,1英里约等于1.609公里的规则,它应该把每加仑英里数转换成每100公里的升数(欧洲通用的燃料消耗表示法),并显示结果,显示方式是在小数点右侧显示一个数字(请注意,美国方案测量每单位距离消耗的燃料数,而欧洲方案测量每单位燃料的行驶距离用符号常量表示两个转换系数(使用ConSt或#(16日加)。ttincludeSdefineGALLON3.785/1gallon=2.785litreSdefineMILE1.609/1mile=1.609kilometerintmain(void)(floatmile,gallo
21、n;printf(*Pleaseinputmilesandgallons:*);scanf(*%f%f*,&mile,&gallon);printf(*Milespergallon:%,lfn*,mile/gallon);printf(*Litresper100kilometre:%,lfn*,gallon*GLL0N(100*mile*MILE);return(0);)第5章运算符、表达式和语句编程练习1.编写一个程序。将用分钟表示的时间转换成以小时和分钟表示的时间。使用#define或者COnSt来创立一个代表60的符号常量。使用WhiIe循环来允许用户重复键入值,并且当键入一个小于等于。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C_primer_plus 第五 课后 编程 练习 答案 完整

链接地址:https://www.desk33.com/p-990220.html