1. 1 double ch7_1_harmonicaverage(double a, double b) {
  2. 2 return 2 / (1 / a + 1 / b);
  3. 3 }
  4. 4
  5. 5 void ch7_1() {
  6. 6 using namespace std;
  7. 7 double a{0}, b{0};
  8. 8 while (true) {
  9. 9 cout << "a: ";
  10. 10 cin >> a;
  11. 11 cout << "b: ";
  12. 12 cin >> b;
  13. 13 if (!(a&&b))
  14. 14 break;
  15. 15 cout << "harmonic average: " << ch7_1_harmonicaverage(a, b) << endl;
  16. 16 }
  17. 17 }
  18. 18
  19. 19 void ch7_2_in(double * arr, int & len, int max) {
  20. 20 using namespace std;
  21. 21 for (int i = 0; i < max; ++ i)
  22. 22 if (!(cin >> arr[i])) {
  23. 23 len = i;
  24. 24 break;
  25. 25 }
  26. 26 len = max;
  27. 27 }
  28. 28
  29. 29 void ch7_2_show(const double * arr, int len) {
  30. 30 using namespace std;
  31. 31 for (int i = 0; i < len; ++ i)
  32. 32 cout << arr[i] << " ";
  33. 33 cout << endl;
  34. 34 }
  35. 35
  36. 36 double ch7_2_mean(const double * arr, int len) {
  37. 37 double sum{0};
  38. 38 for (int i = 0; i < len; ++ i)
  39. 39 sum += arr[i];
  40. 40 return sum / len;
  41. 41 }
  42. 42
  43. 43 void ch7_2() {
  44. 44 using namespace std;
  45. 45 const int ArSize = 10;
  46. 46 double scores[ArSize]{0};
  47. 47 int len{0};
  48. 48 cout << "enter scores (max 10, nan to quit)" << endl;
  49. 49 ch7_2_in(scores, len, ArSize);
  50. 50 cout << "scores: ";
  51. 51 ch7_2_show(scores, len);
  52. 52 cout << "mean: " << ch7_2_mean(scores, len) << endl;
  53. 53 }
  54. 54
  55. 55 void ch7_3_show(box b) {
  56. 56 using namespace std;
  57. 57 cout << "box: maker: " << b.maker << " height: " << b.height
  58. 58 << " width: " << b.width << " length: " << b.length
  59. 59 << " volume: " << b.volume << endl;
  60. 60 }
  61. 61
  62. 62 void ch7_3_setvolume(box * b) {
  63. 63 using namespace std;
  64. 64 b -> volume = b -> height * b -> width * b -> length;
  65. 65 cout << "box volume seted" << endl;
  66. 66 }
  67. 67
  68. 68 void ch7_3() {
  69. 69 using namespace std;
  70. 70 box b{"xushun", 2, 3, 4};
  71. 71 ch7_3_show(b);
  72. 72 ch7_3_setvolume(&b);
  73. 73 ch7_3_show(b);
  74. 74 }
  75. 75
  76. 76 long double ch7_4_probability(unsigned int numbers, unsigned int picks) {
  77. 77 long double result = 1.0;
  78. 78 for (int i = numbers, j = picks; j; -- i, -- j)
  79. 79 result = result * i /j;
  80. 80 return 1 / result;
  81. 81 }
  82. 82
  83. 83 void ch7_4() {
  84. 84 using namespace std;
  85. 85 long double r = ch7_4_probability(47, 5) * ch7_4_probability(27, 1);
  86. 86 cout << "r = " << r << endl;
  87. 87 }
  88. 88
  89. 89 unsigned long long ch7_5_factorial(unsigned long long num) {
  90. 90 if (num == 0)
  91. 91 return 1;
  92. 92 else
  93. 93 return num * ch7_5_factorial(num - 1);
  94. 94 }
  95. 95
  96. 96 void ch7_5() {
  97. 97 using namespace std;
  98. 98 unsigned long long num{0};
  99. 99 cout << "enter num to get factorial (nan to quit)" << endl;
  100. 100 while (true) {
  101. 101 cout << "num: ";
  102. 102 if (!(cin >> num))
  103. 103 break;
  104. 104 cout << "factorial: " << ch7_5_factorial(num) << endl;
  105. 105 }
  106. 106 }
  107. 107
  108. 108 unsigned int ch7_6_fillarray(double * arr, unsigned int max) {
  109. 109 using namespace std;
  110. 110 int i{0};
  111. 111 for (i = 0; i < max; ++ i) {
  112. 112 cout << "#" << i << ": ";
  113. 113 if (!(cin >> arr[i]))
  114. 114 break;
  115. 115 }
  116. 116 return i;
  117. 117 }
  118. 118
  119. 119 void ch7_6_showarray(const double * arr, unsigned int len) {
  120. 120 using namespace std;
  121. 121 cout << "array: ";
  122. 122 for (int i = 0; i < len; ++ i)
  123. 123 cout << arr[i] << " ";
  124. 124 cout << endl;
  125. 125 }
  126. 126
  127. 127 void ch7_6_reversearray(double * arr, unsigned int len) {
  128. 128 double temp{0};
  129. 129 for (int i = 0; i < len / 2; ++ i) {
  130. 130 temp = arr[i];
  131. 131 arr[i] = arr[len - i - 1];
  132. 132 arr[len - i - 1] = temp;
  133. 133 }
  134. 134 }
  135. 135
  136. 136 void ch7_6() {
  137. 137 using namespace std;
  138. 138 const unsigned int max = 100;
  139. 139 double arr[max]{0};
  140. 140 unsigned int len{0};
  141. 141 cout << "enter array (nan to quit)" << endl;
  142. 142 len = ch7_6_fillarray(arr, max);
  143. 143 cout << "array" << endl;
  144. 144 ch7_6_showarray(arr, len);
  145. 145 cout << "reverse array" << endl;
  146. 146 ch7_6_reversearray(arr, len);
  147. 147 ch7_6_showarray(arr, len);
  148. 148 cout << "reverse array except 1 and len" << endl;
  149. 149 ch7_6_reversearray(arr + 1, len - 2);
  150. 150 ch7_6_showarray(arr, len);
  151. 151 }
  152. 152
  153. 153 double * ch7_7_fillarray(double * arr, unsigned int max) {
  154. 154 using namespace std;
  155. 155 for (int i = 0; i < max; ++ i) {
  156. 156 cout << "#" << i << ": ";
  157. 157 if (!(cin >> arr[i]))
  158. 158 return arr + i;
  159. 159 }
  160. 160 return arr + max - 1;
  161. 161 }
  162. 162
  163. 163 void ch7_7_showarray(const double * arr, double * end) {
  164. 164 using namespace std;
  165. 165 cout << "array: ";
  166. 166 while (arr != end) {
  167. 167 cout << *arr << " ";
  168. 168 ++ arr;
  169. 169 }
  170. 170 cout << endl;
  171. 171 }
  172. 172
  173. 173 void ch7_7_revalue(double * arr, double * end, double factor) {
  174. 174 while (arr != end) {
  175. 175 *arr *= factor;
  176. 176 ++ arr;
  177. 177 }
  178. 178 }
  179. 179
  180. 180 void ch7_7() {
  181. 181 using namespace std;
  182. 182 const unsigned int max = 100;
  183. 183 double arr[100]{0};
  184. 184 double * end = arr;
  185. 185 double factor{1};
  186. 186 cout << "enter array (nan to quit)" << endl;
  187. 187 end = ch7_7_fillarray(arr, max);
  188. 188 cout << "array" << endl;
  189. 189 ch7_7_showarray(arr, end);
  190. 190 cout << "enter factor: ";
  191. 191 cin.clear();cin.get();
  192. 192 while (!(cin >> factor)) {
  193. 193 cin.clear();
  194. 194 while (cin.get() != '\n')
  195. 195 continue;
  196. 196 cout << "factor must be a number: ";
  197. 197 }
  198. 198 ch7_7_revalue(arr, end ,factor);
  199. 199 ch7_7_showarray(arr, end);
  200. 200 }
  201. 201
  202. 202 void ch7_8_fill(double * arr, unsigned int n) {
  203. 203 using namespace std;
  204. 204 cout << "enter expenses for 4 seasons" << endl;
  205. 205 for (int i = 0; i < n; ++ i) {
  206. 206 while (!(cin >> arr[i])) {
  207. 207 cin.clear();
  208. 208 while (cin.get() != '\n')
  209. 209 continue;
  210. 210 cout << "must number: ";
  211. 211 }
  212. 212 }
  213. 213 }
  214. 214
  215. 215 void ch7_8_show(const double * arr, unsigned int n) {
  216. 216 using namespace std;
  217. 217 for (int i = 0; i < n; ++ i)
  218. 218 cout << arr[i] << " ";
  219. 219 cout << endl;
  220. 220 }
  221. 221
  222. 222 void ch7_8() {
  223. 223 using namespace std;
  224. 224 const unsigned int snum = 4;
  225. 225 const char * SEASONSNAME[] = {"Spring", "Summer", "Fall", "Winter"};
  226. 226 double expenses[snum]{0};
  227. 227 ch7_8_fill(expenses, snum);
  228. 228 ch7_8_show(expenses, snum);
  229. 229 }
  230. 230
  231. 231 void ch7_9() {
  232. 232 using namespace std;
  233. 233 // 题目太长不看 o.o
  234. 234 }
  235. 235
  236. 236 double ch7_10_calculate(double x, double y, double (* pf)(double, double)) {
  237. 237 return pf(x, y);
  238. 238 }
  239. 239
  240. 240 double ch7_10_add(double x, double y) {
  241. 241 return x + y;
  242. 242 }
  243. 243
  244. 244 double ch7_10_mul(double x, double y) {
  245. 245 return x * y;
  246. 246 }
  247. 247
  248. 248 void ch7_10() {
  249. 249 using namespace std;
  250. 250 double x{0}, y{0};
  251. 251 cout << "enter two double" << endl;
  252. 252 cout << "x: ";
  253. 253 while (!(cin >> x)) {
  254. 254 cin.clear();
  255. 255 while (cin.get() != '\n')
  256. 256 continue;
  257. 257 cout << "must a double: ";
  258. 258 }
  259. 259 cout << "y: ";
  260. 260 while (!(cin >> y)) {
  261. 261 cin.clear();
  262. 262 while (cin.get() != '\n')
  263. 263 continue;
  264. 264 cout << "must a double: ";
  265. 265 }
  266. 266 cout << "x + y == " << ch7_10_calculate(x, y, ch7_10_add) << endl;
  267. 267 cout << "x * y == " << ch7_10_calculate(x, y, ch7_10_mul) << endl;
  268. 268 }

