1. 编写三个文件
注意:记得运用C风格字符串函数:strcpy()、strcmp(),以及如何比较字符串和空字符串。
// golf.h -- for pe9-1.cpp
const int Len = 40;
struct golf {
char fullname[Len];
int handicap;
};
// 重载形式1:输入并设置结构golf中的参数,无返回值,非交互形式
void setgolf(golf& g, const char* name, int hc);
// 重载形式2:输入并设置结构golf中的参数,一个返回值:int,交互形式
// 若输入名字,返回1;否则返回0
int setgolf(golf& g);
// 重新设置handicap值
void handicap(golf& g, int hc);
// 展示golf结构中的参数
void showgolf(const golf& g);
#include <iostream>
#include <cstring>
#include "golf.h"
// 重载形式1:输入并设置结构golf中的参数,无返回值,非交互形式
void setgolf(golf& g, const char* name, int hc) {
strcpy(g.fullname, name); // 复制字符串函数
g.handicap = hc;
}
// 重载形式2:输入并设置结构golf中的参数,一个返回值:int,交互形式
// 若输入名字,返回1;否则返回0
int setgolf(golf& g) {
using std::cin;
using std::cout;
using std::endl;
cout << "Enter the name: ";
cin.getline(g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
return 0;
cout << "Enter the handicap: ";
cin >> g.handicap;
cin.get();
return 1;
}
// 重新设置handicap值
void handicap(golf& g, int hc) {
g.handicap = hc;
}
// 展示golf结构中的参数
void showgolf(const golf& g) {
using std::cout;
using std::endl;
cout << "The name is " << g.fullname;
cout << " and the candicap is " << g.handicap << endl;
}
// 9-1
#include <iostream>
#include "golf.h"
const int N