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("", 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 }
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 »
编程练习答案——第9章