C++ PRIMER PLUS(第六版)第二至第五章课后编程练习答案.doc_第1页
C++ PRIMER PLUS(第六版)第二至第五章课后编程练习答案.doc_第2页
C++ PRIMER PLUS(第六版)第二至第五章课后编程练习答案.doc_第3页
C++ PRIMER PLUS(第六版)第二至第五章课后编程练习答案.doc_第4页
C++ PRIMER PLUS(第六版)第二至第五章课后编程练习答案.doc_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

第二章:开始学习c+/ex2.1-display your name and address#includeint main(void)using namespace std;coutmy name is liao chunguang and i live in hunan chenzhou.n”;/ex2.2-convert the furlong units to yard uints-把浪单位换位码单位#includedouble fur2yd(double);int main()using namespace std;coutfur;coutconvert the furlong to yardendl;double yd;yd=fur2yd(fur);coutfur furlong is yd yardendl;return 0;double fur2yd(double t)return 220*t;/ex2.3-每个函数都被调用两次#includevoid mice();void see();using namespace std;int main()mice();mice();see();see();return 0;void mice()coutthree blind miceendl;void see()coutsee how they runendl;/ex2.4#includeint main() using namespace std; coutage; int month; month=age*12; coutage years is month monthsendl; return 0;/ex2.5-convert the celsius valve to fahrenheit value#includedouble c2f(double);int main()using namespace std;coutc;double f;f=c2f(c);coutc degrees celsius is f degrees fahrenheit.endl;return 0;double c2f(double t)return 1.8*t+32; /ex2.6-convert the light years valve to astronomical units-把光年转换为天文单位#includedouble convert(double);/函数原型int main()using namespace std;coutlight_years;double astro_units;astro_units=convert(light_years);coutlight_years light_years = astro_units astronomical units.endl;return 0;double convert(double t)return 63240*t;/1 光年=63240 天文单位 /ex2.7-显示用户输入的小时数和分钟数#includevoid show();main()using namespace std;show();return 0;void show()using namespace std;int h,m;couth;coutm;couttime:h:mendl;第三章:处理数据/ex3.1将身高用英尺(feet)和英寸(inch)表示#includeconst int inch_per_feet=12;/ const常量-1feet=12inches-1英尺=12英寸int main()using namespace std;coutht_inch;int ht_feet=ht_inch/inch_per_feet;/取商int rm_inch=ht_inch%inch_per_feet;/取余coutyour height is ht_feet feet,and rm_inch inchesn;return 0;/ex3.2-计算相应的body mass index(体重指数)#includeconst int inch_per_feet=12;const double meter_per_inch=0.0254;const double pound_per_kilogram=2.2;int main()using namespace std;coutplease enter your height:endl;coutht_feet;coutht_inch;coutwt_pound;int inch;inch=ht_feet*inch_per_feet+ht_inch;double ht_meter;ht_meter=inch*meter_per_inch;double wt_kilogram;wt_kilogram=wt_pound/pound_per_kilogram;coutendl;coutyour pensonal body information as follows:endl;cout身高:inch(英尺inch)n身高:ht_meter(米meter)n体重:wt_kilogram(千克kilogram)n;double bmi;bmi=wt_kilogram/(ht_meter*ht_meter);coutyour body mass index(体重指数) is bmiendl;return 0;/ex3.3 以度,分,秒输入,以度输出#includeconst int minutes_per_degree=60;const int seconds_per_minute=60;int main()using namespace std;coutenter a latitude in degrees,minutes,and seconds:n;coutdegree;coutminute;coutsecond;double show_in_degree;show_in_degree=(double)degree+(double)minute/minutes_per_degree+(double)second/minutes_per_degree/seconds_per_minute;coutdegree degrees,minute minutes,secondseconds =show_in_degree degreesn;return 0;/ex3.4#includeconst int hours_per_day=24;const int minutes_per_hour=60;const int seconds_per_minute=60;int main()using namespace std;coutseconds;int day,hour,minute,second;day=seconds/seconds_per_minute/minutes_per_hour/hours_per_day;hour=seconds/seconds_per_minute/minutes_per_hour%hours_per_day;minute=seconds/seconds_per_minute%minutes_per_hour;second=seconds%seconds_per_minute;coutsecondsseconds = day days,hour hours,minute minutes,second secondsn;return 0;/ex3.5#includeint main() using namespace std; coutworld_population; coutus_population; double percentage; percentage=(double)us_population/world_population*100; coutthe population of the us is percentage% of the world population.n; return 0;/ex3.6 汽车耗油量-美国(mpg)or欧洲风格(l/100km)#includeint main()using namespace std;coutm_distance;coutm_gasoline;coutyour car can run m_distance/m_gasoline miles per gallonn;coutcomputing by european style:n;coutk_distance;coutk_gasoline;coutin european style:your can used 100*k_gasoline/k_distance liters of petrol per 100 kilometersn;return 0;/ex3.7 automobile gasoline consumption-耗油量-欧洲风格(l/100km)转换成美国风格(mpg)#includeint main()using namespace std;coutenter the automobile gasoline consumption figure inneuro_style;coutconverts to u.s. style(miles per gallon):endl;couteuro_style l/100km = 62.14*3.875/euro_style mpgn;return 0;/ note that 100 kilometers is 62.14 miles, and 1 gallon is 3.875 liters. /thus, 19 mpg is about 12.4 l/100km, and 27 mpg is about 8.7 l/100km.enter the automobile gasoline consumption figure ineuropean style(liters per 100 kilometers):12.4converts to u.s. style(miles per gallon):12.4 l/100km = 19.4187 mpgpress any key to continue/ ex3.7 automobile gasoline consumption-耗油量-美国风格(mpg)转换成欧洲风格(l/100km)#includeint main()using namespace std;coutenter the automobile gasoline consumption figure innus_style;coutconverts to european style(miles per gallon):endl;coutus_style mpg = 62.14*3.875/us_stylel/100kmn;return 0;/ enter the automobile gasoline consumption figure inu.s. style(miles per gallon):19converts to european style(miles per gallon):19 mpg = 12.6733l/100kmpress any key to continue第四章 复合类型/ex4.1 display the information of student#includeconst int asize=20;using namespace std;struct student/定义结构描述char firstnameasize;char lastnameasize;char grade;int age;void display(student);/函数原型放在结构描述后int main()coutwhat is your first name?endl;student lcg;/创建结构变量(结构数据对象)cin.getline(lcg.firstname,asize);coutwhat is your last name?endl;cin.getline(lcg.lastname,asize);coutwhat letter grade do you deserve?lcg.grade;coutwhat is your age?lcg.age;display(lcg);return 0;void display(student name)coutname: name.firstname,name.lastnameendl;coutgrade:char(name.grade+1)endl;coutage:name.ageendl;/ex4.2 use the string-class instead of char-array#include#includeint main()using namespace std;string name,dessert;coutenter your name: n;getline(cin,name); coutenter your favorite dessert: n;getline(cin,dessert); couti have some delicious dessert;cout for you, namesbumpc();/修改后的 break; ex4.3 输入其名和姓,并组合显示#include#includeconst int asize=20;int main()using namespace std;char fnameasize;char lnameasize;char fullname2*asize+1;coutenter your first name:;/输入名字,存储在fname数组中cin.getline(fname,asize);coutenter your last name:;/输入姓,存储在lname数组中cin.getline(lname,asize);strncpy(fullname,lname,asize);/把姓lname复制到fullname空数组中strcat(fullname, );/把“, ”附加到上述fullname尾部strncat(fullname,fname,asize);/把fname名字附加到上述fullname尾部fullname2*asize=0;/为防止字符型数组溢出,在数组结尾添加结束符 coutheres the information in a single string:fullnameendl;/显示组合结果return 0;/ex4.4 使用string对象 存储、显示组合结果#include#includeint main()using namespace std;string fname,lname,attach,fullname;coutenter your first name:;getline(cin,fname);/note:将一行输入读取到string类对象中使用的是getline(cin,str) /它没有使用句点表示法,所以不是类方法coutenter your last name:;getline(cin,lname);attach=, ;fullname=lname+attach+fname;coutheres the information in a single string:fullnameendl;return 0;/ex4.5 declare a struct and initialize it 声明结果并创建一个变量#includeconst int asize=20;struct candybarchar brandasize;double weight;int calory;int main()using namespace std;candybar snack=mocha munch,2.3,350;coutheres the information of snack:n;coutbrand:snack.brandendl;coutweight:snack.weightendl;coutcalory:snack.caloryendl;return 0;/ex4.6 结构数组的声明及初始化#includeconst int asize=20;struct candybarchar brandasize;double weight;int calory;int main()using namespace std;candybar snack3=mocha munch,2.3,350,xufuji,1.1,300,alps,0.4,100; for(int i=0;i3;i+)/利用for循环来显示snack变量的内容coutsnacki.brandendlsnacki.weightendlsnacki.caloryendlendl;return 0;/ex4.7 pizza披萨饼#include#includeconst int size=20;struct pizza/声明结构char companysize;double diameter;double weight;int main()using namespace std;pizza pie;/创建一个名为pie的结构变量coutwhats the name of pizza company:;cin.getline(pany,size);coutpie.diameter;coutpie.weight;coutcompany:panyendl;coutdiameter:pie.diameterinchesendl;coutweight:pie.weightounchesendl;return 0;/ex4.8 pizza pie 披萨饼 使用new创建动态结构#include#includeconst int size=20;struct pizza/声明结构char companysize;double diameter;double weight;int main()using namespace std;pizza *pie=new pizza;/使用new创建动态结构coutpie-diameter;cin.get();/读取下一个字符coutcompany,size);coutpie-weight;coutdiameter:diameter inchesendl;coutcompany:companyendl;coutweight:weight ounchesendl;delete pie;/delete释放内存return 0;/ex.4.9 使用new动态分配数组方法1#include#includeusing namespace std;struct candybarstring brand;double weight;int calory;int main()candybar *snack= new candybar3;snack0.brand=a;/单个初始化由new动态分配的内存snack0.weight=1.1;snack0.calory=200;snack1.brand=b;snack1.weight=2.2;snack1.calory=400;snack2.brand=c;snack2.weight=4.4;snack2.calory=500;for(int i=0;i3;i+)cout brand: snacki.brand endl;cout weight: snacki.weight endl;cout calorie: snacki.calory endlendl;delete snack; return 0;/ex.4.10 数组方法1#include int main() using namespace std; const int size = 3; int successsize; cout success0success1success2; coutsuccess1:success0endl;coutsuccess2:success1endl;coutsuccess3:success2endl;double average=(success0+success1+success2)/3;coutaverage:averageendl; return 0; /ex.4.10 array方法2#include #include int main() using namespace std;arrayad=0; cout ad0ad1ad2; coutsuccess1:ad0endl;coutsuccess2:ad1endl;coutsuccess3:ad2endl;ad3=(ad0+ad1+ad2)/3;coutaverage:ad3endl; return 0; 第五章 循环和关系表达式/ex.5.1#include int main() using namespace std; coutnum1num2; int sum=0; for(int temp=num1;temp=num2;+temp)/or temp+ sum+=temp; coutthe sum from num1 to num2 is sumendl; return 0;/ex.5.2#include #includeint main() using namespace std;arrayad=0; ad1=ad0=1l;for(int i=2;i101;i+)adi=i*adi-1;for(int i=0;i101;i+)couti! = adiendl; return 0;/ex.5.3#include int main() using namespace std; coutnum)&num!=0) sum+=num; coutso far, the sum is sumendl; coutplease enter an integer: ; return 0;/ex.5.4#include int main() using namespace std; double sum1,sum2; sum1=sum2=0.0; int year=0; while(sum2=sum1) +year; sum1+=10; sum2=(100+sum2)*0.05+sum2; cout经过year年后,cleo的投资价值才能超过daphne的投资价值。endl; cout此时,cleo的投资价值为sum1,而daphne的投资价值为sum2endl; return 0; /ex.5.5#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; int salesmonths,sum=0; for(int i=0;imonths;i+) cout请输入在monthsisalesi; sum+=salesi; cout这一年中的c+ for fools的总销售量为:sumendl; return 0;/ex.5.6#include const int months = 12;const char* monthsmonths=january,february,march,april,may,june,july,august,september,october,november,december;const char* years3=第一年,第二年,第三年;int main() using namespace std; int year_sale3,sum=0,sales3months; for(int i=0;i3;i+) int temp=0; coutyearsi的每个月销售量:endl; for(int j=0;jmonths;j+) cout请输入monthsjsalesij; temp+=salesij; year_salei=temp; sum+=year_salei; for(int i=0;i3;i+) coutyearsi的销售量为:year_saleiendl; cout这三年的总销售量为:sumendl; return 0; /ex.5.7#include #include using namespace std;struct car string name; int year;int main() coutnum).get(); car* ps=new carnum; for(int i=0;inum;+i) coutcar #i+1:n; coutplease enter the make: ; getline(cin,); coutpsi.year).get(); couthere is your collection:n; for(int i=0;inum;+i) coutpsi.year endl; delete ps; return 0; /ex.5.8#include #include int main() using namespace std; char word20; int sum=0; coutword; while(strcmp(word,done) sum+; cinword; coutyou entered a total of sum words.n; return 0;/ex.5.9#include #include int main() using namespace std; string word; int sum=0; coutword; while(word!=done) sum+; cinword; coutyou entered a total of sum words.n; return 0;/ex.5.10#include int main() using namespace std; coutnum;for(int i=0;i1;j-) cout.;for(int k=0;k=i;+k) cout*;coutendl;return 0;以下是大学体验英语综合教程2(第三版)课后翻译答案unit11. 任何年满18岁的人都有资格投票。(be eligible to, vote)answer:anyone over the age of 18 is eligible to vote.2. 每学期开学前,这些奖学金的申请表格就会由学校发给每一个学生。(apply for, scholarship)answer:a form to apply for these scholarships is sent by the university to every student before the start of every semester.3. 遵照医生的建议,我决定戒烟。(on the advice of)answer:on the advice of my doctor, i decided to give up smoking.4. 公园位于县城的正中央。(be located in)answer:the park is located right in the center of town.5. 这所大学提供了我们所需的所有材料和设备。(facilities)answer:the university provides all the materials and facilities we desire.1. 他们花了多年的

温馨提示

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

评论

0/150

提交评论