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章的更多相关文章

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

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

  5. 【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_ ...

  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. Spring详解(十)------spring 环境切换

    软件开发过程一般涉及"开发 -> 测试 -> 部署上线"多个阶段,每个阶段的环境的配置参数会有不同,如数据源,文件路径等.为避免每次切换环境时都要进行参数配置等繁琐的操 ...

  2. LeetCoded第25题题解--K个一组翻转链表--java--链表

    链表 单链表:链表中的每个元素实际上是一个单独的对象,而所有对象都通过每个元素的引用字段链接在一起. 双链表:与单链表不同的是,双链表的每个节点都含有两个引用字段. 链表优点 灵活分配内存空间 能在O ...

  3. SpringBoot笔记(5)

    一.Web原生组件注入 1.使用Servlet API @ServletComponentScan(basePackages = "com.atguigu.admin") :指定原 ...

  4. Linux中MySQL的安装以及卸载

    一.MySQL MySQL是一种开放源代码的关系型数据库管理系统,开发者为瑞典MySQL AB公司.在2008年1月16号被Sun公司收购.而2009年,SUN又被Oracle收购.目前 MySQL被 ...

  5. JDBC简介及JDBC编写步骤及常见API

    JDBC : Java Database Connectivity,Java数据库连接.SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC. JDBC就像一座桥 ...

  6. kubernetes部署一个应用程序

    文章原文 部署 nginx Deployment 如果你已经完成了Kubernetes的搭建,那我跟我一块来部署第一个应用程序吧.没有完成 Kubernetes 集群搭建的,请参考文档 使用 kube ...

  7. 被面试官问懵:TCP 四次挥手收到乱序的 FIN 包会如何处理?

    摘要:收到个读者的问题,他在面试的时候,被搞懵了,因为面试官问了他这么一个网络问题. 本文分享自华为云社区<TCP 四次挥手收到乱序的 FIN 包会如何处理?>,作者:小林coding . ...

  8. HZ游记

    HZ 游记 Day -1 收拾东西,准备出发. 话说这几天一直比较懒,也没什么心情和效率学习,颓废好几天了,希望到衡水以后能感觉好点. 不知道衡水有没有妹子 非常想看看衡水的样子,但是又害怕封闭式教学 ...

  9. IP头详解

    IP包头长度(Header Length):长度4比特.这个字段的作用是为了描述IP包头的长度,因为在IP包头中有变长的可选部分.该部分占4个bit位,单位为32bit(4个字节),即本区域值= IP ...

  10. freeswitch的网关配置

    vim  /usr/local/freeswitch/conf/sip_profiles/external/weihu1.xml 1 <!-- 点对点式 --> 2 <!-- 3 & ...