19C语言程序设计现代方法(第二版)习题答案

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Chapter2AnswerstoSelectedExercises2.[was#2](a)Theprogramcontainsonedirective(#include)andfourstatements(threecallsofprintfandonereturn).(b)Parkinson'sLaw:Workexpandssoastofillthetimeavailableforitscompletion.3.[was#4]#includestdio.hintmain(void){intheight=8,length=12,width=10,volume;volume=height*length*width;printf(Dimensions:%dx%dx%d\n,length,width,height);printf(Volume(cubicinches):%d\n,volume);printf(Dimensionalweight(pounds):%d\n,(volume+165)/166);return0;}4.[was#6]Here'sonepossibleprogram:#includestdio.hintmain(void){inti,j,k;floatx,y,z;printf(Valueofi:%d\n,i);printf(Valueofj:%d\n,j);printf(Valueofk:%d\n,k);printf(Valueofx:%g\n,x);printf(Valueofy:%g\n,y);printf(Valueofz:%g\n,z);return0;}WhencompiledusingGCCandthenexecuted,thisprogramproducedthefollowingoutput:Valueofi:5618848Valueofj:0Valueofk:6844404Valueofx:3.98979e-34Valueofy:9.59105e-39Valueofz:9.59105e-39Thevaluesprinteddependonmanyfactors,sothechancethatyou'llgetexactlythesenumbersissmall.5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.8.[was#12]Thereare14tokens:a,=,(,3,*,q,-,p,*,p,),/,3,and;.AnswerstoSelectedProgrammingProjects4.[was#8;modified]#includestdio.hintmain(void){floatoriginal_amount,amount_with_tax;printf(Enteranamount:);scanf(%f,&original_amount);amount_with_tax=original_amount*1.05f;printf(Withtaxadded:$%.2f\n,amount_with_tax);return0;}Theamount_with_taxvariableisunnecessary.Ifweremoveit,theprogramisslightlyshorter:#includestdio.hintmain(void){floatoriginal_amount;printf(Enteranamount:);scanf(%f,&original_amount);printf(Withtaxadded:$%.2f\n,original_amount*1.05f);return0;}Chapter3AnswerstoSelectedExercises2.[was#2](a)printf(%-8.1e,x);(b)printf(%10.6e,x);(c)printf(%-8.3f,x);(d)printf(%6.0f,x);5.[was#8]Thevaluesofx,i,andywillbe12.3,45,and.6,respectively.AnswerstoSelectedProgrammingProjects1.[was#4;modified]#includestdio.hintmain(void){intmonth,day,year;printf(Enteradate(mm/dd/yyyy):);scanf(%d/%d/%d,&month,&day,&year);printf(Youenteredthedate%d%.2d%.2d\n,year,month,day);return0;}3.[was#6;modified]#includestdio.hintmain(void){intprefix,group,publisher,item,check_digit;printf(EnterISBN:);scanf(%d-%d-%d-%d-%d,&prefix,&group,&publisher,&item,&check_digit);printf(GS1prefix:%d\n,prefix);printf(Groupidentifier:%d\n,group);printf(Publishercode:%d\n,publisher);printf(Itemnumber:%d\n,item);printf(Checkdigit:%d\n,check_digit);/*Thefiveprintfcallscanbecombinedasfollows:printf(GS1prefix:%d\nGroupidentifier:%d\nPublishercode:%d\nItemnumber:%d\nCheckdigit:%d\n,prefix,group,publisher,item,check_digit);*/return0;}Chapter4AnswerstoSelectedExercises2.[was#2]NotinC89.Supposethatiis9andjis7.Thevalueof(-i)/jcouldbeeither–1or–2,dependingontheimplementation.Ontheotherhand,thevalueof-(i/j)isalways–1,regardlessoftheimplementation.InC99,ontheotherhand,thevalueof(-i)/jmustbeequaltothevalueof-(i/j).9.[was#6](a)638(b)321(c)2-13(d)00013.[was#8]Theexpression++iisequivalentto(i+=1).Thevalueofbothexpressionsisiaftertheincrementhasbeenperformed.AnswerstoSelectedProgrammingProjects2.[was#4]#includestdio.hintmain(void){intn;printf(Enterathree-digitnumber:);scanf(%d,&n);printf(Thereversalis:%d%d%d\n,n%10,(n/10)%10,n/100);return0;}Chapter5AnswerstoSelectedExercises2.[was#2](a)1(b)1(c)1(d)14.[was#4](ij)-(ij)6.[was#12]Yes,thestatementislegal.Whennisequalto5,itdoesnothing,since5isnotequalto–9.10.[was#16]Theoutputisonetwosincetherearenobreakstatementsafterthecases.AnswerstoSelectedProgrammingProjects2.[was#6]#includestdio.hintmain(void){inthours,minutes;printf(Entera24-hourtime:);scanf(%d:%d,&hours,&minutes);printf(Equivalent12-hourtime:);if(hours==0)printf(12:%.2dAM\n,minutes);elseif(hours12)printf(%d:%.2dAM\n,hours,minutes);elseif(hours==12)printf(%d:%.2dPM\n,hours,minutes);elseprintf(%d:%.2dPM\n,hours-12,minutes);return0;}4.[was#8;modified]#includestdio.hintmain(void){intspeed;printf(Enterawindspeedinknots:);scanf(%d,&speed);if(speed1)printf(Calm\n);elseif(speed=3)printf(Lightair\n);elseif(speed=27)printf(Breeze\n);elseif(speed=47)printf(Gale\n);elseif(speed=63)printf(Storm\n);elseprintf(Hurricane\n);return0;}6.[was#10]#includestdio.hintmain(void){intcheck_digit,d,i1,i2,i3,i4,i5,j1,j2,j3,j4,j5,first_sum,second_sum,total;printf(Enterthefirst(single)digit:);scanf(%1d,&d);printf(Enterfirstgroupoffivedigits:);scanf(%1d%1d%1d%1d%1d,&i1,&i2,&i3,&i4,&i5);printf(Entersecondgroupoffivedigits:);scanf(%1d%1d%1d%1d%1d,&j1,&j2,&j3,&j4,&j5);printf(Enterthelast(single)digit:);scanf(%1d,&check_digit);first_sum=d+i2+i4+j1+j3+j5;second_sum=i1+i3+i5+j2+j4;total=3*first_sum+second_sum;if(check_digit==9-((total-1)%10))printf(VALID\n);elseprintf(NOTVALID\n);return0;}10.[was#14]#includestdio.hintmain(void){intgrade;printf(Enternumericalgrade:);scanf(%d,&grade);if(grade0||grade100){printf(Illegalgrade\n);return0;}switch(grade/10){case10:case9:printf(Lettergrade:A\n);break;case8:printf(Lettergrade:B\n);break;case7:printf(Lettergrade:C\n);break;case6:printf(Lettergrade:D\n);break;case5:case4:case3:case2:case1:case0:printf(Lettergrade:F\n);break;}return0;}Cha

1 / 84
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功