cprimerplus(第六版)课后编程练习答案资料

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

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

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

资源描述

C++//ex2.1--displayyournameandaddress#includeiostreamintmain(void){usingnamespacestd;coutMynameisliaochunguangandIliveinhunanchenzhou.\n}//ex2.2--convertthefurlongunitstoyarduints-#includeiostreamdoublefur2yd(double);intmain(){usingnamespacestd;coutenterthedistancemeasuredbyfurlongunits:;doublefur;cinfur;coutconvertthefurlongtoyardendl;doubleyd;yd=fur2yd(fur);coutfurfurlongisydyardendl;return0;}doublefur2yd(doublet){return220*t;}//ex2.3-#includeiostreamvoidmice();voidsee();usingnamespacestd;intmain(){mice();mice();see();see();return0;}voidmice(){coutthreeblindmiceendl;}voidsee(){coutseehowtheyrunendl;}//ex2.4#includeiostreamintmain(){usingnamespacestd;coutEnteryourage:;intage;cinage;intmonth;month=age*12;coutageyearsismonthmonthsendl;return0;}//ex2.5---converttheCelsiusvalvetoFahrenheitvalue#includeiostreamdoubleC2F(double);intmain(){usingnamespacestd;coutpleaseenteraCelsiusvalue:;doubleC;cinC;doubleF;F=C2F(C);coutCdegreesCelsiusisFdegreesFahrenheit.endl;return0;}doubleC2F(doublet){return1.8*t+32;}//ex2.6---convertthelightyearsvalvetoastronomicalunits--#includeiostreamdoubleconvert(double);//intmain(){usingnamespacestd;coutEnterthenumberoflightyears:;doublelight_years;cinlight_years;doubleastro_units;astro_units=convert(light_years);coutlight_yearslight_years=astro_unitsastronomicalunits.endl;return0;}doubleconvert(doublet){return63240*t;//1=63240}//ex2.7--#includeiostreamvoidshow();main(){usingnamespacestd;show();return0;}voidshow(){usingnamespacestd;inth,m;coutenterthenumberofhours:;cinh;coutenterthenumberofminutes:;cinm;coutTime:h:mendl;}//ex3.1(feet)(inch)#includeiostreamconstintinch_per_feet=12;//const--1feet=12inches--1=12intmain(){usingnamespacestd;coutpleaseenteryourheightininches:___\b\b\b;//\bintht_inch;cinht_inch;intht_feet=ht_inch/inch_per_feet;//intrm_inch=ht_inch%inch_per_feet;//coutyourheightisht_feetfeet,andrm_inchinches\n;return0;}//ex3.2--bodymassindex#includeiostreamconstintinch_per_feet=12;constdoublemeter_per_inch=0.0254;constdoublepound_per_kilogram=2.2;intmain(){usingnamespacestd;coutPleaseenteryourheight:endl;coutFirst,enteryourheightoffeetpart:_\b;intht_feet;cinht_feet;coutSecond,enteryourheightofinchpart:_\b;intht_inch;cinht_inch;coutNow,pleaseenteryourweightinpound:___\b\b\b;doublewt_pound;cinwt_pound;intinch;inch=ht_feet*inch_per_feet+ht_inch;doubleht_meter;ht_meter=inch*meter_per_inch;doublewt_kilogram;wt_kilogram=wt_pound/pound_per_kilogram;coutendl;coutYourpensonalbodyinformationasfollows:endl;cout:inch(inch)\n:ht_meter(meter)\n:wt_kilogram(kilogram)\n;doubleBMI;BMI=wt_kilogram/(ht_meter*ht_meter);coutyourBodyMassIndex()isBMIendl;return0;}//ex3.3#includeiostreamconstintminutes_per_degree=60;constintseconds_per_minute=60;intmain(){usingnamespacestd;coutEnteralatitudeindegrees,minutes,andseconds:\n;coutFirst,enterthedegrees:;intdegree;cindegree;coutNext,entertheminutesofarc:;intminute;cinminute;coutFianlly,enterthesecondsofarc:;intsecond;cinsecond;doubleshow_in_degree;show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/minutes_per_degree/seconds_per_minute;coutdegreedegrees,minuteminutes,secondseconds=show_in_degreedegrees\n;return0;}//ex3.4#includeiostreamconstinthours_per_day=24;constintminutes_per_hour=60;constintseconds_per_minute=60;intmain(){usingnamespacestd;coutEnterthenumberofseconds:;longseconds;cinseconds;intDay,Hour,Minute,Second;Day=seconds/seconds_per_minute/minutes_per_hour/hours_per_day;Hour=seconds/seconds_per_minute/minutes_per_hour%hours_per_day;Minute=seconds/seconds_per_minute%minutes_per_hour;Second=seconds%seconds_per_minute;coutsecondsseconds=Daydays,Hourhours,Minuteminutes,Secondseconds\n;return0;}//ex3.5#includeiostreamintmain(){usingnamespacestd;coutEntertheworldpopulation:;longlongworld_population;cinworld_population;coutEnterthepopulationoftheUS:;longlongUS_population;cinUS_population;doublepercentage;percentage=(double)US_population/world_population*100;coutThepopulationoftheUSispercentage%oftheworldpopulation.\n;return0;}//ex3.6-(mpg)or(L/100Km)#includeiostreamintmain(){usingnamespacestd;coutEnterthemilesofdistanceyouhavedriven:;doublem_distance;cinm_distance;coutEnterthegallonsofgasolineyouhaveused:;doublem_gasoline;cinm_gasoline;coutYourcarcanrunm_distance/m_gasolinemilespergallon\n;coutComputingbyEuropeanstyle:\n;coutEnterthedistanceinkilometers:;doublek_distance;cink_distance;coutEnterthepetrolinliters:;doublek_gasoline;cink_gasoline;coutInEuropeanstyle:yourcanused100*k_gasoline/k_distancelitersofpetrolper100kilometers\n;return0;}//ex3.7automobilegasolineconsumption---(L/100Km)(mpg)#includeiostreamintmain(){usingnamespacestd;coutEntertheautomobilegasolineconsumptionfigurein\nEuropeanstyle(litersper100kilometers):;doubleEuro_style;cinEuro_sty

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

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

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

×
保存成功