c++-primer-plus(第6版)中文版-编程练习答案(word版).docx
文本预览下载声明
第二章:开始学习C++//ex2.1--display your name andaddress#includeiostreamintmain(void){using namespacestd;coutMy name is liao chunguang and I live in hunanchenzhou.\n”;}//ex2.2--convertthefurlongunitstoyarduints-把浪单位换位码单位#includeiostreamdoublefur2yd(double);intmain(){using namespacestd;coutenter the distance measured by furlongunits:;doublefur;cinfur;coutconvert the furlong toyardendl;doubleyd;yd=fur2yd(fur);coutfur furlong is ydyardendl;return0;}double fur2yd(doublet){return220*t;}//ex2.3-每个函数都被调用两次#includeiostreamvoidmice();voidsee();using namespacestd;intmain(){mice();mice();see();see();return0;}voidmice(){coutthree blindmiceendl;}voidsee(){coutsee how theyrunendl;}//ex2.4#includeiostreamint main(){usingnamespacestd;coutEnter yourage:;intage;cinage;int month;month=age*12;coutage years is monthmonthsendl;return0;}//ex2.5---converttheCelsiusvalvetoFahrenheitvalue#includeiostreamdoubleC2F(double);intmain(){using namespacestd;coutplease enter a Celsiusvalue:;doubleC;cinC;doubleF;F=C2F(C);coutCdegreesCelsiusisFdegreesFahrenheit.endl;return0;}double C2F(doublet){return1.8*t+32;}//ex2.6---convertthelightyearsvalvetoastronomicalunits--把光年转换为天文单位#includeiostreamdoubleconvert(double);//函数原型intmain(){using namespacestd;coutEnter the number of lightyears:;doublelight_years;cinlight_years;doubleastro_units;astro_units=convert(light_years);coutlight_yearslight_years=astro_unitsastronomicalunits.endl;return0;}doubleconvert(doublet){return 63240*t;//1 光年=63240 天文单位}//ex2.7--显示用户输入的小时数和分钟数#includeiostreamvoidshow();main(){using namespacestd;show();return0;}voidshow(){using namespacestd;inth,m;coutenter the number ofhours:;cinh;coutenter the number ofminutes:;cinm;coutTime:h:mendl;}第三章:处理数据//ex3.1—将身高用英尺(feet)和英寸(inch)表示#includeiostreamconst intinch_per_feet=12;//const 常量--1feet=12inches--1 英尺=12英寸intmain(){using namespacestd;coutpleaseenteryourheightininches:\b\b\b;//\b表示为退格字符intht_inch;cinht_inch;intht_feet=ht_inch/inch_per_feet;//取商intrm_inch=ht_inch%inch_per_feet;//取余coutyourheightish
显示全部