【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 ...
随机推荐
- HttpURLconnection的介绍
一,HttpURLconnection的介绍 在Android开发中网络请求是最常用的操作之一, Android SDK中对HTTP(超文本传输协议)也提供了很好的支持,这里包括两种接口: 1.标准J ...
- linux 常用命令(二)——(centos6.8-centos7)防火墙的启动、关闭
centos 6.8 [centos6.5]: 查看chkconfig列表里面是否有iptables的服务: chkconfig | grep iptables 查看防火墙状态: service ip ...
- go语言初始化结构体指针
go语言初始化结构体指针 head:=&ListNode{} 或者 head:=new(ListNode)
- Shell脚本逐行读取文本内容并拆分,根据条件筛选文件
时间:2018-11-13 整理:byzqy 需求: 最近帮朋友写了一段脚本,他的需求是根据一份产品清单,去服务器上捞取对应产品编号的测试Log,数量大概有9000~10000条左右.文本内容大致如下 ...
- 重启网络服务 network 出现问题
2021-08-24 地址冲突了,因为想要设置成静态 ip 一直都不对,情急之下就将本地 ip 设置成了虚拟机的 ip,故出现此错误 后将地址改掉,重启网络服务就没有错误了 一开始我设置的虚拟网卡 n ...
- MySQL之连接查询和子查询
多表连接的基本语法 多表连接,就是将几张表拼接为一张表,然后进行查询 select 字段1, 字段2, ... from 表1 {inner|lift|right} join 表2 on 连接条件; ...
- SpringBoot2.x+mybatis plus3.x集成Activit7版本
最近在写一个开源项目ruoyi-vue-pro,暂时负责Activiti7工作流的搭建,接这个任务一个原因,是比较好奇Activiti7版本与先前的5.6版本究竟有什么区别,因为先前在工作当中,最开始 ...
- HDD成都站:HMS Core 6.0带来新可能 多元服务驱动产品商业成功
9月10日,由华为开发者联盟主办的HDD(Huawei Developer Day)于成都举行.活动中,华为HMS Core各领域专家重点解读了HMS Core 6.0为开发者带来的多项全新能力,及生 ...
- ClientValidationFunction
CustomValidator.ClientValidationFunction 属性 获取或设置用于验证的自定义客户端脚本函数的名称. 命名空间: System.Web.UI.WebContro ...
- .Net 如何修改 HttpHeaders 中的 Content-Disposition
最近在看一些.Net5的内容,于是就想将之前Spring写的一个项目迁移到.Net上来看看. 不得不说.Net这几年发展的确实挺好的,超快的启动速度,极佳的性能让它一点不比Java差,但确实在国内生态 ...