當前位置:商標查詢大全網 - 會計考試 - 運動會計分程序!請用C++編寫出壹個程序以實現:

運動會計分程序!請用C++編寫出壹個程序以實現:

/*學校運動會管理系統

問題描述:

(1) 初始化輸入:N-參賽院系總數,M-男子競賽項目數,W-女子競賽項目數;

(2) 各項目名次取法有如下幾種:

取前5名:第1名得分 7,第2名得分 5,第3名得分3,第4名得分2,第5名得分 1;

(3) 由程序提醒用戶填寫比賽結果,輸入各項目獲獎運動員的信息。

(4) 所有信息記錄完畢後,用戶可以查詢各個院系或個人的比賽成績,生成團體總分報表,查看參賽院系信息、獲獎運動員、比賽項目信息等。

(5) 原始數據需保存到磁盤文件中。

*/

#include "stdafx.h"

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include <algorithm>

using namespace std;

#define N 4

#define M 3

#define W 3

struct Student

{

Student(){}

Student(vector<string>* departments):depars(departments){}

void Input();

void Print();

friend ofstream& operator<<(ofstream& ofs, Student& stu);

friend ifstream& operator>>(ifstream& ifs, Student& stu);

string name;

string department;

int points;

private:

vector<string>* depars;

};

ofstream& operator<<(ofstream& ofs, Student& stu)

{

ofs << stu.name <<"\t"<< stu.department<<"\t" << stu.points;

return ofs;

}

ifstream& operator>>(ifstream& ifs, Student& stu)

{

ifs >> stu.name >> stu.department >> stu.points;

return ifs;

}

void Student::Input()

{

cout<<"輸入名字:"<<endl;

cin >> name;

bool invalid(true);

do

{

string d;

cout<<"輸入院系名稱:"<<endl;

cin >> d;

vector<string>::iterator found=find(depars->begin(),depars->end(),d);

if(found != depars->end())

{

invalid = false;

department = d;

} else {

cout<<"無效院系名稱,請重新輸入。"<<endl;

}

} while(invalid);

}

void Student::Print()

{

cout<< name <<"\t"<<department<<endl;

}

// Forward declaration

class GameInfo;

class SportEvent

{

public:

static int GetPoint(int ranking);

void InputWinners(GameInfo& g);

void Print();

friend ofstream& operator<<(ofstream& ofs, SportEvent& se);

friend ifstream& operator>>(ifstream& ifs, SportEvent& se);

public:

vector<Student> winners;

string name;

};

ofstream& operator<<(ofstream& ofs, SportEvent& se)

{

ofs << se.name << endl;

ofs << se.winners.size() << endl;

for(unsigned int i(0);i<se.winners.size();i++)

{

ofs << se.winners.at(i) <<"\t";

}

return ofs;

}

ifstream& operator>>(ifstream& ifs, SportEvent& se)

{

ifs >> se.name;

unsigned int count(0);

ifs >> count;

se.winners.clear();

for(unsigned int i(0);i<count;i++)

{

Student student;

ifs >> student;

se.winners.push_back(student);

}

return ifs;

}

int SportEvent::GetPoint(int ranking)

{

switch(ranking)

{

case 1:

return 7;

break;

case 2:

return 5;

break;

case 3:

return 3;

break;

case 4:

return 2;

break;

case 5:

return 1;

break;

}

return 0;

}

void SportEvent::Print()

{

// #1 student_name department_name

for(int i(0);i<5;i++)

{

cout <<"#"<<(i+1)<<"\t";

winners.at(i).Print();

}

}

class GameInfo

{

public:

void Init();

void InputResult();

void QueryDep(string depart);

void QueryStu(string student);

void QueryEve(string eventname);

void Save(ofstream&);

void Load(ifstream&);

public:

vector<string> departments;

vector<SportEvent> mevents;

vector<SportEvent> wevents;

};

void SportEvent::InputWinners(GameInfo& g)

{

for(int i(0);i<5;i++)

{

Student s(&g.departments);

cout<<"輸入第"<<(i+1)<<"個獲獎者"<<endl;

s.Input();

winners.push_back(s);

}

}

void GameInfo::Save(ofstream& fout)

{

//保存院系名稱

fout << departments.size() << endl;

for(unsigned int i(0);i<departments.size();i++)

{

fout << departments.at(i) <<"\t";

}

fout << endl;

//保存男子項目

fout << mevents.size() << endl;

for(unsigned int i(0);i<mevents.size();i++)

{

fout << mevents.at(i) <<"\t";

}

fout << endl;

//保存女子項目

fout << wevents.size() << endl;

for(unsigned int i(0);i<wevents.size();i++)

{

fout << wevents.at(i) <<"\t";

}

fout << endl;

}

void GameInfo::Load(ifstream& fin)

{

}

void GameInfo::Init()

