您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > C程序设计语言(第二版)课后答案
TheCProgrammingLanguage,2ndedition,KernighanandRitchie《c程序设计语言》英文的配套答案,所列页码均为英文版的。1.011.021.031.041.051.061.071.081.091.101.111.121.131.141.151.161.171.181.191.201.211.221.231.242.012.022.032.042.052.062.072.082.092.103.013.023.033.043.053.064.014.024.034.044.054.064.074.084.124.134.145.015.025.035.045.055.065.075.085.095.105.115.135.146.016.036.046.057.017.027.037.067.087.098.018.038.048.06AnswertoExercise1-1Runthehello,worldprogramonyoursystem.Experimentwithleavingoutpartsoftheprogram,toseewhaterrormessagesyouget.Murphy'sLawdictatesthatthereisnosinglecorrectanswertotheveryfirstexerciseinthebook.Ohwell.Here'sahelloworldprogram:#includestdio.hintmain(void){printf(hello,world\n);return0;}Asyoucansee,I'veaddedareturnstatement,becausemainalwaysreturnsint,andit'sgoodstyletoshowthisexplicitly.AnswertoExercise1-2Experimenttofindoutwhathappenswhenprintf'sargumentstringcontains\c,wherecissomecharacternotlistedabove.By'above',thequestionisreferringto:\n(newline)\t(tab)\b(backspace)\(doublequote)\\(backslash)Wehavetotreadcarefullyhere,becauseusinganon-specifiedescapesequenceinvokesundefinedbehaviour.Thefollowingprogramattemptstodemonstrateallthelegalescapesequences,notincludingtheonesalreadyshown(except\n,whichIactuallyneedintheprogram),andnotincludinghexadecimalandoctalescapesequences.#includestdio.hintmain(void){printf(Audibleorvisualalert.\a\n);printf(Formfeed.\f\n);printf(Thisescape,\r,movestheactivepositiontotheinitialpositionofthecurrentline.\n);printf(Verticaltab\vistricky,asitsbehaviourisunspecifiedundercertainconditions.\n);return0;}AnswertoExercise1-3Modifythetemperatureconversionprogramtoprintaheadingabovethetable.#includestdio.hintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf(FC\n\n);fahr=lower;while(fahr=upper){celsius=(5.0/9.0)*(fahr-32.0);printf(%3.0f%6.1f\n,fahr,celsius);fahr=fahr+step;}return0;}AnswertoExercise1-4WriteaprogramtoprintthecorrespondingCelsiustoFahrenheittable.#includestdio.hintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf(CF\n\n);celsius=lower;while(celsius=upper){fahr=(9.0/5.0)*celsius+32.0;printf(%3.0f%6.1f\n,celsius,fahr);celsius=celsius+step;}return0;}AnswertoExercise1-5Modifythetemperatureconversionprogramtoprintthetableinreverseorder,thatis,from300degreesto0.Thisversionusesawhileloop:#includestdio.hintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf(CF\n\n);celsius=upper;while(celsius=lower){fahr=(9.0/5.0)*celsius+32.0;printf(%3.0f%6.1f\n,celsius,fahr);celsius=celsius-step;}return0;}Thisversionusesaforloop:#includestdio.hintmain(void){floatfahr,celsius;intlower,upper,step;lower=0;upper=300;step=20;printf(CF\n\n);for(celsius=upper;celsius=lower;celsius=celsius-step){fahr=(9.0/5.0)*celsius+32.0;printf(%3.0f%6.1f\n,celsius,fahr);}return0;}ChrisSidinotesthatSection1.3HasashortForstatementexample,andBasedonthatexample,Ithinkthesolutionto1.5:a)shoulddofahrtocelsiusconversion(whereasthesolutionsonyourpagedocelsiustofahr)b)shouldbesimilartotheexampleandassmall.Heoffersthissolution:#includestdio.h/*printFahrenheit-Celsiustable*/intmain(){intfahr;for(fahr=300;fahr=0;fahr=fahr-20)printf(%3d%6.1f\n,fahr,(5.0/9.0)*(fahr-32));return0;}AnswertoExercise1-6Verifythattheexpressiongetchar()!=EOFis0or1./*Thisprogrampromptsforinput,andthencapturesacharacter*fromthekeyboard.IfEOFissignalled(typicallythrougha*control-Dorcontrol-Zcharacter,thoughnotnecessarily),*theprogramprints0.Otherwise,itprints1.**Ifyourinputstreamisbuffered(anditprobablyis),then*youwillneedtopresstheENTERkeybeforetheprogramwill*respond.*/#includestdio.hintmain(void){printf(Pressakey.ENTERwouldbenice:-)\n\n);printf(Theexpressiongetchar()!=EOFevaluatesto%d\n,getchar()!=EOF);return0;}AnswertoExercise1-7WriteaprogramtoprintthevalueofEOF.#includestdio.hintmain(void){printf(ThevalueofEOFis%d\n\n,EOF);return0;}Exercise1-8Writeaprogramtocountblanks,tabs,andnewlines.#includestdio.hintmain(void){intblanks,tabs,newlines;intc;intdone=0;intlastchar=0;blanks=0;tabs=0;newlines=0;while(done==0){c=getchar();if(c=='')++blanks;if(c=='\t')++tabs;if(c=='\n')++newlines;if(c==EOF){if(lastchar!='\n'){++newlines;/*thisisabitofasemanticstretch,butitcopes*withimplementationswhereatextfilemightnot*endwithanewline.ThankstoJimStadforpointing*thisout.*/}done=1;}lastchar=c;}printf(Blanks:%d\nTabs:%d\nLines:%d\n,blanks,tabs,newlines);return0;}Exercise1-9Writeaprogramtocopyitsinputtoitsoutput,replacingeachstringofoneormoreblanksbyasingleblank.#includestdio.hintmain(void){intc;intinspace;inspace=0;while((c=getchar())!=EOF){if(c==''){if(inspace==0){inspace=1;putchar(c);}}/*Wehaven'tmet'else'yet,sowehavetobealittleclumsy*/if(c!=''){inspace=0;putchar(c);}}return0;}ChrisSidiwrites:insteadofhavinganinspaceboolean,youcankeeptrackofthepreviouscharacterandseeifboththecurrentcharacterandpreviouscharacterarespaces:#includestdio.h/*countlinesininput*/intmain(){intc,pc;/*c=character,pc=previouscharacter*//*setpctoavaluethatwouldn'tmatchanycharacte
本文标题:C程序设计语言(第二版)课后答案
链接地址:https://www.777doc.com/doc-5236365 .html