C++ Primer Plus中文第五版编程练习答案-非扫描.pdf
《C++ Primer Plus中文第五版编程练习答案-非扫描.pdf》由会员分享,可在线阅读,更多相关《C++ Primer Plus中文第五版编程练习答案-非扫描.pdf(65页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 1 of 65 September 2,2004Chapter 2/?pe2-2.cpp#include?int?main(void)?using?namespace?std;?cout?furlongs;?double?feet;?feet?=?220?*?furlongs;?cout?furlongs?furlongs?=?feet?feetn;?return?0;/?pe2-3.cpp#include?using?namespace?std;void?mi
2、ce();void?run();int?main()?mice();?mice();?run();?run();?return?0;void?mice()?cout?Three?blind?micen;void?run()?cout?See?how?they?runn;/?pe2-4.cpp#include?double?C_to_F(double);int?main()?using?namespace?std;?cout?C;?double?F;?F?=?C_to_F(C);C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描1/65Solutions for Progra
3、mming Exercises in C+Primer Plus,5th EditionSP 2 of 65 September 2,2004?cout?C?degrees?Celsius?=?F?degrees?Fahrenheitn;?return?0;double?C_to_F(double?temp)?return?1.8?*?temp?+?32.0;Chapter 3/?pe3-1.cpp#include?const?int?Inch_Per_Foot?=?12;int?main(void)?using?namespace?std;/?Note:?some?environments?
4、dont?support?the?backspace?character?cout?ht_inch;?int?ht_feet?=?ht_inch?/?Inch_Per_Foot;?int?rm_inch?=?ht_inch?%?Inch_Per_Foot;?cout?Your?height?is?ht_feet?feet,?;?cout?rm_inch?inch(es).n;?return?0;/?pe3-3.cpp#include?const?double?MINS_PER_DEG?=?60.0;const?double?SECS_PER_MIN?=?60.0;int?main()?usin
5、g?namespace?std;?int?degrees;?int?minutes;?int?seconds;?double?latitude;?cout?Enter?a?latitude?in?degrees,?minutes,?and?seconds:n;?cout?degrees;?cout?minutes;?cout?seconds;?latitude?=?degrees?+?(minutes?+?seconds?/?SECS_PER_MIN)/MINS_PER_DEG;?cout?degrees?degrees,?minutes?minutes,?seconds?seconds?=?
6、latitude?degreesn;?return?0;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描2/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 3 of 65 September 2,2004/?pe3-5.cpp#include?int?main(void)?using?namespace?std;?cout?miles;?cout?gallons;?cout?Your?car?got?miles?/?gallons;?cout?miles?per?gallon.n;
7、?return?0;/?pe3-6.cpp#include?const?double?KM100_TO_MILES?=?62.14;const?double?LITERS_PER_GALLON?=?3.875;int?main?(?void?)?using?namespace?std;?double?euro_rating;?double?us_rating;?cout?euro_rating;?/?divide?by?LITER_PER_GALLON?to?get?gallons?per?100-km?/?divide?by?KM100_TO_MILES?to?get?gallons?per
8、?mile?/?invert?result?to?get?miles?per?gallon?us_rating?=?(LITERS_PER_GALLON?*?KM100_TO_MILES)?/?euro_rating;?cout?euro_rating?liters?per?100?km?is?;?cout?us_rating?miles?per?gallon.n;?return?0;Chapter 4/?pe4-2.cpp?-?storing?strings?in?string?objects#include?#include?int?main()?using?namespace?std;?
9、string?name;?string?dessert;?cout?Enter?your?name:n;?getline(cin,?name);?/?reads?through?newline?cout?Enter?your?favorite?dessert:n;?getline(cin,?dessert);?cout?I?have?some?delicious?dessert;?cout?for?you,?name?.n;?return?0;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描3/65Solutions for Programming Exercises
10、in C+Primer Plus,5th EditionSP 4 of 65 September 2,2004/?pe4-3.cpp?-?storing?strings?in?char?arrays#include?#include?const?int?SIZE?=?20;int?main()?using?namespace?std;?char?firstNameSIZE;?char?lastNameSIZE;?char?fullName2*SIZE?+?1;?cout?firstName;?cout?lastName;?strncpy(fullName,lastName,SIZE);?str
11、cat(fullName,?,?);?strncat(fullName,?firstName,?SIZE);?fullNameSIZE?-?1?=?0;?cout?Heres?the?information?in?a?single?string:?fullName?endl;?return?0;?/?pe4-5.cpp/?a?candybar?structurestruct?CandyBar?char?brand40;?double?weight;?int?calories;#include?int?main()?using?namespace?std;?/introduces?namespa
12、ce?std?CandyBar?snack?=?Mocha?Munch,?2.3,?350?;?cout?Brand?name:?snack.brand?endl;?cout?Weight:?snack.weight?endl;?cout?Calories:?snack.calories?endl;?return?0;/?pe4-7.ccp#include?const?int?Slen?=?70;struct?pizza?char?nameSlen;?float?diameter;?float?weight;int?main(void)C+Pr i m e r Pl u s 中文第五版编程练习
13、答案-非扫描4/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 5 of 65 September 2,2004?using?namespace?std;?pizza?pie;?cout?What?is?the?name?of?the?pizza?company?;?cin.getline(pie.name,?Slen);?cout?pie.diameter;?cout?pie.weight;?cout?Company:?pie.name?n;?cout?Diameter:?pie.diameter?in
14、chesn;?cout?Weight:?pie.weight?ouncesn;?return?0;Chapter 5/?pe5-2.cpp#include?int?main(void)?using?namespace?std;?double?sum?=?0.0;?double?in;?cout?in;?while?(in?!=?0)?sum?+=?in;?cout?Running?total?=?sum?n;?cout?in;?cout?Bye!n;?return?0;/?pe5-4.cpp/?book?sales#include?const?int?MONTHS?=?12;const?cha
15、r?*?monthsMONTHS?=?January,?February,?March,?April,?May,?June,?July,?August,?September,?October,?November,?December;int?main()?using?namespace?std;?/introduces?namespace?std?int?salesMONTHS;?int?month;?cout?Enter?the?monthly?sales?for?C+?for?Fools:n;?for?(month?=?0;?month?MONTHS;?month+)?cout?Sales?
16、for?monthsmonth?salesmonth;?C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描5/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 6 of 65 September 2,2004?double?total?=?0.0;?for?(month?=?0;?month?MONTHS;?month+)?total?+=?salesmonth;?cout?Total?sales:?total?endl;?return?0;/?pe5-6.cpp#include?str
17、uct?car?char?name20;?int?year;int?main(void)?using?namespace?std;?int?n;?cout?n;?while(cin.get()?!=?n)?/?get?rid?of?rest?of?line?;?car?*?pc?=?new?car?n;?int?i;?for?(i?=?0;?i?n;?i+)?cout?Car?#?(i?+?1)?:n;?cout?Please?enter?the?make:?;?cin.getline(pci.name,20);?cout?pci.year;?while(cin.get()?!=?n)?/?g
18、et?rid?of?rest?of?line?;?cout?Here?is?your?collection:n;?for?(i?=?0;?i?n;?i+)?cout?pci.year?pci.name?n;?delete?pc;?return?0;/?pe5-7.cpp?-?count?words?using?C-style?string#include?#include?/?prototype?for?strcmp()const?int?STR_LIM?=?50;int?main()?using?namespace?std;?char?wordSTR_LIM;?int?count?=?0;?
19、cout?word?&?strcmp(done,?word)?+count;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描6/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 7 of 65 September 2,2004?cout?You?entered?a?total?of?count?words.n;?return?0;?/?pe5-9.cpp/nested?loops#include?int?main()?using?namespace?std;?/introduces?n
20、amespace?std?int?rows;?int?row;?int?col;?int?periods;?cout?rows;?for?(row?=?1;?row?=?rows;?row+)?periods?=?rows?-?row;?for?(col?=?1;?col?=?periods;?col+)?cout?.;?/?col?already?has?correct?value?for?next?loop?for?(?;?col?=?rows;?col+)?cout?*;?cout?endl;?return?0;Chapter 6/?pe6-1.cpp#include?#include?
21、int?main(?)?using?namespace?std;?/introduces?namespace?std?char?ch;?cin.get(ch);?while(ch?!=?)?if?(!isdigit(ch)?if?(isupper(ch)?ch?=?tolower(ch);?else?if?(islower(ch)?ch?=?toupper(ch);?cout?ch;?cin.get(ch);?return?0;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描7/65Solutions for Programming Exercises in C+Prim
22、er Plus,5th EditionSP 8 of 65 September 2,2004/?pe6-3.cpp#include?int?main(void)?using?namespace?std;?cout?Please?enter?one?of?the?following?choices:n;?cout?c)?carnivore?p)?pianistn?ch;?while?(ch?!=?c?&?ch?!=?p?&?ch?!=?t?&?ch?!=?g)?cout?ch;?switch?(ch)?case?c?:?cout?A?cat?is?a?carnivore.n;?break;?ca
23、se?p?:?cout?Radu?Lupu?is?a?pianist.n;?break;?case?t?:?cout?A?maple?is?a?tree.n;?break;?case?g?:?cout?Golf?is?a?game.n;?break;?default?:?cout?The?program?shouldnt?get?here!n;?return?0;/?pe6-5.cpp/?Neutronia?taxation#include?const?double?LEV1?=?5000;const?double?LEV2?=?15000;const?double?LEV3?=?35000;
24、const?double?RATE1?=?0.10;const?double?RATE2?=?0.15;const?double?RATE3?=?0.20;int?main(?)?using?namespace?std;?double?income;?double?tax;?cout?income;?if?(income?=?LEV1)?tax?=?0;?else?if?(income?=?LEV2)?tax?=?(income?-?LEV1)?*?RATE1;?else?if?(income?=?LEV3)?tax?=?RATE1?*?(LEV2?-?LEV1)?+?RATE2?*?(inc
25、ome?-?LEV2);?else?tax?=?RATE1?*?(LEV2?-?LEV1)?+?RATE2?*?(LEV3?-?LEV2)?+?RATE3?*?(income?-?LEV3);?cout?You?owe?Neutronia?tax?tvarps?in?taxes.n;C+Pr i m e r Pl u s 中文第五版编程练习答案-非扫描8/65Solutions for Programming Exercises in C+Primer Plus,5th EditionSP 9 of 65 September 2,2004?return?0;/?pe6-7.cpp#includ
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ Primer Plus中文第五版编程练习答案-非扫描 Plus 中文 第五 编程 练习 答案 扫描
限制150内