c++primerplus课后习题答案_第1页
c++primerplus课后习题答案_第2页
c++primerplus课后习题答案_第3页
c++primerplus课后习题答案_第4页
c++primerplus课后习题答案_第5页
已阅读5页,还剩170页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、solutions for programming exercises in c+ primer plus, 5th edition目录chapter 21pe 2-11pe 2-22pe 2-32pe 2-42pe 2-53pe 2-63chapter 34pe 3-14pe 3-24pe 3-35pe 3-45pe 3-56pe 3-66chapter 47pe 4-17pe 4-27pe 4-38pe 4-48pe 4-59pe 4-69pe 4-710pe 4-810pe 4-911chapter 512pe 5-112pe 5-212pe 5-313pe 5-413pe 5-514p

2、e 5-616pe 5-717pe 5-818pe 5-918chapter 619pe 6-119pe 6-219pe 6-320pe 6-421pe 6-522pe 6-623pe 6-725pe 6-826pe 6-926chapter 728pe 7-128pe 7-229pe 7-330pe 7-431pe 7-532pe 7-632pe 7-734pe 7-835pe 7-937chapter 838pe 8-138pe 8-238pe 8-339pe 8-440pe 8-541pe 8-642pe 8-743chapter 944pe 9-144pe 9-246pe 9-346p

3、e 9-447chapter 1047pe 10-147pe 10-249pe 10-350pe 10-451pe 10-553pe 10-656pe 10-757pe 10-859chapter 1161pe 11-161pe 11-266pe 11-370pe 11-475pe 11-577pe 11-680pe 11-782chapter 1285pe 12-185pe 12-286pe 12-390pe 12-493pe 12-595pe 12-699chapter 13104pe 13-1104pe 13-2108pe 13-3112pe 13-4115chapter 14119pe

4、 14-1119pe 14-2122pe 14-3125pe 14-4131pe 14-5134chapter 15140pe 15-1140pe 15-2143pe 15-3144pe 15-4147chapter 16151pe 16-1151pe 16-2151pe 16-3152pe 16-4153pe 16-5154pe 16-6155pe 16-7157pe 16-8158chapter 17159pe 17-1159pe 17-2159pe 17-3160pe 17-4160pe 17-5162pe 17-6163pe 17-7172chapter 2pe 2-1/pe2-1.c

5、pp#include int main()using namespace std;coutname: liangdongxu endl;coutaddress: china daliann;return 0;pe 2-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;pe 2-3/ pe2-3.cpp#include using names

6、pace std;void mice();void run();int main() mice(); mice(); run(); run(); return 0;void mice() cout three blind micen;void run() cout see how they runn;pe 2-4/ pe2-4.cpp#include double c_to_f(double);int main() using namespace std; cout c; double f; f = c_to_f(c); cout c degrees celsius = f degrees f

7、ahrenheitn; return 0;double c_to_f(double temp) return 1.8 * temp + 32.0;pe 2-5/ pe2-5.cpp#include double lightyear_to_astro(double);int main() using namespace std; coutyears; astrounit = lightyear_to_astro(years); coutyears light years = astrounit astronomical units.n; return 0;double lightyear_to_

8、astro(double years) return 63240 * years;pe 2-6/ pe2-6.cpp#include using namespace std;void maketime(int, int);int main()double hours, minutes; couthours; coutminutes; maketime(hours,minutes); return 0;void maketime(int hours, int minutes) couttime: hours:minutesendl;chapter 3pe 3-1/ pe3-1.cpp#inclu

9、de const int inch_per_foot = 12;int main(void) using namespace std;/ note: some environments 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;pe 3-2/

10、pe3-2.cpp#include const int inch_per_foot = 12;const double meter_per_inch = 0.0254;const double pound_per_kilo = 2.2;int main(void) using namespace std;/ note: some environments dont support the backspace character cout ht_footht_inch; coutweight;double bmi = (weight/pound_per_kilo)/(ht_foot * inch

