您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > C++--primer-plus-(第6版)-中文版编程练习答案
第二章:开始学习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英尺=12英寸intmain(){usingnamespacestd;coutpleaseenteryourheightininches:___\b\b\b;//\b表示为退格字符intht_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/
本文标题:C++--primer-plus-(第6版)-中文版编程练习答案
链接地址:https://www.777doc.com/doc-4407696 .html