c+++primer+plus+第五版》编程练习习题答案Word文件下载.docx
《c+++primer+plus+第五版》编程练习习题答案Word文件下载.docx》由会员分享,可在线阅读,更多相关《c+++primer+plus+第五版》编程练习习题答案Word文件下载.docx(112页珍藏版)》请在冰豆网上搜索。
EnteratemperatureinCelsius:
doubleC;
C;
doubleF;
F=C_to_F(C);
C<
degreesCelsius="
F<
degreesFahrenheit\n"
doubleC_to_F(doubletemp)
return1.8*temp+32.0;
//pe3-1.cpp
constintInch_Per_Foot=12;
//Note:
someenvironmentsdon'
tsupportthebackspacecharacter
Pleaseenteryourheightininches:
___/b/b/b"
intht_inch;
ht_inch;
intht_feet=ht_inch/Inch_Per_Foot;
intrm_inch=ht_inch%Inch_Per_Foot;
Yourheightis"
ht_feet<
feet,"
rm_inch<
inch(es).\n"
//pe3-3.cpp
constdoubleMINS_PER_DEG=60.0;
constdoubleSECS_PER_MIN=60.0;
intdegrees;
intminutes;
intseconds;
doublelatitude;
Enteralatitudeindegrees,minutes,andseconds:
\n"
First,enterthedegrees:
degrees;
Next,entertheminutesofarc:
minutes;
Finally,enterthesecondsofarc:
seconds;
latitude=degrees+(minutes+seconds/SECS_PER_MIN)/MINS_PER_DEG;
degrees<
degrees,"
minutes<
minutes,"
seconds<
seconds="
latitude<
degrees\n"
//pe3-5.cpp
Howmanymileshaveyoudrivenyourcar?
floatmiles;
miles;
Howmanygallonsofgasolinedidthecaruse?
floatgallons;
gallons;
Yourcargot"
miles/gallons;
milespergallon.\n"
//pe3-6.cpp
constdoubleKM100_TO_MILES=62.14;
constdoubleLITERS_PER_GALLON=3.875;
intmain(void)
doubleeuro_rating;
doubleus_rating;
Enterfuelconsumptioninlitersper100km:
euro_rating;
//dividebyLITER_PER_GALLONtogetgallonsper100-km
//dividebyKM100_TO_MILEStogetgallonspermile
//invertresulttogetmilespergallon
us_rating=(LITERS_PER_GALLON*KM100_TO_MILES)/euro_rating;
euro_rating<
litersper100kmis"
us_rating<
//pe4-2.cpp--storingstringsinstringobjects
string>
stringname;
stringdessert;
Enteryourname:
getline(cin,name);
//readsthroughnewline
Enteryourfavoritedessert:
getline(cin,dessert);
Ihavesomedelicious"
dessert;
foryou,"
name<
.\n"
//pe4-3.cpp--storingstringsinchararrays
cstring>
constintSIZE=20;
charfirstName[SIZE];
charlastName[SIZE];
charfullName[2*SIZE+1];
Enteryourfirstname:
firstName;
Enteryourlastname:
lastName;
strncpy(fullName,lastName,SIZE);
strcat(fullName,"
"
);
strncat(fullName,firstName,SIZE);
fullName[SIZE-1]='
\0'
Here'
stheinformationinasinglestring:
fullName<
endl;
//pe4-5.cpp
//acandybarstructure
structCandyBar{
charbrand[40];
doubleweight;
intcalories;
};
//introducesnamespacestd
CandyBarsnack={"
MochaMunch"
2.3,350};
Brandname:
snack.brand<
Weight:
snack.weight<
Calories:
snack.calories<
//pe4-7.ccp
constintSlen=70;
structpizza{
charname[Slen];
floatdiameter;
floatweight;
pizzapie;
Whatisthenameofthepizzacompany?
cin.getline(pie.name,Slen);
Whatisthediameterofthepizzaininches?
pie.diameter;
Howmuchdoesthepizzaweighinounces?
pie.weight;
Company:
pie.name<
Diameter:
pie.diameter<
inches\n"
pie.weight<
ounces\n"
//pe5-2.cpp
doublesum=0.0;
doublein;
Enteranumber(0toterminate):
in;
while(in!
=0){
sum+=in;
Runningtotal="
sum<
Enternextnumber(0toterminate):
}
Bye!
//pe5-4.cpp
//booksales
constintMONTHS=12;
constchar*months[MONTHS]={"
January"
February"
March"
April"
May"
June"
July"
August"
September"
October"
November"
December"
intsales[MONTHS];
intmonth;
Enterthemonthlysalesfor\"
C++forFools\"
:
for(month=0;
month<
MONTHS;
month++)
{
Salesfor"
months[month]<
sales[month];
doubletotal=0.0;
total+=sales[month];
Totalsales:
total<
//pe5-6.cpp
structcar{charname[20];
intyear;
intn;
Howmanycarsdoyouwishtocatalog?
n;
while(cin.get()!
='
\n'
)//getridofrestofline
;
car*pc=newcar[n];
inti;
for(i=0;
i<
i++)
Car#"
(i+1)<
Pleaseenterthemake:
cin.getline(pc[i].name,20);
Pleaseentertheyearmade:
pc[i].year;
Hereisyourcollection:
pc[i].year<
pc[i].name<
delete[]pc;
//pe5-7.cpp--countwordsusingC-stylestring
//prototypeforstrcmp()
constintSTR_LIM=50;
charword[STR_LIM];
intcount=0;
Enterwords(tostop,typetheworddone):
while(cin>
word&
&
strcmp("
done"
word))
++count;
Youenteredatotalof"
count<
words.\n"
//pe5-9.cpp
//nestedloops
introws;
introw;
intcol;
intperiods;
Enternumberofrows:
rows;
for(row=1;
row<
=rows;
row++)
periods=rows-row;
for(col=1;
col<
=periods;
col++)
'
.'
//colalreadyhascorrectvaluefornextloop
for(;
*'
//pe6-1.cpp
cctype>
intmain()
charch;
cin.get(ch);
while(ch!
@'
)
if(!
isdigit(ch))
if(isupper(ch))
ch=tolower(ch);
elseif(islower(ch))
ch=toupper(ch);
ch;
//pe6-3.cpp
Pleaseenteroneofthefollowingchoices:
c)carnivorep)pianist\n"
t)treeg)game\n"
while(ch!
c'
&
ch!
p'
t'
g'
Pleaseenterac,p,t,org:
switch(ch)
case'
:
Acatisacarnivore.\n"
break;
RaduLupuisapianist.\n"
Amapleisatree.\n"
Golfisagame.\n"
default:
Theprogramshouldn'
tgethere!
//pe6-5.cpp
//Neutroniataxation
constdoubleLEV1=5000;
constdoubleLEV2=15000;
constdoubleLEV3=35000;
constdoubleRATE1=0.10;
constdoubleRATE2=0.15;
constdoubleRATE3=0.20;
doubleincome;
doubletax;
Enteryourannualincomeintvarps:
income;
if(income<
=LEV1)
tax=0;
elseif(income<
=LEV2)
tax=(income-LEV1)*RATE1;
=LEV3)
tax=RATE1*(LEV2-LEV1)+RATE2*(income-LEV2);
else
tax=RATE1*(LEV2-LEV1)+RATE2*(LEV3-LEV2)
+RATE3*(income-LEV3);
YouoweNeutronia"
tax<
tvarpsintaxes.\n"
//pe6-7.cpp
stringword;
intvowel=0;
intconsonant=0;
intother=0;
Enterwords(qtoquit):
word;
while(word!
="
q"
ch=tolower(word[0]);
if(isalpha(ch))
if(ch=='
a'
||ch=='
e'
i'
o'
||ch=='
u'
vowel++;
consonant++;
other++;
vowel<
"
wordsbeginningwithvowels\n"
consonant<
wordsbeginningwithconsonants\n"
other<
others\n"
//pe6-8.cpp--countingcharacters
fstream>
//fileI/Osuppport
cstdlib>
//supportforexit()
constint