第10章第3题

    xiaoxiao2021-03-25  87

    golf.h

    #ifndef GOLF_H_#define GOLF_H_class golf{ enum { Len = 40 }; private: char fullname[Len]; int handicap; public: golf(const char* nm = "\0" , int cp = 0); ~golf(); int setGolf(const char* nm , int p); void cap(int ac); void show()const;};#endif

    golf.cxx

    #include<iostream>#include<cstring>#include"golf.h"golf::golf(const char* nm , int cp){ strcpy(fullname,nm); handicap = cp;}golf::~golf(){}int golf::setGolf(const char* nm , int p){ if(*nm == '\0') return 0; else { strcpy(fullname,nm); handicap = p; } return 1;}void golf::cap(int ac){ handicap = ac;}void golf::show()const{ std::cout<<"姓名: "<<fullname<<"\n"; std::cout<<"成绩: "<<handicap<<"\n";}

    main.cxx

    #include<iostream>#include"golf.h"int main(){ golf tod[10]; char names[40]; int hc = 0; int nu = 0; //记录输入了几个成员 for(int i = 0 ; i < 10 ; ++i) { std::cout<<"请输入姓名:"; std::cin.getline(names,40); std::cout<<"请输入成绩:"; std::cin>>hc; std::cin.get(); int bl = (tod+i)->setGolf(names,hc); if(bl == 0) break; ++nu; } for(int i = 0 ; i < nu ; ++i) (tod+i)->show(); int newCap = 0; for(int i = 0 ; i < nu ; ++i) { std::cout<<"请输入第"<<i+1<<"位先生的新成绩:"; std::cin>>newCap; if(std::cin) (tod+i)->cap(newCap); //如果输入不合法就部更改成绩 } for(int i = 0 ; i < nu ; ++i) (tod+i)->show(); return 0;}

    转载请注明原文地址: https://ju.6miu.com/read-15501.html

    最新回复(0)