1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. double Sum2(double x, double y)
  6. {
  7. double sum = 0;
  8.  
  9. if (x + y < 0.0000000001)
  10. {
  11. cout << "x, y 的调和数为无穷大;\n";
  12. system("pause");
  13. exit(0);
  14. }
  15. sum = 2.0*x*y / (x + y);
  16. return sum;
  17. }
  18.  
  19. void test7_1()
  20. {
  21. double x;
  22. double y;
  23. double sum = 0;
  24.  
  25. while ((cin>>x>>y) && x != 0 && y!=0)
  26. {
  27. sum = Sum2(x, y);
  28. cout << "x,y的调和为: " << sum << endl;
  29. }
  30. }
  31.  
  32. /************************************************************************/
  33. /* 2 */
  34. /************************************************************************/
  35. const int SIZE = 10;
  36. int GetScores(double scores [],int *size)
  37. {
  38. cout << "Enter the scores( press q to quit).\n";
  39. int i = 0;
  40. for (i = 0; i < 10 && cin >> scores[i]; i++)
  41. {
  42. if (scores[i]<0)
  43. {
  44. cout << "score below zero, bad input.\n";
  45. cout << "Input terminated.\n";
  46. return -1;
  47. }
  48. }
  49.  
  50. *size = i;
  51. return 0;
  52. }
  53.  
  54. void Show(const double * scores, int size)
  55. {
  56. int i = 0;
  57. for (i = 0; i < size; i++)
  58. {
  59. cout << scores[i] << " ";
  60. }
  61. cout << endl;
  62. }
  63.  
  64. double Average(const double *scores, int size)
  65. {
  66. double sum = 0;
  67.  
  68. if (size == 0)
  69. {
  70. return 0;
  71. }
  72. for (int i = 0; i < size; i ++)
  73. {
  74. sum += scores[i];
  75. }
  76.  
  77. return sum/size;
  78. }
  79. void test7_2()
  80. {
  81. int size = 0;
  82. double scores[SIZE] = { 0 };
  83.  
  84. int ret = 0;
  85. ret = GetScores(scores, &size);
  86. while (ret != 0)
  87. {
  88. cout << "Please input again.\n";
  89. ret = GetScores(scores, &size);
  90. }
  91.  
  92. Show(scores, size);
  93. double avg = Average(scores, size);
  94. cout << "平均成绩为:" << avg << endl;
  95.  
  96. }
  97.  
  98. /************************************************************************/
  99. /* 3 */
  100. /************************************************************************/
  101. typedef struct Box
  102. {
  103. char maker[40];
  104. float height;
  105. float width;
  106. float length;
  107. float volume;
  108. }box;
  109.  
  110. void ShowBox(box b1)
  111. {
  112. cout << "Showing the box......\n";
  113. cout << b1.maker << endl;
  114. cout << b1.height << endl;
  115. cout << b1.width << endl;
  116. cout << b1.length << endl;
  117. cout << b1.volume << endl;
  118. }
  119.  
  120. void GetVol(box * b1)
  121. {
  122. b1->volume = b1->height *b1->length*b1->width;
  123. }
  124. void test7_3()
  125. {
  126. box b1 = { "Cat", 10, 2, 3, 0 };
  127. ShowBox(b1);
  128. GetVol(&b1);
  129. ShowBox(b1);
  130. }
  131.  
  132. /************************************************************************/
  133. /* 4 */
  134. /************************************************************************/
  135.  
  136. void test7_4()
  137. {
  138.  
  139. }
  140. /************************************************************************/
  141. /* 5 */
  142. /************************************************************************/
  143. long GetFactorial(int n)
  144. {
  145. if (n == 0 || n == 1)
  146. {
  147. return 1;
  148. }
  149. else
  150. return n*GetFactorial(n - 1);
  151.  
  152. }
  153. void test7_5()
  154. {
  155. int n = 0;
  156. cin >> n;
  157. if (n < 0)
  158. {
  159. cout << "bad input.\nprogram terminated.\n";
  160. return;
  161. }
  162.  
  163. cout << n << "! = " << GetFactorial(n)<<endl;
  164.  
  165. }
  166.  
  167. void test7_6()
  168. {
  169.  
  170. }
  171.  
  172. /************************************************************************/
  173. /* 7 */
  174. /************************************************************************/
  175. const int Max = 5;
  176. double * full_array(double ar[], int limit)
  177. {
  178. double *ar_end = ar;
  179. double tmp = 0;
  180. for (int i = 0; i < limit; i++, ar_end++)
  181. {
  182. cout << "Enter value #" << (i + 1) << ": ";
  183. cin >> tmp;
  184.  
  185. if (!cin)
  186. {
  187. cin.clear();
  188. while (cin.get()!='\n')
  189. {
  190. continue;
  191. }
  192.  
  193. cout << "Bad input;input process terminated.\n";
  194. break;
  195. }
  196. else if (tmp < 0)
  197. {
  198. break;
  199. }
  200. *ar_end = tmp;
  201. }
  202.  
  203. return --ar_end;
  204. }
  205.  
  206. void show_array(double ar[], const double *ar_end)
  207. {
  208. double *pt = ar;
  209. for (int i = 0; pt <= ar_end; pt++, i++)
  210. {
  211. cout << "Property #" << (i + 1) << ": $";
  212. cout << *pt << endl;
  213. }
  214. }
  215.  
  216. void revalue(double r, double *ar, double *ar_end)
  217. {
  218. double *pt = ar;
  219. for (; pt <= ar_end ; pt++)
  220. {
  221. *pt *= r;
  222.  
  223. }
  224. }
  225.  
  226. void test7_7()
  227. {
  228. double properties[Max];
  229. double *ar_end = NULL;
  230. ar_end = full_array(properties, Max);
  231. show_array(properties,ar_end);
  232. if (ar_end - properties > 0)
  233. {
  234. cout << "Enter revaluation factor: ";
  235. double factor;
  236. while (!(cin >> factor))
  237. {
  238. cin.clear();
  239. while (cin.get()!= '\n')
  240. {
  241. continue;
  242. }
  243. cout << "Bad input. input process terminated.";
  244. }
  245. revalue(factor, properties, ar_end);
  246. show_array(properties, ar_end);
  247. }
  248. cout << "Done.\n";
  249. cin.get();
  250. cin.get();
  251. }
  252.  
  253. /************************************************************************/
  254. /* 8 */
  255. /************************************************************************/
  256. const int Seasons = 4;
  257. const char * Snames[Seasons] = { "Spring", "Summer", "Fall", "Winter" };
  258. void fill(double expenses[], int Seasons)
  259. {
  260. for (int i = 0; i < Seasons; i++)
  261. {
  262. cout << "Enter " << Snames[i] << " expense: ";
  263. cin >> expenses[i];
  264. }
  265. }
  266.  
  267. void show(double expense[], int Seasons)
  268. {
  269. double total = 0;
  270. cout << "EXPENSE\n";
  271. for (int i = 0; i < Seasons; i++)
  272. {
  273. cout << Snames[i] << ": $" << expense[i] << endl;
  274. total += expense[i];
  275. }
  276.  
  277. cout << "Total Expense: $" << total << endl;
  278.  
  279. }
  280. void test7_8()
  281. {
  282. double expense[Seasons] = { 0 };
  283. fill(expense, Seasons);
  284. show(expense, Seasons);
  285. }
  286.  
  287. /************************************************************************/
  288. /* 9 */
  289. /************************************************************************/
  290. const int Size = 30;
  291. typedef struct Student{
  292.  
  293. char fullname[Size];
  294. char hobby[Size];
  295. int ooplevel;
  296.  
  297. }student;
  298.  
  299. int getInfo(student pa[], int n)
  300. {
  301. int i = 0;
  302. for (i = 0; i < n; i++)
  303. {
  304. cout << "Student #" << i + 1 << ":\n";
  305. cout << "Enter name: ";
  306. cin >> pa[i].fullname;
  307. if (!cin)
  308. {
  309. cin.clear();
  310. while (cin.get()!= '\n')
  311. {
  312. continue;
  313. }
  314. cout << "bad input. input process terminated.\n";
  315. }
  316.  
  317. cout << "Enter hobby:";
  318. cin >> pa[i].hobby;
  319. cout << "Enter ooplevel:";
  320. cin >> pa[i].ooplevel;
  321. }
  322.  
  323. return i;
  324. }
  325.  
  326. void display1(student st)
  327. {
  328. cout << "Student info:\n";
  329. cout << "name: " << st.fullname << endl;
  330. cout << "hobby: " << st.hobby << endl;
  331. cout << "ooplevel" << st.ooplevel << endl;
  332. }
  333.  
  334. void display2(student *ps)
  335. {
  336. cout << "Student info:\n";
  337. cout << "name: " << ps->fullname << endl;
  338. cout << "hobby: " << ps->hobby << endl;
  339. cout << "ooplevel" << ps->ooplevel << endl;
  340. }
  341.  
  342. void display3(const student pa[], int n)
  343. {
  344. int i = 0;
  345. for (; i < n; i++)
  346. {
  347. cout << "Student #" << i + 1<< "info:"<< endl;
  348. cout << "name: " << pa[i].fullname << endl;
  349. cout << "hobby: " << pa[i].hobby << endl;
  350. cout << "ooplevel" << (pa+i)->ooplevel << endl;
  351. }
  352. }
  353.  
  354. void test7_9()
  355. {
  356. cout << "Enter class size: ";
  357. int class_size;
  358. cin >> class_size;
  359.  
  360. while (cin.get()!= '\n')
  361. {
  362. continue;
  363. }
  364.  
  365. student *ptr_stu = new student[class_size];
  366. int entered = getInfo(ptr_stu, class_size);
  367. for (int i = 0; i < entered; i++)
  368. {
  369. display1(ptr_stu[i]);
  370. display2(ptr_stu + i);
  371. }
  372.  
  373. display3(ptr_stu, entered);
  374. delete [] ptr_stu;
  375. cout << "Done.\n";
  376. }
  377. double add(double x, double y)
  378. {
  379. return x + y;
  380. }
  381.  
  382. double sub(double x, double y)
  383. {
  384. return x - y;
  385. }
  386.  
  387. double mul(double x, double y)
  388. {
  389. return x*y;
  390. }
  391.  
  392. void test7_10()
  393. {
  394. double(*calculate[3])(double, double);
  395.  
  396. calculate[0] = add;
  397. calculate[1] = sub;
  398. calculate[2] = mul;
  399. for (int i = 0; i < 3; i++)
  400. {
  401. cout << calculate[i] << ": "<<calculate[i](5, 2) << endl;
  402. }
  403. }
  404. int main()
  405. {
  406. test7_10();
  407. system("pause");
  408. return 0;
  409. }

  

