//功能:实现裁判打分 //描述:为那些初学者学习方便所写的 //程序要求如下: /************************ 模拟一个裁判给比赛选手打分。 1.要求如下: ⑴ 裁判人数为UMPIRE; ⑵ 参赛选手为任意人; ⑶ 裁判给选手打分; ⑷ 去掉一个最高分,一个最低分,取其平均分为选手得分; ⑸ 按参赛选手的序号显示选手得分; ⑹ 按名次显示参赛选手得分(用插入法排序)。 2.提示: ⑴ 定义一个类名为Result的记分类为选手记分,数据成员至少包括选手编号(用整型)、姓名(用字符数组)、裁判为选手的打分及得分(用实型数组)等,成员函数自定(必须有构造函数),将类的定义保存在“result.h”中; ⑵ 测试程序(保存在exp_206.cpp中)采用交互方式: ① 提示输入参赛选手人数; ② 提示输入所有参赛选手的编号及姓名; ③ 显示比赛开始,请为选手打分; ④ 显示:去掉的最高分、最低分,选手得分; ⑤ 比赛结束,按编号显示选手的得分; ⑥ 按名次显示选手得分。 **************************************/ #include <iostream> #include <string> #include <algorithm> using namespace std; const int UMPIRE=5; class Result{ public: Result(int n,char *p)//编号,姓名 { if(strlen(p)<=0) cout << " error" << endl; num=n; strcpy(name,p); score=0; memset(judgescore,0,UMPIRE); } Result() { num=0; name[0]='/0'; score=0; memset(judgescore,0,UMPIRE); } Result(const Result& rhs) { num=rhs.num; strcpy(name,rhs.name); score=rhs.score; memcpy(judgescore,rhs.judgescore,UMPIRE); } void InitResult(int n,char *p) { if(strlen(p)<=0) cout << " error" << endl; num=n; strcpy(name,p); score=0; memset(judgescore,0,UMPIRE); } void SetJudgeScore(int *s) { int ix=0; while(ix<UMPIRE) { judgescore[ix]=s[ix]; ix++; } } int* GetJudgeScore() { return judgescore; } int GetScore() { return score; } int GetNumber() { return num; } void ShowName() { cout << name; } int FindHighPos(); int FindLessPos(); int FindHigh() { return judgescore[FindHighPos()]; } int FindLess() { return judgescore[FindLessPos()]; } void DoCalculatorScore(); friend ostream& operator<<(ostream &out,Result ); ~Result(){} private: int num; //参赛选手编号 char name[10];//姓名 int judgescore[UMPIRE];//裁判打分 int score;//得分 }; ostream& operator<<(ostream &out,Result rhs) { //由于VC6在版本较老的原因,不可以直接访问私有数据成员 //out << rhs.num << "/t" << rhs.name // << "/t" << rhs.score << endl; //因此在这里采用访问成员函数来代替访问私有成员 out << rhs.GetNumber() << "/t"; rhs.ShowName(); out << "/t" << rhs.GetScore() << endl; return out; } int Result::FindHighPos() { int *pmax=max_element(judgescore,(judgescore+UMPIRE)); return pmax-judgescore; } int Result::FindLessPos() { int *pmin=min_element(judgescore,(judgescore+UMPIRE)); return pmin-judgescore; } void Result::DoCalculatorScore() { int maxpos=FindHighPos(); int minpos=FindLessPos(); cout << "maxpos:" <<maxpos << endl; cout << "minpos:" <<minpos << endl; for(int i=0; i<UMPIRE; i++) { if((i!=maxpos) && (i!=minpos)) { score+=judgescore[i]; } } cout << "总成绩=" << score <<endl; if(FindHigh()==FindLess()) score = score/(UMPIRE); else score = score/(UMPIRE-2); } bool lessbyscore( Result& l, Result& r) { if(l.GetScore()<r.GetScore()) return true; else return false; } bool lessbynumber( Result& l, Result& r) { if(l.GetNumber()<r.GetNumber()) return true; else return false; } int init(Result *&p); void SortByPlayerNumber(); void SortByPlayerScore(); int init(Result *&p) { cout << "输入参赛选手人数:" << endl; int n; cin >> n; cout << endl; if(!(p = new Result[n])) { cout << "error" << endl; exit(1); } Result *pmv=p; int number; char name[10]; for(int i=0; i<n; i++,pmv++) { cout << "输入所有参赛选手的编号及姓名:" << endl; cin >> number >> name; pmv->InitResult(number,name); cout << "开始对第" << i+1 <<"个参赛选手的成绩的打分"<< endl; int tmpix=0; int pjudgescore[UMPIRE]; while(tmpix<UMPIRE) { cout << "输入第" << (tmpix+1) << "个裁判的打分:"; cin >> pjudgescore[tmpix++]; } cout << "结束对第" << n <<"个参赛选手的成绩的打分"<< endl; pmv->SetJudgeScore(pjudgescore); pmv->DoCalculatorScore(); cout << "去掉的最高分是:" << pmv->FindHigh() << "/t去掉最低分是:" << pmv->FindLess() << "/t最后的得分是:" << pmv->GetScore() << endl; tmpix=0; } return n; } void SortByPlayerNumber(Result *p,int n) { sort(p,p+n,lessbynumber); } void SortByPlayerScore (Result *p,int n) { sort(p,p+n,lessbyscore); } int main() { Result *r; int n=init(r); cout << "按序号从低到高显示结果如下:" << endl; SortByPlayerNumber(r,n); for (int i=0; i<n; i++) { cout << r[i]; } cout <<endl; cout << "按得分从低到高显示结果如下:" << endl; SortByPlayerScore(r,n); for (int i=0; i<n; i++) { cout << r[i]; } return 0; }