收藏 分享(赏)

C++ primer plus (第6版) 中文版编程练习答案.doc

上传人:tangtianxu1 文档编号:3086358 上传时间:2018-10-03 格式:DOC 页数:135 大小:126.91KB
下载 相关 举报
C++  primer plus (第6版) 中文版编程练习答案.doc_第1页
第1页 / 共135页
C++  primer plus (第6版) 中文版编程练习答案.doc_第2页
第2页 / 共135页
C++  primer plus (第6版) 中文版编程练习答案.doc_第3页
第3页 / 共135页
C++  primer plus (第6版) 中文版编程练习答案.doc_第4页
第4页 / 共135页
C++  primer plus (第6版) 中文版编程练习答案.doc_第5页
第5页 / 共135页
点击查看更多>>
资源描述

1、第二章:开始学习C+/ex2.1-display your name and address#includeint main(void)using namespace std;coutdouble fur2yd(double);int main()using namespace std;coutfur;coutvoid mice();void see();using namespace std;int main()mice();mice();see();see();return 0;void mice()coutint main()using namespace std;coutage;int

2、 month;month=age*12;coutdouble C2F(double);int main()using namespace std;coutC;double F;F=C2F(C);coutdouble convert(double);/函数原型int main()using namespace std;coutlight_years;double astro_units;astro_units=convert(light_years);coutvoid show();main()using namespace std;show();return 0;void show()usin

3、g namespace std;int h,m;couth;coutm;coutconst 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;/取余coutconst int inch_per_feet=12;const double meter_per_inch=0.0254;const double po

4、und_per_kilogram=2.2;int main()using namespace std;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;coutconst int minutes_per_degree=60;const int seconds_per_minute

5、=60;int main()using namespace std;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;coutconst int hours_per_day=24;const int minutes_per_hour=60;const int seconds_per_minute=60;i

6、nt 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;coutint main()using na

7、mespace std;coutworld_population;coutUS_population;double percentage;percentage=(double)US_population/world_population*100;coutint main()using namespace std;coutm_distance;coutm_gasoline;coutk_distance;coutk_gasoline;coutint main()using namespace std;coutEuro_style;coutint main()using namespace std;

8、coutUS_style;coutconst int Asize=20;using namespace std;struct student/定义结构描述char firstnameAsize;char lastnameAsize;char grade;int age;void display(student);/函数原型放在结构描述后int main()coutlcg.grade;coutlcg.age;display(lcg);return 0;void display(student name)cout#includeint main()using namespace std;strin

9、g name,dessert;coutsbumpc();/修改后的break; ex4.3 输入其名和姓,并组合显示#include#includeconst int Asize=20;int main()using namespace std;char fnameAsize;char lnameAsize;char fullname2*Asize+1;cout#include const int Asize = 20;int main()using namespace std;char firstnameAsize;char lastname50;cout #includeint main(

10、)using namespace std;string fname,lname,attach,fullname;coutconst int Asize=20;struct CandyBarchar brandAsize;double weight;int calory;int main()using namespace std;CandyBar snack=“Mocha Munch“,2.3,350;coutconst int Asize=20;struct CandyBarchar brandAsize;double weight;int calory;int main()using nam

