完整word版c++primerplus第六版课后编程练习答案0807172120文档格式.docx
《完整word版c++primerplus第六版课后编程练习答案0807172120文档格式.docx》由会员分享,可在线阅读,更多相关《完整word版c++primerplus第六版课后编程练习答案0807172120文档格式.docx(21页珍藏版)》请在冰豆网上搜索。
fur;
convertthefurlongtoyard"
endl;
doubleyd;
yd=fur2yd(fur);
fur<
furlongis"
yd<
yard"
return0;
}
doublefur2yd(doublet)
return220*t;
〃ex2.3-每个函数都被调用两次
voidmice();
voidsee();
mice();
see();
return0;
voidmice()
{cout<
threeblindmice"
endl;
voidsee()
seehowtheyrun"
//ex2.4
#include<
intmain()
usingnamespacestd;
Enteryourage:
intage;
cin>
>
age;
intmonth;
month=age*12;
age<
yearsis"
month<
months"
//ex2.5---converttheCelsiusvalvetoFahrenheitvalue#include<
doubleC2F(double);
pleaseenteraCelsiusvalue:
doubleC;
C;
doubleF;
F=C2F(C);
C<
degreesCelsiusis"
F<
degreesFahrenheit."
doubleC2F(doublet)
return1.8*t+32;
〃ex2.6---convertthelightyearsvalvetoastronomicalunits--把光年转换为天文单位#include<
doubleconvert(double);
//函数原型
Enterthenumberoflightyears:
doublelight_years;
>
light_years;
doubleastro_units;
astro_units=convert(light_years);
light_years<
light_years="
astro_units<
astronomicalunits."
doubleconvert(doublet)
return63240*t;
//1光年=63240天文单位
//ex2.7--显示用户输入的小时数和分钟数
voidshow();
main()
show();
voidshow()
inth,m;
enterthenumberofhours:
h;
enterthenumberofminutes:
m;
Time:
h<
:
m<
第三章:
处理数据
//ex3.1—将身高用英尺(feet)和英寸(inch)表示
constintinch_per_feet=12;
//const常量--1feet=12inches--1英尺=12英寸
pleaseenteryourheightininches:
___\b\b\b"
//\b表示为退格字符
intht_inch;
ht_inch;
intht_feet=ht_inch/inch_per_feet;
//取商
intrm_inch=ht_inch%inch_per_feet;
//取余cout<
yourheightis"
ht_feet<
feet,and"
rm_inch<
inches\n"
//ex3.2--计算相应的bodymassindex(体重指数)#include<
constdoublemeter_per_inch=0.0254;
constdoublepound_per_kilogram=2.2;
Pleaseenteryourheight:
First,enteryourheightoffeetpart(输入你身高的英尺部分):
_\b"
intht_feet;
ht_feet;
Second,enteryourheightofinchpart(输入你身高的英寸部分):
intht_inch;
Now,pleaseenteryourweightinpound:
doublewt_pound;
wt_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;
Yourpensonalbodyinformationasfollows:
身高:
inch<
(英尺inch)\n"
身高:
ht_meter<
(米meter)\n"
<
体重:
wt_kilogram<
(千克kilogram)\n"
doubleBMI;
BMI=wt_kilogram/(ht_meter*ht_meter);
yourBodyMassIndex(体重指数)is"
BMI<
//ex3.3以度,分,秒输入,以度输出
constintminutes_per_degree=60;
constintseconds_per_minute=60;
Enteralatitudeindegrees,minutes,andseconds:
\n"
First,enterthedegrees:
intdegree;
degree;
Next,entertheminutesofarc:
intminute;
minute;
Fianlly,enterthesecondsofarc:
intsecond;
second;
doubleshow_in_degree;
show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/minutes_per_degree/seconds_per_minute;
degree<
degrees,"
minute<
minutes,"
second<
seconds="
show_in_degree<
degrees\n"
//ex3.4
constinthours_per_day=24;
constintminutes_per_hour=60;
cout<
Enterthenumberofseconds:
longseconds;
cin>
seconds;
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;
seconds<
seconds="
Day<
days,"
Hour<
hours,"
Minute<
Second<
seconds\n"
//ex3.5
Entertheworldpopulation: