C++primerplus中文版第六版源代码第二章到第四章,后续继续更新………第二章1:#includeiostreamvoidmain(){usingnamespacestd;intcarrots;carrots=25;coutIhave;coutcarrots;coutcarrots.;coutendl;carrots=carrots-1;coutCrunch,crunch.NowIhavecarrotscarrotsendl;}2:#includeiostreamintstonetolb(int);intmain(){usingnamespacestd;intstone;coutEntertheweightinstone:;cinstone;intpounds=stonetolb(stone);coutstonestone=;coutpoundspounds.endl;return0;}intstonetolb(intsts){return14*sts;}3:#includeiostreamvoidmain(){usingnamespacestd;intcarrots;carrots=25;coutHowmanycarrotsdoyouhave?endl;cincarrots;coutHerearetwomore.;carrots=carrots+2;coutNowyouhavecarrotscarrots.endl;//下两行专门测试cin.get()cin.get();cin.get();}4:#includeiostreamusingnamespacestd;voidmain(){coutComeupandC++mesometime.;coutendl;coutYouwon'tregretit!endl;}5#includeiostreamvoidsimon(int);intmain(){usingnamespacestd;simon(3);coutPickaninteger:;intcount;cincount;simon(count);coutDone!endl;return0;}voidsimon(intn){usingnamespacestd;coutSimonsaystouchyourtoesntimes.endl;}6:#includeiostream#includecmathvoidmain(){usingnamespacestd;doublearea;coutEnterthefloorarae,insquarefeet,ofyourhome:;cinarea;doubleside;side=sqrt(area);coutThat'stheequivalentofasquaresidefeettotheside.endl;coutHowfascinating!endl;}第三章1:#includeiostream#includeclimitsusingnamespacestd;intmain(){intn_int=INT_MAX;shortn_short=SHRT_MAX;longn_long=LONG_MAX;coutintissizeof(int)bytes.endl;coutshortissizeofn_shortbytes.endl;coutlongissizeofn_longbytes.endlendl;coutMaximumvalues:endl;coutint:n_intendl;coutshort:n_shortendl;coutlong:n_longendl;coutMinimumintvalue=INT_MINendl;coutBitsperbyts=CHAR_BITendl;return0;}2:#includeiostream#includeclimits#defineZERO0usingnamespacestd;intmain(){shortsam=SHRT_MAX;unsignedshortsue=sam;coutsamhassamdollarsandsuehassue;coutdollarsdeposited.endlAdd$1toeachaccount.endlNow;sam=sam+1;sue=sue+1;coutSamhassamdollarsandsuehassue;coutdollarsdeposited.\npoorsam!endl;sam=ZERO;sue=ZERO;coutsamhassamdollarsandsuehassue;coutdollarsdeposited.endl;coutTake$1fromeachaccount.endlNow;sam=sam-1;sue=sue-1;coutsamhassamdolarsandsuehassue;coutdollarsdeposited.endlLuckysue!endl;return0;}3:#includeiostreamusingnamespacestd;voidmain(){intchest=42;intwaist=0x42;intinseam=042;coutMonsieurcutsastrikingfigure!\n;coutchest=chest(42indecimal)\n;coutwaist=waist(0x42inhex)\n;coutinseam=inseam(042inoctal)\n;}4:#includeiostreamusingnamespacestd;voidmain(){intchest=42;intwaist=42;intinseam=42;coutMonsieurcutsastrikingfigure!\n;coutchest=chest(decimalfor42)endl;couthex;coutwaist=waist(hexadecimalfor42)endl;coutoct;coutinseam=inseam(octalfor42)\nendl;}5:#includeiostreamusingnamespacestd;voidmain(){cout\aoperation\HyperHype\isnowactivated!\n;coutEnteryouragentcode:__________\b\b\b\b\b\b\b\b;longcode;cincode;cout\aYouenteredcode...\n;cout\acodeverified!proceedwithplanz3!\n;}6:#includeiostreamusingnamespacestd;voidmain(){charch;coutEnteracharacter:endl;cinch;coutHola!;coutThankyouforthechcharacter.endl;}7:#includeiostreamusingnamespacestd;voidmain(){charch='M';inti=ch;coutTheASCIIcodeforchisiendl;coutAddonetothecharactercode:endl;ch=ch+1;i=ch;coutTheASCIIcodeforchisiendl;coutDisplayingcharchusingcout.put(ch):;cout.put(ch);cout.put('!');coutendlDoneendl;}8:#includeiostreamusingnamespacestd;voidmain(){cout.setf(ios_base::fixed,ios_base::floatfield);//控制cout显示的形式floattub=10.0/3.0;doublemint=10.0/3.0;constfloatmillion=1.0e6;couttub=tub;cout,amilliontubs=million*tub;cout,\nandtenmilliontubs=;cout10*million*tubendl;coutmint=mintandamillionmints=;coutmillion*mintendl;}9:#includeiostreamusingnamespacestd;voidmain(){floata=2.34e+22f;floatb=a+1.0f;couta=aendl;coutb-a=b-aendl;}10:#includeiostreamusingnamespacestd;voidmain(){doublehats,heads;//或者是floatcout.setf(ios_base::fixed,ios_base::floatfield);coutEnteranumber:;cinhats;coutEnteranothernumber:;cinheads;couthats=hats;heads=headsendl;couthats+heads=hats+headsendl;couthats-heads=hats-headsendl;couthats*heads=hats*headsendl;couthats/heads=hats/headsendl;}11:#includeiostreamusingnamespacestd;voidmain(){cout.setf(ios_base::fixed,ios_base::floatfield);coutIntegerdivision:9/5=9/5endl;coutFloating-pointdivision:9.0/5.0=;cout9.0/5.0endl;coutMixeddivision:9.0/5=9.0/5endl;coutdoubleconstants:1.e7/9.0=;cout1.e7/9.0endl;coutfloatconstants:1.e7f/9.0f=;cout1.e7f/9.0fendl;}12:#includeiostreamusingnamespacestd;voidmain(){constintLbs_per_stn=14;intlbs;coutEnteryourweightinpounds:;cinlbs;intstone=lbs/Lbs_per_stn;intpounds=lbs%Lbs_per_stn;coutlbspoundsarestonestone,poundspound(s).\n;}13:#includeiostreamusingnamespacestd;voidmain(){cout.setf(ios_base: