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 }