【C++ Primer Plus】编程练习答案——第7章的更多相关文章

  1. 【C++ Primer Plus】编程练习答案——第12章

    1 // chapter12_1_cow.h 2 3 4 #ifndef LEARN_CPP_CHAPTER12_1_COW_H 5 #define LEARN_CPP_CHAPTER12_1_COW ...

  2. 【C++ Primer Plus】编程练习答案——第11章 (待更新)

    最近开学,事情较多,过两天更新...

  3. 【C++ Primer Plus】编程练习答案——第10章

    1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...

  4. 【C++ Primer Plus】编程练习答案——第9章

    1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 5 ...

  5. 【C++ Primer Plus】编程练习答案——第8章

    1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...

  6. 【C++ Primer Plus】编程练习答案——第6章

    1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...

  7. 【C++ Primer Plus】编程练习答案——第5章

    1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...

  8. 【C++ Primer Plus】编程练习答案——第4章

    1 void ch4_1() { 2 using namespace std; 3 string fname, lname; 4 char grade; 5 unsigned int age; 6 c ...

  9. 【C++ Primer Plus】编程练习答案——第3章

    1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...

随机推荐

  1. jsoup的Element类

    一.简介 该类是Node的直接子类,同样实现了可克隆接口.类声明:public class Element extends Node 它表示由一个标签名,多个属性和子节点组成的html元素.从这个元素 ...

  2. WPF 中的 经典的ModelView 通知页面更新 UI

    view model ------------------------------------------------------------------------------ using HPCo ...

  3. Flink中的算子操作

    一.Connect DataStream,DataStream ->  ConnectedStream,连接两个保持他们类型的数据流,两个数据流被Connect之后,只是被放在了同一个流中,内部 ...

  4. 笔记本+ubuntu18.04 关闭触摸板touchpad

    方法1: Settings -> Devices -> Mouse&Touchpad -> Touchpad OFF 方法2: 终端运行如下命令 touchpad off:  ...

  5. BUUCTF-[CISCN2019 华北赛区 Day1 Web5]CyberPunk

    BUUCTF-[CISCN2019 华北赛区 Day1 Web5]CyberPunk 看题 看源码有提示?file=? 文件包含漏洞,可以利用这个漏洞读取源码. 分析 index.php?file=p ...

  6. 存储系统管理(二)——Linux系统的swap分区、磁盘加密、磁盘阵列

    磁盘驱动器上的空间 , 用作当前未使用部分内存的溢出.这样 , 系统就能在主内存中留出空间用于储存当前正在处理的数据 , 并在系统面临主内存空间不足的风险时提供应急溢出. swap分区的建立: fdi ...

  7. vue 引入 tcplayer,并且实现视频点播,腾讯点播

    这篇文章仅用于上传到 腾讯的视频点播,上传到腾讯视频请看上一篇文章,话不多说,直接上代码 <template> <div> <video :id="tcPlay ...

  8. ICCV2021 | MicroNet:以极低的 FLOPs 改进图像识别

    ​前言:这篇论文旨在以极低的计算成本解决性能大幅下降的问题.提出了微分解卷积,将卷积矩阵分解为低秩矩阵,将稀疏连接整合到卷积中.提出了一个新的动态激活函数-- Dynamic Shift Max,通过 ...

  9. AQS快速入门

    一.模板方法模式 父子类多态,父类中用一个方法调用执行所有所需要的方法: 父类: 子类: 主线程执行时候调用父类的模板方法: 二.AQS思想 sync都是独占锁,lock显示锁也是,只有读写锁是共享锁 ...

  10. jdbcTemplate快速入门

    一. c3p0和dbcp区别 二.导包 hibernate通过映射自动创建表: 三.代码实现