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

来源:互联网 发布:c盘中windows.old 编辑:程序博客网 时间:2024/06/08 14:47
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 functionvoid 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 stringint setgolf( golf &g);// function resets handicap to new valuevoid handicap (golf & g,int hc);// function displays contents of golf structurevoid 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;}



0 0