11、_per_foot+ht_inch)/meter_per_inch)*(ht_foot * inch_per_foot+ht_inch)/meter_per_inch);cout your height is ( ht_foot*inch_per_foot+ht_inch)” inches”;cout” and ”; cout your bmi is bmiendl; return 0;pe 3-3/ pe3-3.cpp#include const double mins_per_deg = 60.0;const double secs_per_min = 60.0;int main() us

12、ing 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

13、= latitude degreesn; return 0; pe 3-4/ pe3-4.cpp#include const int hours_per_day = 24;const int mins_per_hour = 60;const int seconds_per_minute = 60;int main() using namespace std; int day; int hour; int minute;int second; long seconds; cout seconds; day = seconds/(hours_per_day * mins_per_hour * se

14、conds_per_minute); hour = (seconds%(hours_per_day * mins_per_hour * seconds_per_minute)/(mins_per_hour*seconds_per_minute);minute = (seconds%(hours_per_day * mins_per_hour * seconds_per_minute)%(mins_per_hour*seconds_per_minute)/seconds_per_minute;second = (seconds%(hours_per_day * mins_per_hour * s

15、econds_per_minute)%(mins_per_hour*seconds_per_minute)%seconds_per_minute; cout seconds seconds = day days, hour hours, minute minutessecond secondsn; return 0; pe 3-5/ pe3-5.cpp#include int main(void) using namespace std; cout miles; cout gallons; cout your car got miles / gallons; cout miles per ga

16、llon.n; return 0;pe 3-6/ 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 ge

17、t gallons per 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 4pe 4-1/ pe4-1.cpp - storing strings in string objects#include int main() using names

18、pace std;const int arsize = 20;char firstnamearsize;char lastnamearsize;char grade;int age; cout what is your first name? ; cin.getline(firstname,arsize);coutlastname;/cin.getline(lastname,arsize); cout grade;coutage; coutname: lastname, firstnameendl;coutgrade: (char)(grade+1)endl;coutage: ageendl;

19、 return 0; pe 4-2/ pe4-2.cpp - storing strings in string objects#include #include int main() using namespace std; 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 des

20、sert; cout for you, name .n; return 0; pe 4-3/ 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); strcat(fullna

21、me, , ); strncat(fullname, firstname, size); fullname2*size - 1 = 0; cout heres the information in a single string: fullname endl; return 0; pe 4-4/ pe4-4.cpp#include #include int main() using namespace std; string firstname; string lastname; string fullname; cout firstname; cout lastname; fullname

22、+= lastname; fullname +=, ; fullname += firstname; cout heres the information in a single string: fullname endl; return 0; pe 4-5/ pe4-5.cpp#include / a candybar structurestruct candybar char brand40; double weight; int calories;int main() using namespace std; /introduces namespace std candybar snac

23、k = mocha munch, 2.3, 350 ; cout brand name: snack.brand endl; cout weight: snack.weight endl; cout calories: snack.calories endl; return 0;pe 4-6/ pe4-6.cpp#include struct candybar char brand40; double weight; int calories;int main() using namespace std; /introduces namespace std candybar snack3 =

24、mocha munch, 2.3, 350,moncha,2.4,360, munch,2.5,370; coutthe first of snack is:n; cout brand name: snack0.brand endl; cout weight: snack0.weight endl; cout calories: snack0.calories endl;coutthe second of snack is:n; cout brand name: snack1.brand endl; cout weight: snack1.weight endl; cout calories:

25、 snack1.calories endl;coutthe third of snack is:n; cout brand name: snack2.brand endl; cout weight: snack2.weight endl; cout calories: snack2.calories endl; return 0;pe 4-7/ pe4-7.cpp#include const int slen = 70;struct pizza char nameslen; float diameter; float weight;int main(void) using namespace

26、std; pizza pie; cout what is the name of the pizza company? ; cin.getline(, slen); cout pie.diameter; cout pie.weight; cout company: n; cout diameter: pie.diameter inchesn; cout weight: pie.weight ouncesn; return 0;pe 4-8/ pe4-8.cpp#include const int slen = 70;struct pizza char name

27、slen; float diameter; float weight;int main(void) using namespace std; pizza* pie = new pizza; cout name, slen); cout pie-diameter; cout pie-weight; cout company: name n; cout diameter: diameter inchesn; cout weight: weight ouncesn; return 0;pe 4-9/ pe4-9.ccp#include struct candybar char brand40; do

28、uble weight; int calories;int main() using namespace std; /introduces namespace std candybar *candy= new candybar3; strcpy(candy0.brand,mocha munch); candy0.weight=2.3; candy0.calories=350; strcpy(candy1.brand,moncha); candy1.weight=2.4; candy1.calories=360; strcpy(candy2.brand,munch); candy2.weight

29、=2.5; candy2.calories=370; coutthe first of candy is:n; cout brand name: candy0.brand endl; cout weight: candy0.weight endl; cout calories: candy0.calories endl; coutthe second of candy is:n; cout brand name: candy1.brand endl; cout weight: candy1.weight endl; cout calories: candy1.calories endl; co

30、utthe third of candy is:n; cout brand name: candy2.brand endl; cout weight: candy2.weight endl; cout calories: candy2.calories endl; return 0;chapter 5pe 5-1/ pe5-1.cpp#include int main() using namespace std; int small,big;int sum = 0;coutplease input two integers for calculating the sum.n;coutsmall

31、;coutbig;for(int i=small;i=big;i+)sum += i;coutthe sum result between small and big is:sumendl; return 0;pe 5-2/ 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;pe 5-3/ pe

32、5-3.cpp#include double investment = 100;const double interest1 = 0.1;const double interest2 = 0.05;int main()using namespace std;double bonus1,bonus2;double sum1 = 0;double sum2 = 0;int year = 0;bonus1 = investment * interest1;sum1 += bonus1;bonus2 = investment * interest2;investment += bonus2;sum2

33、+= bonus2; while(sum1sum2)year+;sum1 += bonus1;bonus2 = investment*interest2;investment += bonus2;sum2 += bonus2;coutover year+1 years cleos investment exceeds daphnes investment!n;coutcleos investment value is:sum1endl;coutdaphnes investment value is:sum2endl;return 0;pe 5-4/ pe5-4a.cpp/ book sales

34、 with char array.#include const int months = 12;const char * 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 f

35、ools:n; for (month = 0; month months; month+) cout sales for monthsmonth salesmonth; int total = 0; for (month = 0; month months; month+) total += salesmonth; cout total sales: total endl; return 0;/ pe5-4b.cpp/ book sales with string array.#include #include using namespace std;const int months = 12

36、;const string monthsmonths = january, february, march,april,may,june, july, august, september,october, november, december;int main() int salesmonths; int month; cout enter the monthly sales for c+ for fools:n; for (month = 0; month months; month+) cout sales for monthsmonth salesmonth; int total = 0

37、; for (month = 0; month months; month+) total += salesmonth; cout total sales: total endl; return 0;pe 5-5/ pe5-5.cpp#include const int months = 12;const int year = 3;const char * monthsmonths = january, february, march, april,may, june, july, august, september,october, november, december;int main()

38、 using namespace std; int salesyearmonths; int month; int year;for(year = 0;year 3; year+)if(year = 0)coutenter the first year sales for c+ for fools:n;else if(year = 1)coutenter the second year sales for c+ for fools:n;elsecoutenter the third year sales for c+ for fools:n;for (month = 0; month months; month+)cout sales for monthsmonth salesyearmonth; int totalone = 0;int totaltwo = 0;int totalthree = 0; int total = 0;int sum =0;for(year = 0; year 3; year+)sum =0; for (month = 0; month months; month+) total += salesyearmonth;sum +=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论