{

for(int i(0);i<N;i++)

{

string dname;

cout<<"輸入第"<<(i+1)<<"個參賽院系名稱:";

cin >> dname;

departments.push_back(dname);

}

cout <<endl;

for(int i(0);i<M;i++)

{

SportEvent mevent;

cout<<"輸入第"<<(i+1)<<"個男子項目名稱:";

cin >> mevent.name;

mevents.push_back(mevent);

}

cout <<endl;

for(int i(0);i<W;i++)

{

SportEvent wevent;

cout<<"輸入第"<<(i+1)<<"個女子項目名稱:";

cin >> wevent.name;

wevents.push_back(wevent);

}

cout <<endl;

}

void GameInfo::InputResult()

{

for(int i(0);i<M;i++)

{

cout<<"輸入第"<<(i+1)<<"個男子項目\"" << mevents.at(i).name<<"\""<<endl;

mevents.at(i).InputWinners(*this);

}

for(int i(0);i<W;i++)

{

cout<<"輸入第"<<(i+1)<<"個女子項目\"" << wevents.at(i).name<<"\""<<endl;

wevents.at(i).InputWinners(*this);

}

}

void GameInfo::QueryDep(string depart)

{

cout << depart << endl;

int points(0);

for(int i(0);i<M;i++)

{

vector<Student> w = mevents.at(i).winners;

for(unsigned int n(0);n<w.size();n++)

{

if(w.at(n).department==depart)

{

cout << (w.at(n).name) << "\t";

cout << (mevents.at(i).name);

cout << "\t#" << (n+1); // ranking

int p(SportEvent::GetPoint(n+1));

cout << "\t" << p << "分"<< endl; // point

points+=p;

}

}

}

for(unsigned int i(0);i<W;i++)

{

vector<Student> w = wevents.at(i).winners;

for(unsigned int n(0);n<w.size();n++)

{

if(w.at(n).department==depart)

{

cout << w.at(n).name << "\t";

cout << wevents.at(i).name;

cout << "\t#" << (n+1); // ranking

int p(SportEvent::GetPoint(n+1));

cout << "\t" << p << "分"<< endl; // point

points+=p;

}

}

}

if(points)

cout << "團體總分(男女混合):" << points << "分"<<endl;

}

void GameInfo::QueryStu(string student)

{

cout << student << endl;

int points(0);

string d;

for(int i(0);i<M;i++)

{

vector<Student> w = mevents.at(i).winners;

for(unsigned int n(0);n<w.size();n++)

{

if(w.at(n).name==student)

{

cout << mevents.at(i).name;

cout << "\t#" << (n+1); // ranking

int p(SportEvent::GetPoint(n+1));

cout << "\t" << p << "分"<< endl; // point

points+=p;

d = w.at(n).department;

}

}

}

if(points)

cout <<"隸屬於:"<< d <<endl<< "個人總分:" << points << "分"<<endl;

cout << endl;

points =0;

for(int i(0);i<W;i++)

{

vector<Student> w = wevents.at(i).winners;

for(unsigned int n(0);n<w.size();n++)

{

if(w.at(n).name==student)

{

cout << wevents.at(i).name;

cout << "\t#" << (n+1); // ranking

int p(SportEvent::GetPoint(n+1));

cout << "\t" << p << "分"<< endl; // point

points+=p;

d =w.at(n).department;

}

}

}

if(points)

cout <<"隸屬於:"<< d <<endl<< "個人總分:" << points << "分"<<endl;

}

void GameInfo::QueryEve(string eventname)

{

for(int i(0);i<M;i++)

{

if(mevents.at(i).name == eventname)

{

mevents.at(i).Print();

}

}

for(int i(0);i<W;i++)

{

if(wevents.at(i).name == eventname)

{

wevents.at(i).Print();

}

}

}

int main()

{

const char* filename= "data.txt";

GameInfo gi;

//讀取

ifstream ifs(filename);

gi.Load(ifs);

ifs.close();

//錄入

gi.Init();

gi.InputResult();

int opt(0);

string name;

while(opt!=4)

{

cout << "1.按院系名稱查詢"<<endl;

cout << "2.按比賽項目查詢"<<endl;

cout << "3.按運動員姓名查詢"<<endl;

cout << "4.退出"<<endl;

cin >> opt;

switch(opt)

{

case 1:

cout << "請輸入院系名稱:" << endl;

cin >> name;

gi.QueryDep(name);

break;

case 2:

cout << "請輸入比賽項目:" << endl;

cin >> name;

gi.QueryEve(name);

break;

case 3:

cout << "請輸入運動員姓名:" << endl;

cin >> name;

gi.QueryStu(name);

break;

}

}

//保存

ofstream ofs(filename, ios::out | ios::app);

gi.Save(ofs);

ofs.close();

return 0;

}