C++PrimerPlus第五版习题解答.docx

上传人:b****4 文档编号:4673528 上传时间:2022-12-07 格式:DOCX 页数:115 大小:42.66KB
下载 相关 举报
C++PrimerPlus第五版习题解答.docx_第1页
第1页 / 共115页
C++PrimerPlus第五版习题解答.docx_第2页
第2页 / 共115页
C++PrimerPlus第五版习题解答.docx_第3页
第3页 / 共115页
C++PrimerPlus第五版习题解答.docx_第4页
第4页 / 共115页
C++PrimerPlus第五版习题解答.docx_第5页
第5页 / 共115页
点击查看更多>>
下载资源
资源描述

C++PrimerPlus第五版习题解答.docx

《C++PrimerPlus第五版习题解答.docx》由会员分享,可在线阅读,更多相关《C++PrimerPlus第五版习题解答.docx(115页珍藏版)》请在冰豆网上搜索。

C++PrimerPlus第五版习题解答.docx

C++PrimerPlus第五版习题解答

Chapter2

//pe2-2.cpp

#include

intmain(void)

{

usingnamespacestd;

cout<<"Enteradistanceinfurlongs:

";

doublefurlongs;

cin>>furlongs;

doublefeet;

feet=220*furlongs;

cout<

<

return0;

}

//pe2-3.cpp

#include

usingnamespacestd;

voidmice();

voidrun();

intmain()

{

mice();

mice();

run();

run();

return0;

}

voidmice()

{

cout<<"Threeblindmice\n";

}

voidrun()

{

cout<<"Seehowtheyrun\n";

}

//pe2-4.cpp

#include

doubleC_to_F(double);

intmain()

{

usingnamespacestd;

cout<<"EnteratemperatureinCelsius:

";

doubleC;

cin>>C;

doubleF;

F=C_to_F(C);

cout<

<

return0;

}

doubleC_to_F(doubletemp)

{

return1.8*temp+32.0;

}

Chapter3

//pe3-1.cpp

#include

constintInch_Per_Foot=12;

intmain(void)

{

usingnamespacestd;

//Note:

someenvironmentsdon'tsupportthebackspacecharacter

cout<<"Pleaseenteryourheightininches:

___/b/b/b";

intht_inch;

cin>>ht_inch;

intht_feet=ht_inch/Inch_Per_Foot;

intrm_inch=ht_inch%Inch_Per_Foot;

cout<<"Yourheightis"<

cout<

return0;

}

//pe3-3.cpp

#include

constdoubleMINS_PER_DEG=60.0;

constdoubleSECS_PER_MIN=60.0;

intmain()

{

usingnamespacestd;

intdegrees;

intminutes;

intseconds;

doublelatitude;

cout<<"Enteralatitudeindegrees,minutes,andseconds:

\n";

cout<<"First,enterthedegrees:

";

cin>>degrees;

cout<<"Next,entertheminutesofarc:

";

cin>>minutes;

cout<<"Finally,enterthesecondsofarc:

";

cin>>seconds;

latitude=degrees+(minutes+seconds/SECS_PER_MIN)/MINS_PER_DEG;

cout<

<

return0;

}

//pe3-5.cpp

#include

intmain(void)

{

usingnamespacestd;

cout<<"Howmanymileshaveyoudrivenyourcar?

";

floatmiles;

cin>>miles;

cout<<"Howmanygallonsofgasolinedidthecaruse?

";

floatgallons;

cin>>gallons;

cout<<"Yourcargot"<

cout<<"milespergallon.\n";

return0;

}

//pe3-6.cpp

#include

constdoubleKM100_TO_MILES=62.14;

constdoubleLITERS_PER_GALLON=3.875;

intmain(void)

{

usingnamespacestd;

doubleeuro_rating;

doubleus_rating;

cout<<"Enterfuelconsumptioninlitersper100km:

";

cin>>euro_rating;

//dividebyLITER_PER_GALLONtogetgallonsper100-km

//dividebyKM100_TO_MILEStogetgallonspermile

//invertresulttogetmilespergallon

us_rating=(LITERS_PER_GALLON*KM100_TO_MILES)/euro_rating;

cout<

cout<

return0;

}

Chapter4

//pe4-2.cpp--storingstringsinstringobjects

#include

#include

intmain()

{

usingnamespacestd;

stringname;

stringdessert;

cout<<"Enteryourname:

\n";

getline(cin,name);//readsthroughnewline

cout<<"Enteryourfavoritedessert:

\n";

getline(cin,dessert);

cout<<"Ihavesomedelicious"<

cout<<"foryou,"<

return0;

}

//pe4-3.cpp--storingstringsinchararrays

#include

#include

constintSIZE=20;

intmain()

{

usingnamespacestd;

charfirstName[SIZE];

charlastName[SIZE];

charfullName[2*SIZE+1];

cout<<"Enteryourfirstname:

";

cin>>firstName;

cout<<"Enteryourlastname:

";

cin>>lastName;

strncpy(fullName,lastName,SIZE);

strcat(fullName,",");

strncat(fullName,firstName,SIZE);

fullName[SIZE-1]='\0';

cout<<"Here'stheinformationinasinglestring:

"

<

return0;

}

//pe4-5.cpp

//acandybarstructure

structCandyBar{

charbrand[40];

doubleweight;

intcalories;

};

#include

intmain()

{

usingnamespacestd;//introducesnamespacestd

CandyBarsnack={"MochaMunch",2.3,350};

cout<<"Brandname:

"<

cout<<"Weight:

"<

cout<<"Calories:

"<

return0;

}

//pe4-7.ccp

#include

constintSlen=70;

structpizza{

charname[Slen];

floatdiameter;

floatweight;

};

