【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_1() {
- 6 using namespace std;
- 7 double a{0}, b{0};
- 8 while (true) {
- 9 cout << "a: ";
- 10 cin >> a;
- 11 cout << "b: ";
- 12 cin >> b;
- 13 if (!(a&&b))
- 14 break;
- 15 cout << "harmonic average: " << ch7_1_harmonicaverage(a, b) << endl;
- 16 }
- 17 }
- 18
- 19 void ch7_2_in(double * arr, int & len, int max) {
- 20 using namespace std;
- 21 for (int i = 0; i < max; ++ i)
- 22 if (!(cin >> arr[i])) {
- 23 len = i;
- 24 break;
- 25 }
- 26 len = max;
- 27 }
- 28
- 29 void ch7_2_show(const double * arr, int len) {
- 30 using namespace std;
- 31 for (int i = 0; i < len; ++ i)
- 32 cout << arr[i] << " ";
- 33 cout << endl;
- 34 }
- 35
- 36 double ch7_2_mean(const double * arr, int len) {
- 37 double sum{0};
- 38 for (int i = 0; i < len; ++ i)
- 39 sum += arr[i];
- 40 return sum / len;
- 41 }
- 42
- 43 void ch7_2() {
- 44 using namespace std;
- 45 const int ArSize = 10;
- 46 double scores[ArSize]{0};
- 47 int len{0};
- 48 cout << "enter scores (max 10, nan to quit)" << endl;
- 49 ch7_2_in(scores, len, ArSize);
- 50 cout << "scores: ";
- 51 ch7_2_show(scores, len);
- 52 cout << "mean: " << ch7_2_mean(scores, len) << endl;
- 53 }
- 54
- 55 void ch7_3_show(box b) {
- 56 using namespace std;
- 57 cout << "box: maker: " << b.maker << " height: " << b.height
- 58 << " width: " << b.width << " length: " << b.length
- 59 << " volume: " << b.volume << endl;
- 60 }
- 61
- 62 void ch7_3_setvolume(box * b) {
- 63 using namespace std;
- 64 b -> volume = b -> height * b -> width * b -> length;
- 65 cout << "box volume seted" << endl;
- 66 }
- 67
- 68 void ch7_3() {
- 69 using namespace std;
- 70 box b{"xushun", 2, 3, 4};
- 71 ch7_3_show(b);
- 72 ch7_3_setvolume(&b);
- 73 ch7_3_show(b);
- 74 }
- 75
- 76 long double ch7_4_probability(unsigned int numbers, unsigned int picks) {
- 77 long double result = 1.0;
- 78 for (int i = numbers, j = picks; j; -- i, -- j)
- 79 result = result * i /j;
- 80 return 1 / result;
- 81 }
- 82
- 83 void ch7_4() {
- 84 using namespace std;
- 85 long double r = ch7_4_probability(47, 5) * ch7_4_probability(27, 1);
- 86 cout << "r = " << r << endl;
- 87 }
- 88
- 89 unsigned long long ch7_5_factorial(unsigned long long num) {
- 90 if (num == 0)
- 91 return 1;
- 92 else
- 93 return num * ch7_5_factorial(num - 1);
- 94 }
- 95
- 96 void ch7_5() {
- 97 using namespace std;
- 98 unsigned long long num{0};
- 99 cout << "enter num to get factorial (nan to quit)" << endl;
- 100 while (true) {
- 101 cout << "num: ";
- 102 if (!(cin >> num))
- 103 break;
- 104 cout << "factorial: " << ch7_5_factorial(num) << endl;
- 105 }
- 106 }
- 107
- 108 unsigned int ch7_6_fillarray(double * arr, unsigned int max) {
- 109 using namespace std;
- 110 int i{0};
- 111 for (i = 0; i < max; ++ i) {
- 112 cout << "#" << i << ": ";
- 113 if (!(cin >> arr[i]))
- 114 break;
- 115 }
- 116 return i;
- 117 }
- 118
- 119 void ch7_6_showarray(const double * arr, unsigned int len) {
- 120 using namespace std;
- 121 cout << "array: ";
- 122 for (int i = 0; i < len; ++ i)
- 123 cout << arr[i] << " ";
- 124 cout << endl;
- 125 }
- 126
- 127 void ch7_6_reversearray(double * arr, unsigned int len) {
- 128 double temp{0};
- 129 for (int i = 0; i < len / 2; ++ i) {
- 130 temp = arr[i];
- 131 arr[i] = arr[len - i - 1];
- 132 arr[len - i - 1] = temp;
- 133 }
- 134 }
- 135
- 136 void ch7_6() {
- 137 using namespace std;
- 138 const unsigned int max = 100;
- 139 double arr[max]{0};
- 140 unsigned int len{0};
- 141 cout << "enter array (nan to quit)" << endl;
- 142 len = ch7_6_fillarray(arr, max);
- 143 cout << "array" << endl;
- 144 ch7_6_showarray(arr, len);
- 145 cout << "reverse array" << endl;
- 146 ch7_6_reversearray(arr, len);
- 147 ch7_6_showarray(arr, len);
- 148 cout << "reverse array except 1 and len" << endl;
- 149 ch7_6_reversearray(arr + 1, len - 2);
- 150 ch7_6_showarray(arr, len);
- 151 }
- 152
- 153 double * ch7_7_fillarray(double * arr, unsigned int max) {
- 154 using namespace std;
- 155 for (int i = 0; i < max; ++ i) {
- 156 cout << "#" << i << ": ";
- 157 if (!(cin >> arr[i]))
- 158 return arr + i;
- 159 }
- 160 return arr + max - 1;
- 161 }
- 162
- 163 void ch7_7_showarray(const double * arr, double * end) {
- 164 using namespace std;
- 165 cout << "array: ";
- 166 while (arr != end) {
- 167 cout << *arr << " ";
- 168 ++ arr;
- 169 }
- 170 cout << endl;
- 171 }
- 172
- 173 void ch7_7_revalue(double * arr, double * end, double factor) {
- 174 while (arr != end) {
- 175 *arr *= factor;
- 176 ++ arr;
- 177 }
- 178 }
- 179
- 180 void ch7_7() {
- 181 using namespace std;
- 182 const unsigned int max = 100;
- 183 double arr[100]{0};
- 184 double * end = arr;
- 185 double factor{1};
- 186 cout << "enter array (nan to quit)" << endl;
- 187 end = ch7_7_fillarray(arr, max);
- 188 cout << "array" << endl;
- 189 ch7_7_showarray(arr, end);
- 190 cout << "enter factor: ";
- 191 cin.clear();cin.get();
- 192 while (!(cin >> factor)) {
- 193 cin.clear();
- 194 while (cin.get() != '\n')
- 195 continue;
- 196 cout << "factor must be a number: ";
- 197 }
- 198 ch7_7_revalue(arr, end ,factor);
- 199 ch7_7_showarray(arr, end);
- 200 }
- 201
- 202 void ch7_8_fill(double * arr, unsigned int n) {
- 203 using namespace std;
- 204 cout << "enter expenses for 4 seasons" << endl;
- 205 for (int i = 0; i < n; ++ i) {
- 206 while (!(cin >> arr[i])) {
- 207 cin.clear();
- 208 while (cin.get() != '\n')
- 209 continue;
- 210 cout << "must number: ";
- 211 }
- 212 }
- 213 }
- 214
- 215 void ch7_8_show(const double * arr, unsigned int n) {
- 216 using namespace std;
- 217 for (int i = 0; i < n; ++ i)
- 218 cout << arr[i] << " ";
- 219 cout << endl;
- 220 }
- 221
- 222 void ch7_8() {
- 223 using namespace std;
- 224 const unsigned int snum = 4;
- 225 const char * SEASONSNAME[] = {"Spring", "Summer", "Fall", "Winter"};
- 226 double expenses[snum]{0};
- 227 ch7_8_fill(expenses, snum);
- 228 ch7_8_show(expenses, snum);
- 229 }
- 230
- 231 void ch7_9() {
- 232 using namespace std;
- 233 // 题目太长不看 o.o
- 234 }
- 235
- 236 double ch7_10_calculate(double x, double y, double (* pf)(double, double)) {
- 237 return pf(x, y);
- 238 }
- 239
- 240 double ch7_10_add(double x, double y) {
- 241 return x + y;
- 242 }
- 243
- 244 double ch7_10_mul(double x, double y) {
- 245 return x * y;
- 246 }
- 247
- 248 void ch7_10() {
- 249 using namespace std;
- 250 double x{0}, y{0};
- 251 cout << "enter two double" << endl;
- 252 cout << "x: ";
- 253 while (!(cin >> x)) {
- 254 cin.clear();
- 255 while (cin.get() != '\n')
- 256 continue;
- 257 cout << "must a double: ";
- 258 }
- 259 cout << "y: ";
- 260 while (!(cin >> y)) {
- 261 cin.clear();
- 262 while (cin.get() != '\n')
- 263 continue;
- 264 cout << "must a double: ";
- 265 }
- 266 cout << "x + y == " << ch7_10_calculate(x, y, ch7_10_add) << endl;
- 267 cout << "x * y == " << ch7_10_calculate(x, y, ch7_10_mul) << endl;
- 268 }
【C++ Primer Plus】编程练习答案——第7章的更多相关文章
- 【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 ...
- 【C++ Primer Plus】编程练习答案——第11章 (待更新)
最近开学,事情较多,过两天更新...
- 【C++ Primer Plus】编程练习答案——第10章
1 // chapter10_1_account.h 2 3 #ifndef LEARN_CPP_CHAPTER10_1_ACCOUNT_H 4 #define LEARN_CPP_CHAPTER10 ...
- 【C++ Primer Plus】编程练习答案——第9章
1 // chapter09_golf.h 2 3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H 4 #define LEARN_CPP_CHAPTER09_GOLF_H 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 ...
- 【C++ Primer Plus】编程练习答案——第6章
1 void ch6_1() { 2 using namespace std; 3 char ch; 4 while ((ch = cin.get()) != '@') { 5 if (isdigit ...
- 【C++ Primer Plus】编程练习答案——第5章
1 void ch5_1() { 2 using namespace std; 3 int small, big, sum{0}; 4 cout << "enter small ...
- 【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 ...
- 【C++ Primer Plus】编程练习答案——第3章
1 void ch3_1() { 2 using namespace std; 3 unsigned int factor = 12; 4 unsigned int inch, feet; 5 cou ...
随机推荐
- linux 端口80占用问题
主要是搭建一次ghost博客网站改成80端口无法启动提示被占用. 提示:80端口被占用,启动失败. netstat -ano 或者 netstat -apn | grep 80 没有发现占用80端口的 ...
- Go-内置函数之append、recover用法
package main import "fmt" import "time" func test() { defer func() { if err := r ...
- uni-app中websocket的使用 断开重连、心跳机制
前言 最近关于H5和APP的开发中使用到了webSocket,由于web/app有时候会出现网络不稳定或者服务端主动断开,这时候导致消息推送不了的情况,需要客户端进行重连.查阅资料后发现了一个心跳机制 ...
- MongoDB(11)- 查询数组
插入测试数据 db.inventory.insertMany([ { item: "journal", qty: 25, tags: ["blank", &qu ...
- Vue Abp vNext获取当前登录用户
系统默认提供了获取当前用户的api方法 https://localhost:44364/api/identity/my-profile 手工实现方法:abp后台获取当前用户需要在AppService应 ...
- C# List集合类常用操作 (一)
所有操作基于以下类 class Employees { public int Id { get; set; } public string Name { get; set; } public stri ...
- Input 只能输入数字,数字和字母等的正则表达式
JS只能输入数字,数字和字母等的正则表达式 1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace( ...
- Lambda函数接口和方法构造器应用
函数式接口 什么是函数式接口? 在java中'有且仅有一个抽象方法的接口',就称为函数式接口. 可以通过Lambda表达式来创建该接口的对象.(若Lambda表达式抛出一个受检异常,那么该异常需要在目 ...
- 获取发布版SHA1和调试版SHA1
总结 调试版: 常见问题 | 高德地图API (amap.com) 发布版: 首先需要生成签名 Android Studio生成签名文件,自动签名,以及获取SHA1和MD5值_donkor_的博客-C ...
- Java on Visual Studio Code的更新 – 2021年8月
Nick Senior Program Manager, Developer Division at Microsoft 大家好,欢迎来到 8 月版的 Visual Studio Code Java ...