1. 1 void ch5_1() {
  2. 2 using namespace std;
  3. 3 int small, big, sum{0};
  4. 4 cout << "enter small and big: " << endl;
  5. 5 cout << "small: "; cin >> small;
  6. 6 cout << "big: "; cin >> big;
  7. 7 for (int i = small; i <= big; ++ i)
  8. 8 sum += i;
  9. 9 cout << "sum between " << small << "and " << big << ": " << sum << endl;
  10. 10 }
  11. 11
  12. 12 void ch5_2() {
  13. 13 using namespace std;
  14. 14 const int ArrSize = 101;
  15. 15 array<long double, ArrSize> factorials;
  16. 16 factorials[0] = factorials[1] = 1;
  17. 17 for (int i = 2; i < ArrSize; ++ i)
  18. 18 factorials[i] = factorials[i-1] * i;
  19. 19 for (int i = 0; i < ArrSize; ++ i)
  20. 20 cout << i << "! = " << factorials[i] << endl;
  21. 21 }
  22. 22
  23. 23 void ch5_3() {
  24. 24 using namespace std;
  25. 25 int num, sum{0};
  26. 26 cout << "enter a num (quit by 0): ";
  27. 27 cin >> num;
  28. 28 while (num) {
  29. 29 sum += num;
  30. 30 cout << "sum == " << sum << ", next num: ";
  31. 31 cin >> num;
  32. 32 }
  33. 33 }
  34. 34
  35. 35 void ch5_4() {
  36. 36 using namespace std;
  37. 37 double D_money{100}, C_money{100}, sin_factor{0.1}, mul_factor{0.05};
  38. 38 int year_count = 1;
  39. 39 while (C_money <= D_money) {
  40. 40 D_money += 100 * sin_factor;
  41. 41 C_money += C_money * mul_factor;
  42. 42 cout << "year " << year_count << ": C: " << C_money << " D:" << D_money << endl;
  43. 43 ++ year_count;
  44. 44 }
  45. 45 }
  46. 46
  47. 47 void ch5_5() {
  48. 48 using namespace std;
  49. 49 const char * MONTHSNAME[12] = {
  50. 50 "January", "February", "March",
  51. 51 "April", "May", "June",
  52. 52 "July", "August", "September",
  53. 53 "October", "November", "December"
  54. 54 };
  55. 55 int sales[12]{0}, sum{0};
  56. 56 for (int i = 0; i < 12; ++ i) {
  57. 57 cout << "enter sales in " << MONTHSNAME[i] << ":";
  58. 58 cin >> sales[i];
  59. 59 sum += sales[i];
  60. 60 }
  61. 61 cout << "all sales: " << sum;
  62. 62 }
  63. 63
  64. 64 void ch5_6() {
  65. 65 using namespace std;
  66. 66 const char * MONTHSNAME[12] = {
  67. 67 "January", "February", "March",
  68. 68 "April", "May", "June",
  69. 69 "July", "August", "September",
  70. 70 "October", "November", "December"
  71. 71 };
  72. 72 int sales[3][12]{0}, sum{0};
  73. 73 for (int i = 0; i < 3; ++ i) {
  74. 74 cout << "enter sales in year " << i + 1 << endl;
  75. 75 for (int j = 0; j < 12; ++ j) {
  76. 76 cout << MONTHSNAME[j] << ":";
  77. 77 cin >> sales[i][j];
  78. 78 sum += sales[i][j];
  79. 79 }
  80. 80 }
  81. 81 cout << "all sales: " << sum;
  82. 82 }
  83. 83
  84. 84 void ch5_7() {
  85. 85 using namespace std;
  86. 86 struct Car{
  87. 87 string brand;
  88. 88 unsigned int year;
  89. 89 };
  90. 90 Car * car_arr;
  91. 91 unsigned int num{0};
  92. 92 cout << "how many cars do you wish to catalog? ";
  93. 93 cin >> num; cin.get();
  94. 94 car_arr = new Car[num];
  95. 95 for (int i = 0; i < num; ++ i) {
  96. 96 cout << "Car# " << i + 1 << ":" << endl;
  97. 97 cout << "enter brand: ";
  98. 98 getline(cin, car_arr[i].brand);
  99. 99 cout << "enter year: ";
  100. 100 cin >> car_arr[i].year; cin.get();
  101. 101 }
  102. 102 cout << "here's your collection: " << endl;
  103. 103 for (int i = 0; i < num ;++ i)
  104. 104 cout << car_arr[i].year << " " << car_arr[i].brand << endl;
  105. 105 }
  106. 106
  107. 107 void ch5_8() {
  108. 108 using namespace std;
  109. 109 char word[100];
  110. 110 unsigned int count{0};
  111. 111 cout << "Enter words (to stop, type the word done):" << endl;
  112. 112 cin >> word;
  113. 113 while (strcmp(word, "done") != 0) {
  114. 114 ++ count;
  115. 115 cin.get();
  116. 116 cin >> word;
  117. 117 }
  118. 118 cout << "You entered a total of " << count << " words.";
  119. 119 }
  120. 120
  121. 121 void ch5_9() {
  122. 122 using namespace std;
  123. 123 string word;
  124. 124 unsigned int count{0};
  125. 125 cout << "Enter words (to stop, type the word done):" << endl;
  126. 126 cin >> word;
  127. 127 while (word != "done") {
  128. 128 ++ count;
  129. 129 cin.get();
  130. 130 cin >> word;
  131. 131 }
  132. 132 cout << "You entered a total of " << count << " words.";
  133. 133 }
  134. 134
  135. 135 void ch5_10() {
  136. 136 using namespace std;
  137. 137 unsigned int rows{0};
  138. 138 cout << "enter number of rows: ";
  139. 139 cin >> rows;
  140. 140 for (int i = 0; i < rows; ++ i) {
  141. 141 for (int j = 0; j < rows - i - 1; ++ j)
  142. 142 cout << '.';
  143. 143 for (int k = 0; k < i + 1; ++ k)
  144. 144 cout << '*';
  145. 145 cout << endl;
  146. 146 }
  147. 147 }

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

  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】编程练习答案——第7章

    1 double ch7_1_harmonicaverage(double a, double b) { 2 return 2 / (1 / a + 1 / b); 3 } 4 5 void ch7_ ...

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

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

  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. 1、Spark简介(Python版)

    此文为个人学习笔记如需系统学习请访问http://dblab.xmu.edu.cn/blog/1709-2/ Spark具有如下几个主要特点:  运行速度快    Spark使用先进的DAG(Dir ...

  2. JDBC基础篇(MYSQL)——使用statement执行DQL语句(select)

    注意:其中的JdbcUtil是我自定义的连接工具类:代码例子链接: package day02_statement; import java.sql.Connection; import java.s ...

  3. Win7安装 Mysql 5.7.22客户端

    根据自己的操作系统下载对应的32位或64位的压缩包: http://dev.mysql.com/downloads/mysql/ 官网下载 选择Windows对应的版本下载 不注册直接下载 安装步骤 ...

  4. Qt5之坐标系统

    窗口坐标为逻辑坐标,是基于视口坐标系的. 视口坐标为物理坐标,是基于绘图设备坐标系的 窗口坐标始终以视口坐标为最终目标进行映射: QPainter::setWindow 修改了窗口位置和大小(左上角重 ...

  5. Kubernetes 持久化数据存储 StorageClass

    文章链接 PV 和 PVC 模式要先创建好 PV,然后再定义好 PVC 进行一对一的绑定.那么如果遇到大集群,也一一的创建吗?这样来说维护成本很高,工作量大.这个时候就有了 Kubernetes 提供 ...

  6. shell循环语句while

    格式1: while 条件 do 执行命令 done 格式2: while 条件;do 命令 done 例子: while [ 1 -eq 1 ];do echo "这一步需要先修改/dat ...

  7. PyQt5 笔记

    一.简介 pyqt5做为Python的一个模块,它有620多个类和6000个函数和方法.这是一个跨平台的工具包,它可以运行在所有主要的操作系统,包括UNIX,Windows,Mac OS.pyqt5是 ...

  8. NOIP模拟16:「Star Way To Heaven·God Knows·Loost My Music」

    T1:Star Way To Heaven 基本思路:   最小生成树.   假如我们将上边界与下边界看作一个点,然后从上边界经过星星向下边界连边,会发现,他会形成一条线将整个矩形分为左右两个部分. ...

  9. Nginx:无处不在的Nginx的八个应用场景与配置

    --- 阅读时间约 15 分钟 --- Nginx概述 众所周知,互联网已经离不开  WEB服务器  ,技术领域中  WEB服务器  不止  Nginx  一个,其他还有很多如  Apache  . ...

  10. 硕盟SM-T54|type-c转接头HDMI+VGA+USB3.0+PD3.0四合一多功能扩展坞接口功能说明

    硕盟SM-T54是一款 TYPE C转HDMI+VGA+USB3.0+PD3.0四合一多功能扩展坞,支持四口同时使用,您可以将含有USB 3.1协议的电脑主机,通过此产品连接到具有HDMI或VGA的显 ...