main.cpp
#include"syx.h"
using namespace std;
const int MAX=3;
golf*dg=new golf[MAX];
golf ann;
int main()
{
int i=0;
for(i;i<MAX&&setgolf(dg[i]);i++);
setgolf(ann,"Ann Birdfree",24);
for(int j=0;j<i;j++)
showgolf(dg[i]);
showgolf(ann);
delay(100);
return 0;
}
syx.h
#ifndef SYX_H_
#define SYX_H_
#include <iostream>
#include<ctime>
#include<cstring>
void delay(float);
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
// non-interactive version;
// function sets golf structure to provided name,handicap
// using values passed as arguments to the function
void setgolf (golf & g, const char * name, int hc);
// interactive version;
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf( golf &g);
// function resets handicap to new value
void handicap (golf & g,int hc);
// function displays contents of golf structure
void showgolf (const golf & g);
#endif
fun.cpp
#include"syx.h"
void delay(float time)
{
clock_t delay=time*CLOCKS_PER_SEC;
clock_t start=clock();
while(clock()-start<delay);
}
int setgolf( golf &g)
{
std::cout<<"请输入选手姓名:";
std::cin.getline(g.fullname,40);
if(g.fullname[0]=='\0')
return 0;
std::cout<<"请输入选手等级";
std::cin>>g.handicap;
std::cin.get();
return 1;
}
void setgolf (golf & g, const char * name, int hc)
{
strcpy(g.fullname,name);
handicap(g,hc);
}
void handicap (golf & g,int hc)
{
g.handicap=hc;
}
void showgolf (const golf & g)
{
std::cout<<"选手:"<<g.fullname<<"等级:"<<g.handicap<<std::endl;
}