【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 ...
 
随机推荐
- C++ template模板编程
			
模板是C++泛型编程的基础,一个模板就是一个创建类或者函数的蓝图或者公式.当使用一个vector这样的泛型类型,我们提供足够的信息,就可以将蓝图转换成特定的类或者函数. 假设我们编写一个函数来比较两个 ...
 - node后台生成echarts图表
			
1 //2,生成图片数据 2 var option = { 3 tooltip: { 4 trigger: 'item', 5 formatter: "{a} <br/>{b} ...
 - WPF 中的 经典的ModelView 通知页面更新 UI
			
view model ------------------------------------------------------------------------------ using HPCo ...
 - js对url进行编码和解码
			
编码 只有 0-9[a-Z] $ - _ . + ! * ' ( ) , 以及某些保留字,才能不经过编码直接用于 URL. 例如:搜索的中文关键字,复制网址之后再粘贴就会发现该URL已经被转码. 1. ...
 - ProjectEuler 008题
			
题目: The four adjacent digits in the 1000-digit number that have the greatest product are 9 9 8 9 = 5 ...
 - git tag的用法及意义
			
git tag 介绍 命令是用来给当前项目状态(在某次commit后)打标签的,目的是便于以后将项目状态回滚到当时打标签的状态.可以把它与虚拟机的snapshot(快照)进行类比. 回想当时在看< ...
 - VMware ESXi 7.0 U2 SLIC & Unlocker Intel NUC 专用镜像 202109 更新
			
2021.08.31 更新:集成 "vmkusb-nic-fling" 和 "nvme-community",现在只有一个镜像. 2021.06.16 更新:集 ...
 - MySQL读写IO的操作过程解析
			
数据库作为存储系统,所有业务访问数据的操作都会转化为底层数据库系统的IO行为(缓存系统也可以当做是key-value的数据库),本文主要介绍访问MySQL数据库的IO流程以及IO相关的参数. 一.My ...
 - JSP(Java Server Pages)内置对象
			
request对象 (1)访问请求参数 处理HTTP请求中的各项参数.在这些参数中,最常用的就是获取访问请求参数.当通过超链接的形式发送请求时,可以为该请求传递参数,这可以通过在超链接的后面加上问好& ...
 - vue 引用高德地图
			
vue 引用地图之傻瓜式教程(复制粘贴即可用) npm 下载 npm install vue-amap --save main.js 代码 import AMap from 'vue-amap'; V ...