C++ Primer Pluse_7_课后题的更多相关文章

  1. C++ Primer Pluse_8_课后题

    #include <iostream> #include <string> #include<cstring> using namespace std; void ...

  2. C++ Primer Pluse_6_课后题

    #include <iostream> #include <cctype> #include <array> #include <string> #in ...

  3. 玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)

    前文 的最后给出了玉伯的一道课后题,今天我们来讲讲这题的思路. 题目是这样的: Number.MAX_VALUE + 1 == Number.MAX_VALUE; Number.MAX_VALUE + ...

  4. 算法(JAVA)----两道小小课后题

    LZ最近翻了翻JAVA版的数据结构与算法,无聊之下将书中的课后题一一给做了一遍,在此给出书中课后题的答案(非标准答案,是LZ的答案,猿友们可以贡献出自己更快的算法). 1.编写一个程序解决选择问题.令 ...

  5. 课后题2.87&2.86

    课后题2.86&2.87 单纯就是想加点分第十章的题目都被做过了就做下第二章的,正好复习一下前面学的知识,第二章给我剩下的题目也不多了,我就挑了这个题目. 2.86 考虑一个基于IEEE浮点格 ...

  6. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...

  7. 学习参考《零基础入门学习Python》电子书PDF+笔记+课后题及答案

    国内编写的关于python入门的书,初学者可以看看. 参考: <零基础入门学习Python>电子书PDF+笔记+课后题及答案 Python3入门必备; 小甲鱼手把手教授Python; 包含 ...

  8. 学习《零基础入门学习Python》电子书PDF+笔记+课后题及答案

    初学python入门建议学习<零基础入门学习Python>.适合新手入门,很简单很易懂.前一半将语法,后一半讲了实际的应用. Python3入门必备,小甲鱼手把手教授Python,包含电子 ...

  9. Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解

    Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...