11、espace std;CandyBar snack3=“Mocha Munch“,2.3,350,“XuFuJi“,1.1,300,“Alps“,0.4,100;for(int i=0;i#includeconst int Size=20;struct pizza/声明结构char companySize;double diameter;double weight;int main()using namespace std;pizza pie;/创建一个名为pie 的结构变量coutpie.diameter;coutpie.weight;cout#includeconst int Size=2

12、0;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;coutdiametercompanyweight#includeusing namespace std;struct CandyBarstring brand;double weight;int cal

13、ory;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;iint main()using namespace std;const int Size = 3;int successSize;

14、cout success0success1success2;cout#include int main()using namespace std;arrayad=0;cout ad0ad1ad2;coutint main()using namespace std;coutnum1num2;int sum=0;for(int temp=num1;temp#includeint main()using namespace std;arrayad=0;ad1=ad0=1L;for(int i=2;i#include using namespace std;int main()array multip

15、ly;multiply0 = multiply1 = 1LL;for (int i = 2; i int main()using namespace std;coutnum)coutint main()using namespace std;double sum1,sum2;sum1=sum2=0.0;int year=0;while(sum2using namespace std;int main()double Daphne = 100.0;double Cleo = 100.0;int year = 0;while (Cleo const int MONTHS = 12;const ch

16、ar*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;isalesi;sum+=salesi;coutconst int MONTHS = 12;const char*monthsMONTHS=“January“,“February“,“March“,“April“,“Ma

17、y“,“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;isalesij;temp+=salesij;year_salei=temp;sum+=year_salei;for(int i=0;iusing namespace std;const int Years = 3, Months =

18、 12;const char* monthsMonths =“January“,“February“,“March“,“April“,“May“,“June“,“July“,“August“,“September“,“October“,“November“,“December“;int main()int saleYearsMonths = 0;for (int i = 0; i saleij;sum += saleij; cout #include using namespace std;struct carstring name;int year;int main()coutnum).ge

19、t();car* ps=new carnum;for(int i=0;ipsi.year).get();cout#include using namespace std;struct carstring maker;int year;int main()int number;cout number;car* a = new carnumber;for (int i = 0; i ai.year;cout using namespace std;struct carchar maker20;int year;int main()int number;cout number;car* a = ne

20、w carnumber;for (int i = 0; i ai.year;cout #include int main()using namespace std;char word20;int sum=0;coutword;while(strcmp(word,“done“)sum+;cinword;cout#include int main()using namespace std;string word;int sum=0;coutword;while(word!=“done“)sum+;cinword;coutint main()using namespace std;coutnum;f

21、or(int i=0;i1;j-)cout#include int main()using namespace std;char ch;cin.get(ch);while(ch!=)if(isdigit(ch)cin.get(ch);elseif(islower(ch)ch=toupper(ch);elsech=tolower(ch);cout#include using namespace std;int main()char ch;cout #includeint main()using namespace std;double sum=0,average=0;double num10;i

22、nt i=0,total=0;double temp;while(cintempcoutusing namespace std;const int Num = 10;int main()double donationNum;int i = 0;int count =0;double sum = 0.0;cout donationi)sum += donationi+; if(i= 0)cout average)+count; cout #include#includeint main()using namespace std;double sum=0,average=0;arrayad=0;i

23、nt i=0,total=0;double temp;while(cintempcoutint main()using namespace std;coutch;while(ch!=cswitch(ch)case c:coutusing namespace std;void show();int main()show();char choice;while (cin choice)switch(choice) case c :cout const int strsize=20;struct bopchar fullnamestrsize;char titlestrsize;char bopna

24、mestrsize;int preference;int main()using namespace std;coutchiusing namespace std;const int strsize = 30;struct bopchar fullnamestrsize;char titlestrsize;char bopnamestrsize;int preference;void show();int main()bop A5 =“Wimp Macho“, “Teacher“, “HAHA“, 0,“Raki Rhodes“, “Junior Programmer“, “LIAR“, 1,

25、“Celia“, “engineer“, “MIPS“, 2,“Hoppy Hipman“, “Analyst Trainee“, “WAHU“, 1,“Pat Hand“, “Student“, “LOOPY“, 2;cout choice;while (choice != q)switch(choice) case a :cout choice; cout int main()using namespace std;double income,revenue;coutincomeint main()double income,tax;cout income struct patronstr

26、ing name;double money;int main()int num,temp=0;coutnum;cin.get();patron *ps=new patronnum;for(int i=0;ipsi.money;cin.get();cout10000)cout#include using namespace std;struct charitystring name;double money;int main()int number;int count = 0;cout number;charity *pt = new charitynumber;for (int i = 0;

27、i pti.money;if(pti.money 10000)count+; if(count = 0)cout 10000)“;elsecout 10000)cout #include int main()using namespace std;int vowel=0,consonant=0,other=0;char word15;coutword)if(isalpha(word0)if(word0=qelse if(word0=a|word0=i|word0=u|word0=e|word0=o)+vowel;else+consonant;else+other;cout#include us

28、ing namespace std;int main()char word20;int vow = 0, consonant = 0, other =0;cout word)if (isalpha(word0)if(word0 = a | word0 = e | word0 = i| word0= o | word0 = u |word0 = A | word0 = E | word0 = I | word0 =O | word0 = U)vow+;else if(word0 = q elseconsonant+; elseother+; cout #include #include int

29、main()using namespace std;char ch;int sum=0;ifstream inFile;inFile.open(“abc.txt“);if(!inFile.is_open()coutch;while(inFile.good()+sum;inFilech;if(inFile.eof()cout#include #include using namespace std;const int Size = 20;int main()char filenameSize;ifstream infile;cout a;while (infile.good()+count;in

30、file a; if (infile.eof()cout #include #include struct memberchar name20;double donation;int main()using namespace std;int num,count1=0,count2=0;ifstream fin;char file20;coutnum;fin.get();member *pd=new membernum;for(int i=0;ipdi.donation;fin.get();cout=10000)cout#include #include #include using name

31、space std;struct charitystring name;double money;int main()string filename;ifstream infile;cout number;charity *pt = new charitynumber;for (int i = 0; i pti.money;if(pti.money 10000)count+; if(count = 0)cout 10000)“;elsecout 10000)cout double t_av(double x, double y);int main() using namespace std;d

32、ouble x, y;double result;cout x y) cout using namespace std;void average(double, double);int main()double A, B;cout A B)if (A = 0 | B = 0)break;elseaverage(A, B);cout const int MAX = 10;using namespace std;int fill_ar(double ar, int limit);void show_ar(const double ar, int n);double average(const do

33、uble ar, int n);int main() double scoresMAX;int size = fill_ar(scores, MAX);show_ar(scores, size);if (size 0)cout temp;if (!cin) cin.clear();while (cin.get() != n)continue;cout using namespace std;double score10;int input(double , int);void average(double , int);void show(const double, int);int main()int size = input(score, 10);if (size 0)show(score, size);average(score, size); cout a;if (!cin)cin.clear();while (cin.get() != n)continue;cout struct box char maker40;

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

当前位置:首页 > 网络科技 > C/C++资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报