下载

1下载券

加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 C++-primer-plus(第6版)中文版编程练习答案

C++-primer-plus(第6版)中文版编程练习答案.pdf

C++-primer-plus(第6版)中文版编程练习答案

薇薇安天涯客
2017-10-21 0人阅读 举报 0 0 暂无简介

简介:本文档为《C++-primer-plus(第6版)中文版编程练习答案pdf》,可适用于IT/计算机领域

第二章:开始学习Cexdisplayyournameandaddress#includeiostreamintmain(void){usingnamespacestdcoutMynameisliaochunguangandIliveinhunanchenzhounrdquo}exconvertthefurlongunitstoyarduints把浪单位换位码单位#includeiostreamdoublefuryd(double)intmain(){usingnamespacestdcoutenterthedistancemeasuredbyfurlongunits:doublefurcinfurcoutconvertthefurlongtoyardendldoubleydyd=furyd(fur)coutfurfurlongisydyardendlreturn}doublefuryd(doublet){return*t}ex每个函数都被调用两次#includeiostreamvoidmice()voidsee()usingnamespacestdintmain(){mice()mice()see()see()return}voidmice(){coutthreeblindmiceendl}voidsee(){coutseehowtheyrunendl}ex#includeiostreamintmain(){usingnamespacestdcoutEnteryourage:intagecinageintmonthmonth=age*coutageyearsismonthmonthsendlreturn}exconverttheCelsiusvalvetoFahrenheitvalue#includeiostreamdoubleCF(double)intmain(){usingnamespacestdcoutpleaseenteraCelsiusvalue:doubleCcinCdoubleFF=CF(C)coutCdegreesCelsiusisFdegreesFahrenheitendlreturn}doubleCF(doublet){return*t}exconvertthelightyearsvalvetoastronomicalunits把光年转换为天文单位#includeiostreamdoubleconvert(double)函数原型intmain(){usingnamespacestdcoutEnterthenumberoflightyears:doublelightyearscinlightyearsdoubleastrounitsastrounits=convert(lightyears)coutlightyearslightyears=astrounitsastronomicalunitsendlreturn}doubleconvert(doublet){return*t光年=天文单位}ex显示用户输入的小时数和分钟数#includeiostreamvoidshow()main(){usingnamespacestdshow()return}voidshow(){usingnamespacestdinth,mcoutenterthenumberofhours:cinhcoutenterthenumberofminutes:cinmcoutTime:h:mendl}第三章:处理数据exmdash将身高用英尺(feet)和英寸(inch)表示#includeiostreamconstintinchperfeet=const常量feet=inches英尺=英寸intmain(){usingnamespacestdcoutpleaseenteryourheightininches:bbbb表示为退格字符inthtinchcinhtinchinthtfeet=htinchinchperfeet取商intrminch=htinchinchperfeet取余coutyourheightishtfeetfeet,andrminchinchesnreturn}ex计算相应的bodymassindex(体重指数)#includeiostreamconstintinchperfeet=constdoublemeterperinch=constdoublepoundperkilogram=intmain(){usingnamespacestdcoutPleaseenteryourheight:endlcoutFirst,enteryourheightoffeetpart(输入你身高的英尺部分):binthtfeetcinhtfeetcoutSecond,enteryourheightofinchpart(输入你身高的英寸部分):binthtinchcinhtinchcoutNow,pleaseenteryourweightinpound:bbbdoublewtpoundcinwtpoundintinchinch=htfeet*inchperfeethtinchdoublehtmeterhtmeter=inch*meterperinchdoublewtkilogramwtkilogram=wtpoundpoundperkilogramcoutendlcoutYourpensonalbodyinformationasfollows:endlcout身高:inch(英尺inch)n身高:htmeter(米meter)n体重:wtkilogram(千克kilogram)ndoubleBMIBMI=wtkilogram(htmeter*htmeter)coutyourBodyMassIndex(体重指数)isBMIendlreturn}ex以度分秒输入以度输出#includeiostreamconstintminutesperdegree=constintsecondsperminute=intmain(){usingnamespacestdcoutEnteralatitudeindegrees,minutes,andseconds:ncoutFirst,enterthedegrees:intdegreecindegreecoutNext,entertheminutesofarc:intminutecinminutecoutFianlly,enterthesecondsofarc:intsecondcinseconddoubleshowindegreeshowindegree=(double)degree(double)minuteminutesperdegree(double)secondminutesperdegreesecondsperminutecoutdegreedegrees,minuteminutes,secondseconds=showindegreedegreesnreturn}ex#includeiostreamconstinthoursperday=constintminutesperhour=constintsecondsperminute=intmain(){usingnamespacestdcoutEnterthenumberofseconds:longsecondscinsecondsintDay,Hour,Minute,SecondDay=secondssecondsperminuteminutesperhourhoursperdayHour=secondssecondsperminuteminutesperhourhoursperdayMinute=secondssecondsperminuteminutesperhourSecond=secondssecondsperminutecoutsecondsseconds=Daydays,Hourhours,Minuteminutes,Secondsecondsnreturn}ex#includeiostreamintmain(){usingnamespacestdcoutEntertheworldpopulation:longlongworldpopulationcinworldpopulationcoutEnterthepopulationoftheUS:longlongUSpopulationcinUSpopulationdoublepercentagepercentage=(double)USpopulationworldpopulation*coutThepopulationoftheUSispercentageoftheworldpopulationnreturn}ex汽车耗油量美国(mpg)or欧洲风格(LKm)#includeiostreamintmain(){usingnamespacestdcoutEnterthemilesofdistanceyouhavedriven:doublemdistancecinmdistancecoutEnterthegallonsofgasolineyouhaveused:doublemgasolinecinmgasolinecoutYourcarcanrunmdistancemgasolinemilespergallonncoutComputingbyEuropeanstyle:ncoutEnterthedistanceinkilometers:doublekdistancecinkdistancecoutEnterthepetrolinliters:doublekgasolinecinkgasolinecoutInEuropeanstyle:yourcanused*kgasolinekdistancelitersofpetrolperkilometersnreturn}exautomobilegasolineconsumption耗油量欧洲风格(LKm)转换成美国风格(mpg)#includeiostreamintmain(){usingnamespacestdcoutEntertheautomobilegasolineconsumptionfigureinnEuropeanstyle(litersperkilometers):doubleEurostylecinEurostylecoutConvertstoUSstyle(milespergallon):endlcoutEurostyleLKm=*Eurostylempgnreturn}Notethatkilometersismiles,andgallonislitersThus,mpgisaboutLKm,andmpgisaboutLKmEntertheautomobilegasolineconsumptionfigureinEuropeanstyle(litersperkilometers):ConvertstoUSstyle(milespergallon):LKm=mpgPressanykeytocontinueexautomobilegasolineconsumption耗油量美国风格(mpg)转换成欧洲风格(LKm)#includeiostreamintmain(){usingnamespacestdcoutEntertheautomobilegasolineconsumptionfigureinnUSstyle(milespergallon):doubleUSstylecinUSstylecoutConvertstoEuropeanstyle(milespergallon):endlcoutUSstylempg=*USstyleLKmnreturn}EntertheautomobilegasolineconsumptionfigureinUSstyle(milespergallon):ConvertstoEuropeanstyle(milespergallon):mpg=LKmPressanykeytocontinue第四章复合类型exdisplaytheinformationofstudent#includeiostreamconstintAsize=usingnamespacestdstructstudent定义结构描述{charfirstnameAsizecharlastnameAsizechargradeintage}voiddisplay(student)函数原型放在结构描述后intmain(){coutwhatisyourfirstnameendlstudentlcg创建结构变量(结构数据对象)cingetline(lcgfirstname,Asize)coutwhatisyourlastnameendlcingetline(lcglastname,Asize)coutwhatlettergradedoyoudeserveendlcinlcggradecoutwhatisyourageendlcinlcgagedisplay(lcg)return}voiddisplay(studentname){coutName:namefirstname,namelastnameendlcoutGrade:char(namegrade)endlcoutAge:nameageendl}exusethestringclassinsteadofchararray#includeiostream#includestringintmain(){usingnamespacestdstringname,dessertcoutEnteryourname:ngetline(cin,name)coutEnteryourfavoritedessert:ngetline(cin,dessert)coutIhavesomedeliciousdessertcoutforyou,namenreturn}有时候会遇到需要按下两次回车键才能正确的显示结果这是vc的一个BUG更改如下:elseif(Tr::eq((E)C,D)){Chg=trueIrdbuf()sbumpc()修改后的break}ex输入其名和姓并组合显示#includeiostream#includecstringconstintAsize=intmain(){usingnamespacestdcharfnameAsizecharlnameAsizecharfullname*AsizecoutEnteryourfirstname:输入名字存储在fname数组中cingetline(fname,Asize)coutEnteryourlastname:输入姓存储在lname数组中cingetline(lname,Asize)strncpy(fullname,lname,Asize)把姓lname复制到fullname空数组中strcat(fullname,,)把ldquordquo附加到上述fullname尾部strncat(fullname,fname,Asize)把fname名字附加到上述fullname尾部fullname*Asize=##为防止字符型数组溢出在数组结尾添加结束符coutHere#stheinformationinasinglestring:fullnameendl显示组合结果return}#defineCRTSECURENOWARNINGS#includeiostream#includecstringconstintAsize=intmain(){usingnamespacestdcharfirstnameAsizecharlastnamecoutEnteryourfirstname:cingetline(firstname,Asize)coutEnteryourlastname:cingetline(lastname,)strcat(lastname,,)strncat(lastname,firstname,Asize)coutHere#stheinformationinasinglestring:lastnameendlreturn}ex使用string对象存储、显示组合结果#includeiostream#includestringintmain(){usingnamespacestdstringfname,lname,attach,fullnamecoutEnteryourfirstname:getline(cin,fname)note:将一行输入读取到string类对象中使用的是getline(cin,str)它没有使用句点表示法所以不是类方法coutEnteryourlastname:getline(cin,lname)attach=,fullname=lnameattachfnamecoutHere#stheinformationinasinglestring:fullnameendlreturn}exdeclareastructandinitializeit声明结果并创建一个变量#includeiostreamconstintAsize=structCandyBar{charbrandAsizedoubleweightintcalory}intmain(){usingnamespacestdCandyBarsnack={MochaMunch,,}coutHere#stheinformationofsnack:ncoutbrand:snackbrandendlcoutweight:snackweightendlcoutcalory:snackcaloryendlreturn}ex结构数组的声明及初始化#includeiostreamconstintAsize=structCandyBar{charbrandAsizedoubleweightintcalory}intmain(){usingnamespacestdCandyBarsnack={{MochaMunch,,},{XuFuJi,,},{Alps,,}}for(inti=ii)利用for循环来显示snack变量的内容{coutsnackibrandendlsnackiweightendlsnackicaloryendlendl}return}expizza披萨饼#includeiostream#includestringconstintSize=structpizza声明结构{charcompanySizedoublediameterdoubleweight}intmain(){usingnamespacestdpizzapie创建一个名为pie的结构变量coutWhat#sthenameofpizzacompany:cingetline(piecompany,Size)coutWhat#sthediameterofpizza:cinpiediametercoutWhat#stheweightofpizza:cinpieweightcoutcompany:piecompanyendlcoutdiameter:piediameterinchesendlcoutweight:pieweightounchesendlreturn}expizzapie披萨饼使用new创建动态结构#includeiostream#includestringconstintSize=structpizza声明结构{charcompanySizedoublediameterdoubleweight}intmain(){usingnamespacestdpizza*pie=newpizza使用new创建动态结构coutWhat#sthediameterofpizza:cinpiediametercinget()读取下一个字符coutWhat#sthenameofpizzacompany:cinget(piecompany,Size)coutWhat#stheweightofpizza:cinpieweightcoutdiameter:piediameterinchesendlcoutcompany:piecompanyendlcoutweight:pieweightounchesendldeletepiedelete释放内存return}ex使用new动态分配数组mdash方法#includeiostream#includestringusingnamespacestdstructCandyBar{stringbranddoubleweightintcalory}intmain(){CandyBar*snack=newCandyBarsnackbrand=A单个初始化由new动态分配的内存snackweight=snackcalory=snackbrand=Bsnackweight=snackcalory=snackbrand=Csnackweight=snackcalory=for(inti=ii){coutbrand:snackibrandendlcoutweight:snackiweightendlcoutcalorie:snackicaloryendlendl}deletesnackreturn}ex数组mdash方法#includeiostreamintmain(){usingnamespacestdconstintSize=intsuccessSizecoutEnteryoursuccessofthethreetimesmetersrunning:ncinsuccesssuccesssuccesscoutsuccess:successendlcoutsuccess:successendlcoutsuccess:successendldoubleaverage=(successsuccesssuccess)coutaverage:averageendlreturn}exarraymdash方法#includeiostream#includearrayintmain(){usingnamespacestdarraydouble,ad={}coutEnteryoursuccessofthethreetimesmetersrunning:ncinadadadcoutsuccess:adendlcoutsuccess:adendlcoutsuccess:adendlad=(adadad)coutaverage:adendlreturn}第五章循环和关系表达式ex#includeiostreamintmain(){usingnamespacestdcoutPleaseentertwointegers:intnum,numcinnumnumintsum=for(inttemp=numtemp=numtemp)ortempsum=tempcoutThesumfromnumtonumissumendlreturn}ex#includeiostream#includearrayintmain(){usingnamespacestdarraylongdouble,ad={}ad=ad=Lfor(inti=ii)adi=i*adifor(inti=ii)couti!=adiendlreturn}#includeiostream#includearrayusingnamespacestdintmain(){arraylongdouble,multiplymultiply=multiply=LLfor(inti=i=i)multiplyi=multiplyi*icoutmultiplyreturn}ex#includeiostreamintmain(){usingnamespacestdcoutPleaseenteraninteger:intsum=,numwhile((cinnum)num!=){sum=numcoutSofar,thesumissumendlcoutPleaseenteraninteger:}return}ex#includeiostreamintmain(){usingnamespacestddoublesum,sumsum=sum=intyear=while(sum=sum){yearsum=sum=(sum)*sum}cout经过year年后Cleo的投资价值才能超过Daphne的投资价值。endlcout此时Cleo的投资价值为sum而Daphne的投资价值为sumendlreturn}#includeiostreamusingnamespacestdintmain(){doubleDaphne=doubleCleo=intyear=while(Cleo=Daphne){Daphne=Cleo*=year}coutyearendlreturn}ex#includeiostreamconstintMONTHS=constchar*monthsMONTHS={January,February,March,April,May,June,July,August,September,October,November,December}intmain(){usingnamespacestdintsalesMONTHS,sum=for(inti=iMONTHSi){cout请输入在monthsi的CForFools的销售量:cinsalesisum=salesi}cout这一年中的CForFools的总销售量为:sumendlreturn}ex#includeiostreamconstintMONTHS=constchar*monthsMONTHS={January,February,March,April,May,June,July,August,September,October,November,December}constchar*years={第一年,第二年,第三年}intmain(){usingnamespacestdintyearsale,sum=,salesMONTHSfor(inti=ii){inttemp=coutyearsi的每个月销售量:endlfor(intj=jMONTHSj){cout请输入monthsj的销售量:cinsalesijtemp=salesij}yearsalei=tempsum=yearsalei}for(inti=ii)coutyearsi的销售量为:yearsaleiendlcout这三年的总销售量为:sumendlreturn}#includeiostreamusingnamespacestdconstintYears=,Months=constchar*monthsMonths={January,February,March,April,May,June,July,August,September,October,November,December}intmain(){intsaleYearsMonths={}for(inti=iYearsi){intsum=for(intj=jMonthsj){coutEnterthesalesmentofmonthsj:cinsaleijsum=saleij}coutSalesmentforthisyear:sumendlendl}return}ex#includeiostream#includestringusingnamespacestdstructcar{stringnameintyear}intmain(){coutHowmanycarsdoyouwishtocatalogintnum(cinnum)get()car*ps=newcarnumfor(inti=inumi){coutCar#i:ncoutPleaseenterthemake:getline(cin,psiname)coutPleaseentertheyearmade:(cinpsiyear)get()}coutHereisyourcollection:nfor(inti=inumi)coutpsiyearpsinameendldeletepsreturn}#includeiostream#includestringusingnamespacestdstructcar{stringmakerintyear}intmain(){intnumbercoutHowmanycarsdoyouwishtocatalogcinnumbercar*a=newcarnumberfor(inti=inumberi){coutCar#i:endlcoutPleaseenterthemaker:cinget()getline(cin,aimaker)coutPleaseentertheyearmade:cinaiyear}coutHereisyourcollection:endlfor(inti=in

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/197

C++-primer-plus(第6版)中文版编程练习答案

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利