您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C++-primer-第五版课后习题答案(完整版)
Chapter1Exercise1.1Reviewthedocumentationforyourcompileranddeterminewhatfilenamingconventionituses.Compileandrunthemainprogramfrompage2.WindowsLinuxExercise1.2Exercise1.2:Changetheprogramtoreturn-1.Areturnvalueof-1isoftentreatedasanindicatorthattheprogramfailed.Recompileandrerunyourprogramtoseehowyoursystemtreatsafailureindicatorfrommain.WindowsLinux255?why?pleaselookatthisExercise1.3WriteaprogramtoprintHello,Worldonthestandardoutput.#includeiostreamintmain(){std::coutHello,Worldstd::endl;return0;}Exercise1.4Ourprogramusedtheadditionoperator,+,toaddtwonumbers.Writeaprogramthatusesthemultiplicationoperator,*,toprinttheproductinstead.#includeiostreamintmain(){std::coutEntertwonumbers:std::endl;intv1=0,v2=0;std::cinv1v2;std::coutTheproductofv1andv2isv1*v2std::endl;return0;}Exercise1.5Wewrotetheoutputinonelargestatement.Rewritetheprogramtouseaseparatestatementtoprinteachoperand.#includeiostreamintmain(){std::coutEntertwonumbers:std::endl;intv1=0,v2=0;std::cinv1v2;std::coutTheproductof;std::coutv1;std::coutand;std::coutv2;std::coutis;std::coutv1*v2;std::coutstd::endl;return0;}Exercise1.6Explainwhetherthefollowingprogramfragmentislegal.It'sillegal.[Error]expectedprimary-expressionbefore''tokenFixedit:removethesparesemicolons.std::coutThesumofv1andv2isv1+v2std::endl;Exercise1.7Compileaprogramthathasincorrectlynestedcomments.Example:/**commentpairs/**/cannotnest.*''cannotnest''isconsideredsourcecode,*asistherestoftheprogram*/intmain(){return0;}Compiledresult(g++):Exercise1.8Indicatewhich,ifany,ofthefollowingoutputstatementsarelegal:std::cout/*;std::cout*/;std::cout/**/*/;std::cout/**//*/**/;Afteryou’vepredictedwhatwillhappen,testyouranswersbycompilingaprogramwitheachofthesestatements.Correctanyerrorsyouencounter.Compiledresult(g++):Corrected?justaddedaquote:std::cout/*;std::cout*/;std::cout/**/*/;std::cout/**//*/**/;Output:/**/*//*Exercise1.9Exercise1.10Exercise1.11Exercise1.12Whatdoesthefollowingforloopdo?Whatisthefinalvalueofsum?intsum=0;for(inti=-100;i=100;++i)sum+=i;theloopsumsthenumbersfrom-100to100.thefinalvalueofsumiszero.Exercise1.13Rewritetheexercisesfrom§1.4.1(p.13)usingforloops.Ex1.9:#includeiostreamintmain(){intsum=0;for(inti=50;i=100;++i)sum+=i;std::coutthesumis:sumstd::endl;return0;}Ex1.10:#includeiostreamintmain(){for(inti=10;i=0;--i)std::coutistd::endl;return0;}Ex1.11:#includeiostreamintmain(){intval_small=0,val_big=0;std::coutpleaseinputtwointegers:;std::cinval_smallval_big;if(val_smallval_big){inttmp=val_small;val_small=val_big;val_big=tmp;}for(inti=val_small;i=val_big;++i)std::coutistd::endl;return0;}Exercise1.14Compareandcontrasttheloopsthatusedaforwiththoseusingawhile.Arethereadvantagesordisadvantagestousingeitherform?Ifyouneedapatternwhichisusingavariableinaconditionandincrementingthatvariableinthebody.Youshoulduseforloop.Elsethewhileloopismoresimple.Wanttoknowmore?lookatthisExercise1.15Writeprogramsthatcontainthecommonerrorsdiscussedintheboxonpage16.Familiarizeyourselfwiththemessagesthecompilergenerates.JUSTREADIT!Exercise1.16Writeyourownversionofaprogramthatprintsthesumofasetofintegersreadfromcin.Manypeopleconfusedaboutthisexercise,suchasthisandthis.Inmyopinion,theexerciseaimtowritetheprogramwithoutEND-OF-FILE.BUT,thecodeinfirstlinkisnotcorrect.Thefollowingaremyownversion:#includeiostreamintmain(){intlimit=0,sum=0,value=0;std::coutHowmanyintegerswouldyouliketoenter?;std::cinlimit;//assumewedon'tknowwhatisEOF(End-Of-File).while(std::cinvalue&&(--limit!=0))sum+=value;std::coutsum+valuestd::endl;return0;}Watchoutforsum+valueinthecoutline.Exercise1.17Whathappensintheprogrampresentedinthissectioniftheinputvaluesareallequal?Whatiftherearenoduplicatedvalues?Iftheinputvaluesareallequal,itwillprintalinewhichshowsthecountofthenumberyouinput.Iftherearenoduplicatedvalues,whendifferentvaluesinput,anewlinewillbeprintedifyouclickEnter.Exercise1.18Compileandruntheprogramfromthissectiongivingitonlyequalvaluesasinput.Runitagaingivingitvaluesinwhichnonumberisrepeated.Exercise1.19Revisetheprogramyouwrotefortheexercisesin§1.4.1(p.13)thatprintedarangeofnumberssothatithandlesinputinwhichthefirstnumberissmallerthanthesecond.Yes,weshoulduseiftojudgewhichisbigger.reviewthiscodeExercise1.20=c++11option.(Neversayitagain.)Howtotestit?usethebook.txtindatafolder.Anddoitlikethis:Exercise1.21WriteaprogramthatreadstwoSales_itemobjectsthathavethesameISBNandproducestheirsum.TheprogramshouldcheckwhethertheobjectshavethesameISBN.(Havealookat1.5.2,surprise!)CodeExercise1.22WriteaprogramthatreadsseveraltransactionsforthesameISBN.Writethesumofallthetransactionsthatwereread.Tips:thisprogra
本文标题:C++-primer-第五版课后习题答案(完整版)
链接地址:https://www.777doc.com/doc-6184920 .html