最新c++primerplus第六版课后编程练习答案资料.docx
《最新c++primerplus第六版课后编程练习答案资料.docx》由会员分享,可在线阅读,更多相关《最新c++primerplus第六版课后编程练习答案资料.docx(30页珍藏版)》请在冰豆网上搜索。
最新c++primerplus第六版课后编程练习答案资料
第二章:
开始学习C++
//ex2.1--displayyournameandaddress
#include
intmain(void)
{
usingnamespacestd;
cout<<"MynameisliaochunguangandIliveinhunanchenzhou.\n”;
}
//ex2.2--convertthefurlongunitstoyarduints-把浪单位换位码单位
#include
doublefur2yd(double);
intmain()
{
usingnamespacestd;
cout<<"enterthedistancemeasuredbyfurlongunits:
";
doublefur;
cin>>fur;
cout<<"convertthefurlongtoyard"<doubleyd;
yd=fur2yd(fur);
cout<return0;
}
doublefur2yd(doublet)
{
return220*t;
}
//ex2.3-每个函数都被调用两次
#include
voidmice();
voidsee();
usingnamespacestd;
intmain()
{
mice();
mice();
see();
see();
return0;
}
voidmice()
{
cout<<"threeblindmice"<}
voidsee()
{
cout<<"seehowtheyrun"<}
//ex2.4
#include
intmain()
{
usingnamespacestd;
cout<<"Enteryourage:
";
intage;
cin>>age;
intmonth;
month=age*12;
cout<return0;
}
//ex2.5---converttheCelsiusvalvetoFahrenheitvalue
#include
doubleC2F(double);
intmain()
{
usingnamespacestd;
cout<<"pleaseenteraCelsiusvalue:
";
doubleC;
cin>>C;
doubleF;
F=C2F(C);
cout<return0;
}
doubleC2F(doublet)
{
return1.8*t+32;
}
//ex2.6---convertthelightyearsvalvetoastronomicalunits--把光年转换为天文单位
#include
doubleconvert(double);//函数原型
intmain()
{
usingnamespacestd;
cout<<"Enterthenumberoflightyears:
";
doublelight_years;
cin>>light_years;
doubleastro_units;
astro_units=convert(light_years);
cout<return0;
}
doubleconvert(doublet)
{
return63240*t;//1光年=63240天文单位
}
//ex2.7--显示用户输入的小时数和分钟数
#include
voidshow();
main()
{
usingnamespacestd;
show();
return0;
}
voidshow()
{
usingnamespacestd;
inth,m;
cout<<"enterthenumberofhours:
";
cin>>h;
cout<<"enterthenumberofminutes:
";
cin>>m;
cout<<"Time:
"<"<}
第三章:
处理数据
//ex3.1—将身高用英尺(feet)和英寸(inch)表示
#include
constintinch_per_feet=12;//const常量--1feet=12inches--1英尺=12英寸
intmain()
{
usingnamespacestd;
cout<<"pleaseenteryourheightininches:
___\b\b\b";//\b表示为退格字符
intht_inch;
cin>>ht_inch;
intht_feet=ht_inch/inch_per_feet;//取商
intrm_inch=ht_inch%inch_per_feet;//取余
cout<<"yourheightis"<<return0;
}
//ex3.2--计算相应的bodymassindex(体重指数)
#include
constintinch_per_feet=12;
constdoublemeter_per_inch=0.0254;
constdoublepound_per_kilogram=2.2;
intmain()
{
usingnamespacestd;
cout<<"Pleaseenteryourheight:
"<cout<<"First,enteryourheightoffeetpart(输入你身高的英尺部分):
_\b";
intht_feet;
cin>>ht_feet;
cout<<"Second,enteryourheightofinchpart(输入你身高的英寸部分):
_\b";
intht_inch;
cin>>ht_inch;
cout<<"Now,pleaseenteryourweightinpound:
___\b\b\b";
doublewt_pound;
cin>>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;
cout<cout<<"Yourpensonalbodyinformationasfollows:
"<cout<<"身高:
"<"<<<"体重:
"<doubleBMI;
BMI=wt_kilogram/(ht_meter*ht_meter);
cout<<"yourBodyMassIndex(体重指数)is"<return0;
}
//ex3.3以度,分,秒输入,以度输出
#include
constintminutes_per_degree=60;
constintseconds_per_minute=60;
intmain()
{
usingnamespacestd;
cout<<"Enteralatitudeindegrees,minutes,andseconds:
\n";
cout<<"First,enterthedegrees:
";
intdegree;
cin>>degree;
cout<<"Next,entertheminutesofarc:
";
intminute;
cin>>minute;
cout<<"Fianlly,enterthesecondsofarc:
";
intsecond;
cin>>second;
doubleshow_in_degree;
show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/minutes_per_degree/seconds_per_minute;
cout<return0;
}
//ex3.4
#include
constinthours_per_day=24;
constintminutes_per_hour=60;
constintseconds_per_minute=60;
intmain()
{
usingnamespacestd;
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;
cout<return0;
}
//ex3.5
#include
intmain()
{
usingnamespacestd;
cout<<"Entertheworldpopulation:
";
longlongworld_population;
cin>>world_population;
cout<<"EnterthepopulationoftheUS:
";
longlongUS_population;
cin>>US_population;
doublepercentage;
percentage=(double)US_population/world_population*100;
cout<<"ThepopulationoftheUSis"<return0;
}
//ex3.6 汽车耗油量-美国(mpg)or欧洲风格(L/100Km)
#include
intmain()
{
usingnamespacestd;
cout<<"Enterthemilesofdistanceyouhavedriven:
";
doublem_distance;
cin>>m_distance;
cout<<"Enterthegallonsofgasolineyouhaveused:
";
doublem_gasoline;
cin>>m_gasoline;
cout<<"Yourcarcanrun"< cout<<"ComputingbyEuropeanstyle:
\n";
cout<<"Enterthedistanceinkilometers:
";
doublek_distance;
cin>>k_distance;
cout<<"Enterthepetrolinliters:
";
doublek_gasoline;
cin>>k_gasoline;
cout<<"InEuropeanstyle:
"<<"yourcanused"<<100*k_gasoline/k_distance<<"litersofpetrolper100kilometers\n";
return0;
}
//ex3.7automobilegasolineconsumption-耗油量--欧洲风格(L/100Km)转换成美国风格(mpg)
#include
intmain()
{
usingnamespacestd;
cout<<"Entertheautomobilegasolineconsumptionfigurein\n"
<<"Europeanstyle(litersper100kilometers):
";
doubleEuro_style;
cin>>Euro_style;
cout<<"ConvertstoU.S.style(milespergallon):
"<cout<return0;
}
//Notethat100kilometersis62.14miles,and1gallonis3.875liters.
//Thus,19mpgisabout12.4L/100Km,and27mpgisabout8.7L/100Km.
Entertheautomobilegasolineconsumptionfigurein
Europeanstyle(litersper100kilometers):
12.4
ConvertstoU.S.style(milespergallon):
12.4L/100Km=19.4187mpg
Pressanykeytocontinue
//ex3.7automobilegasolineconsumption-耗油量--美国风格(mpg)转换成欧洲风格(L/100Km)
#include
intmain()
{
usingnamespacestd;
cout<<"Entertheautomobilegasolineconsumptionfigurein\n"
<<"U.S.style(milespergallon):
";
doubleUS_style;
cin>>US_style;
cout<<"ConvertstoEuropeanstyle(milespergallon):
"<cout<return0;
}
//Entertheautomobilegasolineconsumptionfigurein
U.S.style(milespergallon):
19
ConvertstoEuropeanstyle(milespergallon):
19mpg=12.6733L/100Km
Pressanykeytocontinue
第四章复合类型
//ex4.1displaytheinformationofstudent
#include
constintAsize=20;
usingnamespacestd;
structstudent//定义结构描述
{
charfirstname[Asize];
charlastname[Asize];
chargrade;
intage;
};
voiddisplay(student);//函数原型放在结构描述后
intmain()
{
cout<<"whatisyourfirstname?
"<studentlcg;//创建结构变量(结构数据对象)
cin.getline(lcg.firstname,Asize);
cout<<"whatisyourlastname?
"<cin.getline(lcg.lastname,Asize);
cout<<"whatlettergradedoyoudeserve?
"<cin>>lcg.grade;
cout<<"whatisyourage?
"<cin>>lcg.age;
display(lcg);
return0;
}
voiddisplay(studentname)
{
cout<<"Name:
"<cout<<"Grade:
"<cout<<"Age:
"<}
//ex4.2usethestring-classinsteadofchar-array
#include
#include
intmain()
{
usingnamespacestd;
stringname,dessert;
cout<<"Enteryourname:
\n";
getline(cin,name);
cout<<"Enteryourfavoritedessert:
\n";
getline(cin,dessert);
cout<<"Ihavesomedelicious"<cout<<"foryou,"<return0;
}
//有时候会遇到需要按下两次回车键才能正确的显示结果,这是vc++6.0的一个BUG,更改如下:
elseif(_Tr:
:
eq((_E)_C,_D))
{_Chg=true;
_I.rdbuf()->sbumpc();//修改后的
break;}
ex4.3输入其名和姓,并组合显示
#include
#include
constintAsize=20;
intmain()
{
usingnamespacestd;
charfname[Asize];
charlname[Asize];
charfullname[2*Asize+1];
cout<<"Enteryourfirstname:
";//输入名字,存储在fname[]数组中
cin.getline(fname,Asize);
cout<<"Enteryourlastname:
";//输入姓,存储在lname[]数组中
cin.getline(lname,Asize);
strncpy(fullname,lname,Asize);//把姓lname复制到fullname空数组中
strcat(fullname,",");//把“,”附加到上述fullname尾部
strncat(fullname,fname,Asize);//把fname名字附加到上述fullname尾部
fullname[2*Asize]='\0';//为防止字符型数组溢出,在数组结尾添加结束符
cout<<"Here'stheinformationinasinglestring:
"<return0;
}
//ex4.4使用string对象存储、显示组合结果
#include
#include
intmain()
{
usingnamespacestd;
stringfname,lname,attach,fullname;
cout<<"Enteryourfirstname:
";
getline(cin,fname);//note:
将一行输入读取到string类对象中使用的是getline(cin,str)
//它没有使用句点表示法,所以不是类方法
cout<<"Enteryourlastname:
";
getline(cin,lname);
attach=",";
fullname=lname+attach+fname;
cout<<"Here'stheinformationinasinglestring:
"<return0;
}
//ex4.5declareastructandinitializeit声明结果并创建一个变量
#include
constintAsize=20;
structCandyBar
{
charbrand[Asize];
doubleweight;
in