随机推荐

  1. JSP 中 pageEncoding 和 charset 区别以及中文乱码解决方案

    一.JSP 中 pageEndcodeing 和 charset 的作用 <%@ page contentType="text/html;charset=GB2312"%&g ...

  2. sqlSQL2008如何创建定时作业

    SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...

  3. 【Linux程序设计】之进程间的通信

    这个系列的博客贴的都是我大二的时候学习Linux系统高级编程时的一些实验程序,都挺简单的. 实验题目:Linux环境下的进程间通信 实验目的:熟悉进程通信中信号概念及信号处理:掌握进程间的管道通信编程 ...

  4. ccc 多点触控2

    经过不断的思考发现,如果是两个sprite都添加触控的时候,往往直接成单点触控, 但是如果是两个node的时候在node上面点击就会变成多点触控的形式 cc.Class({ extends: cc.C ...

  5. 【转】敏捷开发 Scrum 总结

    转:http://www.open-open.com/lib/view/open1330413325514.html 最近把之前学习 Scrum 的资料整理为一篇文档,在接下来的团队和项目开发中,根据 ...

  6. 一些有用的HTML5 pattern属性

    最近在做手机页面时,遇到数字输入的键盘的问题,之前的做法只是一刀切的使用 type="tel",不过一直觉得九宫格的电话号码键盘上的英文字母太碍事了.于是想要尝试其它的实现方案,最 ...

  7. 使用Adobe Edge Inspect在各种设备中轻松测试同一页面

    有过移动网站开发经历的开发者都知道,在各种设备中测试同一页面是一项非常繁琐的工作.现在,我们可以使用Adobe Edge Inspect来简化这一工作.如果使用Edge Inspect,可以在各种设备 ...

  8. NOIp 2014 #5 解方程 Label:数论?

    题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, m ] 内的整数解(n 和m 均为正整数) 输入输出格式 输入格式: 输入文件名为equation .i ...

  9. 转:jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  10. 10秒钟安装 Vim编辑器,5分钟浏览常用命令 2015.10.25

    首先我想说,vim与vi的命令几乎相同,,所以学习编辑命令时很轻松,排除扩展相关,以及自动补全等配置的使用在外30秒钟安装 Vim编辑器,5分钟浏览常用命令环境:虚拟机Ubuntu:安装vim并浏览命 ...