【C++ Primer Plus】编程练习答案——第9章
1 // chapter09_golf.h
2
3 #ifndef LEARN_CPP_CHAPTER09_GOLF_H
4 #define LEARN_CPP_CHAPTER09_GOLF_H
5
6 #include <cstring>
7 #include <iostream>
8
9 const int Len = 40;
10 struct golf {
11 char fullname[Len];
12 int handicap;
13 };
14
15 void setgolf(golf &, const char *, int);
16 int setgolf(golf &);
17 void handicap(golf &, int);
18 void showgolf(const golf &);
19
20
21
22 #endif //LEARN_CPP_CHAPTER09_GOLF_H
23
24
25
26
27
28 // chapter09_golf.cpp
29
30 #include "chapter09_golf.h"
31
32 void setgolf(golf & g, const char * name, int hc) {
33 strcpy(g.fullname, name);
34 g.handicap = hc;
35 }
36
37 int setgolf(golf & g) {
38 using namespace std;
39 cout << "enter name: ";
40 cin.getline(g.fullname, Len);
41 cout << "enter hc: ";
42 cin >> g.handicap;cin.get();
43 if (strcmp("\0", g.fullname) == 0)
44 return 0;
45 return 1;
46 }
47
48 void handicap(golf & g, int hc) {
49 g.handicap = hc;
50 }
51
52 void showgolf(const golf & g) {
53 using namespace std;
54 cout << "name: " << g.fullname << endl;
55 cout << "hc: " << g.handicap << endl;
56 }
57
58
59
60
61
62 // run
63
64 void ch9_1() {
65 using namespace std;
66 golf arr_golf[3];
67 cout << "enter golf: ";
68 int i;
69 for (i = 0; i < 3; ++ i) {
70 if (setgolf(arr_golf[i]) == 0)
71 break;
72 }
73 cout << "enter done" << endl;
74 for (int j = 0; j < i; ++ j)
75 showgolf(arr_golf[j]);
76 }
1 void ch9_2_strcount(std::string & str) {
2 using namespace std;
3 static int total = 0;
4 int count = 0;
5 cout << "\"" << str << "\" contains ";
6 count = str.length();
7 total += count;
8 cout << count << " chars" << endl;
9 cout << total << " chars total" << endl;
10 }
11
12 void ch9_2() {
13 using namespace std;
14 string str;
15 while (true) {
16 cout << "enter a line: " << endl;
17 getline(cin, str);
18 if (str == "")
19 break;
20 ch9_2_strcount(str);
21 }
22 cout << "Bye" << endl;
23 }
1 void ch9_3() {
2 using namespace std;
3 int buffer[1000];
4 chaff * arr_chaff = new (buffer)chaff[2];
5 strcpy(arr_chaff[0].dross, "#1"); arr_chaff[0].slag = 123;
6 strcpy(arr_chaff[1].dross, "#2"); arr_chaff[1].slag = 321;
7 for (int i = 0; i < 2; ++ i)
8 cout << "dross: " << arr_chaff[i].dross << " slag: " << arr_chaff[i].slag << endl;
9 }
1 // chapter 09_sales.h
2
3 #ifndef LEARN_CPP_CHAPTER09_SALES_H
4 #define LEARN_CPP_CHAPTER09_SALES_H
5
6 #include <iostream>
7
8 namespace SALES
9 {
10 const int QUARTERS = 4;
11 struct Sales {
12 double sales[QUARTERS];
13 double average;
14 double max;
15 double min;
16 };
17 void setSales(Sales &, const double *, int);
18 void setSales(Sales &);
19 void showSales(const Sales &);
20 }
21
22
23
24 #endif //LEARN_CPP_CHAPTER09_SALES_H
25
26
27
28
29
30
31
32 // chapter09_sales.cpp
33
34 #include "chapter09_sales.h"
35
36 void SALES::setSales(SALES::Sales & s, const double * ar, int n) {
37 double sum = 0;
38 int p_max = 0, p_min = 0;
39 for (int i = 0; i < n; ++ i) {
40 s.sales[i] = ar[i];
41 sum += ar[i];
42 if (ar[i] > ar[p_max])
43 p_max = i;
44 if (ar[i] < ar[p_min])
45 p_min = i;
46 }
47 s.average = sum / n;
48 s.max = ar[p_max];
49 s.min = ar[p_min];
50 }
51
52 void SALES::setSales(SALES::Sales & s) {
53 using namespace std;
54 double sum = 0;
55 int p_max = 0, p_min = 0, n = 0;
56 for (int i = 0; i < SALES::QUARTERS; ++ i) {
57 if (!(cin >> s.sales[i]))
58 break;
59 cin.get();
60 ++ n;
61 sum += s.sales[i];
62 if (s.sales[i] > s.sales[p_max])
63 p_max = i;
64 if (s.sales[i] < s.sales[p_min])
65 p_min = i;
66 }
67 s.average = sum / n;
68 s.max = s.sales[p_max];
69 s.min = s.sales[p_min];
70 }
71
72 void SALES::showSales(const SALES::Sales & s) {
73 using namespace std;
74 cout << "sales: ";
75 for (int i = 0; i < SALES::QUARTERS; ++ i)
76 if (s.sales[i])
77 cout << s.sales[i];
78 cout << endl;
79 cout << "average: " << s.average << endl;
80 cout << "max: " << s.max << endl;
81 cout << "min: " << s.min << endl;
82 }
83
84
85
86
87 // run
88
89 void ch9_4() {
90 using namespace std;
91 SALES::Sales a;
92 cout << "set a: " << endl;
93 SALES::setSales(a);
94 double s[4]{1.1,2.2,3.3,4.4};
95 SALES::Sales b;
96 SALES::setSales(b,s,4);
97 cout << "a: " << endl;
98 SALES::showSales(a);
99 cout << "b: " << endl;
100 SALES::showSales(b);
101 }
【C++ Primer Plus】编程练习答案——第9章的更多相关文章
- 【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】编程练习答案——第8章
1 void ch8_1_print(const std::string & str, int n = 0 ) { 2 using namespace std; 3 static int fl ...
- 【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_ ...
- 【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 ...
随机推荐
- 十九:JDBC操作事务
二.MySQL数据库中操作事务命令 2.1.开启事务(start transaction) 使用"start transaction"开启MySQL数据库的事务,如下所示:
- C++ 子类调用父类的方法,静态方法的调用
#include <iostream> class A { public: A(); ~ A(); virtualvoid say() { std::cout << &quo ...
- [SWMM]模型子汇水区划分的几种方法
子汇水区的划分是SWMM模型建模的主要步骤之一,划分的好坏对结果精度有比较大的影响.概括来讲,子汇水区的划分有以下几种思路: (1)根据管网走向.建筑物和街道分布,直接人工划分子汇水区.这个方法适用于 ...
- CentOS 6.x 系统中安装原生 Hadoop 2
2020年整理博客发现原文地址已经失效,推荐学习地址厦门大学数据库实验室 本教程适合于在 CentOS 6.x 系统中安装原生 Hadoop 2,适用于Hadoop 2.7.1, Hadoop 2.6 ...
- git《一》
org.eclipse.jgit.api.errors.TransportException: https://gitee.com/wbweb/springboot_vue.git: Authenti ...
- 详细解读go语言中的map
Map map底层是由哈希表实现的 Go使用链地址法来解决键冲突. map本质上是一个指针,指向hmap 这里的buckets就是桶,bmap 每一个bucket最多可以放8个键值对,但是为了让内存排 ...
- introduction-to-64-bit-assembly
introduction-to-64-bit-assembly NASM - The Netwide Assembler x86-64 下函数调用及栈帧原理 汇编语言基本概念简介 mycode
- Python网络爬虫——京东商城商品列表
Python_网络爬虫--京东商城商品列表 最近在拓展自己知识面,想学习一下其他的编程语言,处于多方的考虑最终选择了Python,Python从发布之初就以庞大的用户集群占据了编程的一席之地,pyth ...
- MySQL——优化
MySQL数据库优化: 1.优化角度 安全: 数据可持续性 性能: 数据的高性能访问 2.优化范围(优化顺序---->) (1)存储.主机和操作系统: 主机架构稳定性 I/O规划及配置 swap ...
- python3 用multiprocessing模块传递多个参数
from datetime import datetime from time import sleep import numpy as np import multiprocessing # fro ...