intmain(void)

{

usingnamespacestd;

pizzapie;

cout<<"Whatisthenameofthepizzacompany?

";

cin.getline(pie.name,Slen);

cout<<"Whatisthediameterofthepizzaininches?

";

cin>>pie.diameter;

cout<<"Howmuchdoesthepizzaweighinounces?

";

cin>>pie.weight;

cout<<"Company:

"<

cout<<"Diameter:

"<

cout<<"Weight:

"<

return0;

}

Chapter5

//pe5-2.cpp

#include

intmain(void)

{

usingnamespacestd;

doublesum=0.0;

doublein;

cout<<"Enteranumber(0toterminate):

";

cin>>in;

while(in!

=0){

sum+=in;

cout<<"Runningtotal="<

cout<<"Enternextnumber(0toterminate):

";

cin>>in;

}

cout<<"Bye!

\n";

return0;

}

//pe5-4.cpp

//booksales

#include

constintMONTHS=12;

constchar*months[MONTHS]={"January","February","March","April",

"May","June","July","August","September",

"October","November","December"};

intmain()

{

usingnamespacestd;//introducesnamespacestd

intsales[MONTHS];

intmonth;

cout<<"Enterthemonthlysalesfor\"C++forFools\":

\n";

for(month=0;month

{

cout<<"Salesfor"<

";

cin>>sales[month];

}

doubletotal=0.0;

for(month=0;month

total+=sales[month];

cout<<"Totalsales:

"<

return0;

}

//pe5-6.cpp

#include

structcar{charname[20];intyear;};

intmain(void)

{

usingnamespacestd;

intn;

cout<<"Howmanycarsdoyouwishtocatalog?

:

";

cin>>n;

while(cin.get()!

='\n')//getridofrestofline

;

car*pc=newcar[n];

inti;

for(i=0;i

{

cout<<"Car#"<<(i+1)<<":

\n";

cout<<"Pleaseenterthemake:

";

cin.getline(pc[i].name,20);

cout<<"Pleaseentertheyearmade:

";

cin>>pc[i].year;

while(cin.get()!

='\n')//getridofrestofline

;

}

cout<<"Hereisyourcollection:

\n";

for(i=0;i

cout<

delete[]pc;

return0;

}

//pe5-7.cpp--countwordsusingC-stylestring

#include

#include//prototypeforstrcmp()

constintSTR_LIM=50;

intmain()

{

usingnamespacestd;

charword[STR_LIM];

intcount=0;

cout<<"Enterwords(tostop,typetheworddone):

\n";

while(cin>>word&&strcmp("done",word))

++count;

cout<<"Youenteredatotalof"<

return0;

}

//pe5-9.cpp

//nestedloops

#include

intmain()

{

usingnamespacestd;//introducesnamespacestd

introws;

introw;

intcol;

intperiods;

cout<<"Enternumberofrows:

";

cin>>rows;

for(row=1;row<=rows;row++)

{

periods=rows-row;

for(col=1;col<=periods;col++)

cout<<'.';

//colalreadyhascorrectvaluefornextloop

for(;col<=rows;col++)

cout<<'*';

cout<

}

return0;

}

Chapter6

//pe6-1.cpp

#include

#include

intmain()

{

usingnamespacestd;//introducesnamespacestd

charch;

cin.get(ch);

while(ch!

='@')

{

if(!

isdigit(ch))

{

if(isupper(ch))

ch=tolower(ch);

elseif(islower(ch))

ch=toupper(ch);

cout<

}

cin.get(ch);

}

return0;

}

//pe6-3.cpp

#include

intmain(void)

{

usingnamespacestd;

cout<<"Pleaseenteroneofthefollowingchoices:

\n";

cout<<"c)carnivorep)pianist\n"

<<"t)treeg)game\n";

charch;

cin>>ch;

while(ch!

='c'&&ch!

='p'&&ch!

='t'&&ch!

='g')

{

cout<<"Pleaseenterac,p,t,org:

";

cin>>ch;

}

switch(ch)

{

case'c':

cout<<"Acatisacarnivore.\n";

break;

case'p':

cout<<"RaduLupuisapianist.\n";

break;

case't':

cout<<"Amapleisatree.\n";

break;

case'g':

cout<<"Golfisagame.\n";

break;

default:

cout<<"Theprogramshouldn'tgethere!

\n";

}

return0;

}

//pe6-5.cpp

//Neutroniataxation

#include

constdoubleLEV1=5000;

constdoubleLEV2=15000;

constdoubleLEV3=35000;

constdoubleRATE1=0.10;

constdoubleRATE2=0.15;

constdoubleRATE3=0.20;

intmain()

{

usingnamespacestd;

doubleincome;

doubletax;

cout<<"Enteryourannualincomeintvarps:

";

cin>>income;

if(income<=LEV1)

tax=0;

elseif(income<=LEV2)

tax=(income-LEV1)*RATE1;

elseif(income<=LEV3)

tax=RATE1*(LEV2-LEV1)+RATE2*(income-LEV2);

else

tax=RATE1*(LEV2-LEV1)+RATE2*(LEV3-LEV2)

+RATE3*(income-LEV3);

cout<<"YouoweNeutronia"<

return0;

}

//pe6-7.cpp

#include

#include

intmain()

{

usingnamespacestd;

stringword;

charch;

intvowel=0;

intconsonant=0;

intother=0;

cout<<"Enterwords(qtoquit):

\n";

cin>>word;

while(word!

="q")

{

ch=tolower(word[0]);

if(isalpha(ch))

{

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'

||ch=='u')

vowel++;

else

consonant++;

}

else

other++;

cin>>word;

}

cout<

cout<

cout<

return0;

}

//pe6-8.cpp--countin

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1