学习C++ primer plus 第9章编程练习1

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值