一、题目
采用C++ 的方式定义运动员类别,并设计程序实现以下功能:
(1)新增和删除球员(田径、足球运动员)
(2)球员信息查询
(3)统计球员人数
(4)所有信息更新都要写入磁盘中(包括新增、删除运动员)
二、实现
1.类的设计
代码如下(示例):
class Player
{
public:
string name;//运动员姓名
float weight; //体重
float height; //身高
Date birth; //生日
string nation; //民族
bool sex; //性别
Player(string n, float w, float h, Date da, string na, bool s) :name(n), weight(w), height(h), birth(da), nation(na), sex(s) {} //构造函数
virtual void display() { //输出运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
};
//因为容器中是基类指针,但对象是派生类对象
//两个派生类有独有的数据成员,需要虚函数让基类指针可以调用,并且增加代码的复用性
virtual void save_(ofstream&) {}; //虚函数,保存内存中运动员数据至磁盘文件
virtual void read_(ifstream&) {}; //虚函数,读取磁盘文件中的运动员数据
virtual string find_project()=0; //纯虚函数,返回田径派生类中独有的数据成员:项目
virtual int find_number() =0; //纯虚函数,返回足球派生类中独有的数据成员:号码
virtual ~Player() {}; //虚析构函数,用来通过基类指针释放内存(删除派生类对象)
};
class football_player :public Player {
private:
char number; //足球运动员号码
public:
football_player(string n = "", float w = 0, float h = 0, Date da = { 0,0,0 }, string na = "", bool s = 0, char nu = 0) :Player(n, w, h, da, na, s), number(nu) {} //带默认参数的构造函数
~football_player() {} //虚析构函数
void save_(ofstream& fout) { //保存足球运动员的号码数据至磁盘文件
fout << number << endl;
}
void read_(ifstream& fin) { //读取磁盘文件中的足球运动员数据
fin >> name >> weight >> height >> birth.year >> birth.month >> birth.day >> nation >> sex >> number;
}
int find_number() { //返回足球派生类中独有的数据成员:号码
return number;
}
string find_project() { //实际上用不到,为实现多态
return " ";
}
void display() { //输出足球运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
cout << "number: " << number << endl;
}
};
class track_and_field_player :public Player {
public:
string project;//田径项目
track_and_field_player(string n = "", float w = 0, float h = 0, Date da = { 0,0,0 }, string na = "", bool s = 0, string p = "") :Player(n, w, h, da, na, s), project(p) { } //
~track_and_field_player() {}
void save_(ofstream& fout) { //保存田径运动员的项目数据至磁盘文件
fout << project << endl;
}
void read_(ifstream& fin) { //读取磁盘文件中的田径运动员数据
fin >> name >> weight >> height >> birth.year >> birth.month >> birth.day >> nation >> sex >> project;
}
string find_project() { //返回田径派生类中独有的数据成员:号码
return project;
}
int find_number() { //用不到,但为了多态
return 0;
}
void display() { //输出田径运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
cout << "project: " << project << endl;
}
};
2.函数设计
代码如下(示例):
//函数声明
void save(vector<Player*>&, int);
void read(vector<Player*>&, int);
void find(vector<Player*>& c, bool i);
void find_name(vector<Player*>& c, string name, bool i);
void find_project_or_number(vector<Player*>& c, bool i);
void Insert(vector<Player*>& c, int n, bool i);
void delete_(vector<Player*>& c, bool i);
void delete_name(vector<Player*>& c, vector<Player*>::iterator it, string name);
void total(vector<Player*>& c, bool i);
3.完整代码
#include <fstream>
#include<vector>
#include <iostream>
#include <string>
using namespace std;
struct Date {
int year;
int month;
int day;
};
class Player
{
public:
string name;//运动员姓名
float weight; //体重
float height; //身高
Date birth; //生日
string nation; //民族
bool sex; //性别
Player(string n, float w, float h, Date da, string na, bool s) :name(n), weight(w), height(h), birth(da), nation(na), sex(s) {} //构造函数
virtual void display() { //输出运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
};
//因为容器中是基类指针,但对象是派生类对象
//两个派生类有独有的数据成员,需要虚函数让基类指针可以调用,并且增加代码的复用性
virtual void save_(ofstream&) {}; //虚函数,保存内存中运动员数据至磁盘文件
virtual void read_(ifstream&) {}; //虚函数,读取磁盘文件中的运动员数据
virtual string find_project()=0; //纯虚函数,返回田径派生类中独有的数据成员:项目
virtual int find_number() =0; //纯虚函数,返回足球派生类中独有的数据成员:号码
virtual ~Player() {}; //虚析构函数,用来通过基类指针释放内存(删除派生类对象)
};
class football_player :public Player {
private:
char number; //足球运动员号码
public:
football_player(string n = "", float w = 0, float h = 0, Date da = { 0,0,0 }, string na = "", bool s = 0, char nu = 0) :Player(n, w, h, da, na, s), number(nu) {} //带默认参数的构造函数
~football_player() {} //虚析构函数
void save_(ofstream& fout) { //保存足球运动员的号码数据至磁盘文件
fout << number << endl;
}
void read_(ifstream& fin) { //读取磁盘文件中的足球运动员数据
fin >> name >> weight >> height >> birth.year >> birth.month >> birth.day >> nation >> sex >> number;
}
int find_number() { //返回足球派生类中独有的数据成员:号码
return number;
}
string find_project() { //实际上用不到,为实现多态
return " ";
}
void display() { //输出足球运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
cout << "number: " << number << endl;
}
};
class track_and_field_player :public Player {
public:
string project;//田径项目
track_and_field_player(string n = "", float w = 0, float h = 0, Date da = { 0,0,0 }, string na = "", bool s = 0, string p = "") :Player(n, w, h, da, na, s), project(p) { } //
~track_and_field_player() {}
void save_(ofstream& fout) { //保存田径运动员的项目数据至磁盘文件
fout << project << endl;
}
void read_(ifstream& fin) { //读取磁盘文件中的田径运动员数据
fin >> name >> weight >> height >> birth.year >> birth.month >> birth.day >> nation >> sex >> project;
}
string find_project() { //返回田径派生类中独有的数据成员:号码
return project;
}
int find_number() { //用不到,但为了多态
return 0;
}
void display() { //输出田径运动员数据
cout << "This player is" << endl;
cout << "name:" << name << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "birthday:" << birth.year << "." << birth.month << "." << birth.day << endl;
cout << "nation:" << nation << endl;
cout << "sex:" << ((sex == 0) ? "male" : "female") << endl;
cout << "project: " << project << endl;
}
};
//函数声明
void save(vector<Player*>&, int);
void read(vector<Player*>&, int);
void find(vector<Player*>& c, bool i);
void find_name(vector<Player*>& c, string name, bool i);
void find_project_or_number(vector<Player*>& c, bool i);
void Insert(vector<Player*>& c, int n, bool i);
void delete_(vector<Player*>& c, bool i);
void delete_name(vector<Player*>& c, vector<Player*>::iterator it, string name);
void total(vector<Player*>& c, bool i);
void save(vector<Player*>& c, bool i)
{
ofstream fout;
if (i == 0) fout.open("track_and_field_player.txt", ios::out);
else if (i == 1) { fout.open("football_player.txt", ios::out); }
if (!fout)
{
cout << "保存数据失败\n";
return;
}
for (int i = 0; i < c.size(); i++)
{
fout << c[i]->name << " " << c[i]->weight << " " << c[i]->height << " " << c[i]->birth.year << " " << c[i]->birth.month << " " << c[i]->birth.day << " " << c[i]->nation << " " << c[i]->sex << " ";
//cout <<((p.sex==0)?"male":"female")<<endl;
c[i]->save_(fout);
delete c[i];
}
fout.close();
}
void read(vector<Player*>& c, bool o)
{
ifstream fin;
if (o == 0) fin.open("track_and_field_player.txt", ios::in);
else fin.open("football_player.txt", ios::in);
if (!fin)
{
cout << "读取文件失败" << endl;
return;
}
char a;
Player* p;
for (int i = 0;; i++)
{
if (o == 0) { p = new track_and_field_player; }
else { p = new football_player; }
c.push_back(p);
c[i]->read_(fin);
//c[i]->project;
if (!fin.get(a)) { //保证读到文件结尾
delete p;
c.pop_back();
break;
}
}
fin.close();
if (c.empty() != true) cout << "读取存档成功!" << endl;
else cout << "存档中无运动员数据" << endl;
}
void find(vector<Player*>& c, bool i)
{
if (i == 0) cout << "输入查询类型:1.运动员姓名查询; 2.运动员项目查询:" << endl;
else cout << "输入查询类型:1.运动员姓名查询; 2.运动员号码查询:" << endl;
char find_kind;
cin >> find_kind;
string s;
switch (find_kind)
{
case '1':
//运动员姓名查询
cout << "请输入运动员姓名" << endl;
cin >> s;
find_name(c, s, i);
break;
case '2':
//运动员项目/号码查询
find_project_or_number(c, i);
break;
default:
cout << "无效选择\n";
break;
}
cout << "\n";
};
void find_name(vector<Player*>& c, string name, bool i) {
int n = 0;
vector<Player*>::iterator it = c.begin();
vector<Player*>::iterator it1 = it;
for (it = c.begin(); it != c.end(); it++) {
//for (int i=0; i<c.size(); i++) {
if ((*it)->name == name) {
n++;
if (n == 1) {
it1 = it;
}
//if(n==2){break;} //有重合运动员信息则不重复输出
(*it)->display();
}
}
if (n == 0) {
cout << "error:找不到" << name << "运动员!" << endl;
cout << "是否要输入该运动员信息,若要输入则键入1" << endl;
cin >> n;
if (n == 1) {
Insert(c, 0, i);
}
}
else if (n > 1) {
int d = 0;
cout << "运动员信息重合,是否要删除多余重复信息\n删除键入1 否则键入0" << endl;
cin >> d;
if (d == 1) {
delete_name(c, ++it1, name);
}
cout << "成功删除!" << endl;
}
}
void find_project_or_number(vector<Player*>& c, bool i) {
int n = 0;
if (i == 0) {
string p;
cout << "请输入运动员的项目" << endl;
cin >> p;
for (int i = 0; i < c.size(); i++) {
if (c[i]->find_project() == p) {
c[i]->display();
n++;
}
}
if (n == 0) {
cout << "error:找不到" << p << "项目!" << endl;
}
}
else {
char p;
cout << "请输入运动员的号码" << endl;
cin >> p;
for (int i = 0; i < c.size(); i++) {
if (c[i]->find_number() == p) {
c[i]->display();
n++;
}
}
if (n == 0) {
cout << "error:找不到" << p << "号码!" << endl;
}
}
}
void Insert(vector<Player*>& c, int n, bool i) { //在第n个前插入,如果n=0则在尾部插入
if (n > c.size()) {
cout << "error:该系统中只有" << c.size() << "个运动员数据!\n请重新输入n" << endl;
int a = 0;
cin >> a;
Insert(c, a, i);
return;
}
if (i == 0) cout << "请输入运动员的姓名、体重、身高、生日、民族、性别、田径项目" << endl;
else cout << "请输入运动员的姓名、体重、身高、生日、民族、性别、号码" << endl;
cout << "(性别:男性请键入0,女性请键入1)" << endl;
Player* p;
string name; float weight; float height;
Date birth; string nation; bool sex;
cin >> name >> weight >> height >> birth.year >> birth.month >> birth.day >> nation >> sex;
if (i == 0) {
string project;
cin >> project;
p = new track_and_field_player(name, weight, height, birth, nation, sex, project);
}
else {
int number;
cin >> number;
p = new football_player(name, weight, height, birth, nation, sex, number);
}
if (n == 0) { c.push_back(p); }
else {
vector<Player*>::iterator it = c.begin() + n - 1;
c.insert(it, p);
}
cout << "输入成功!" << endl;
}
void delete_name(vector<Player*>& c, vector<Player*>::iterator it, string name) {
//vector<track_and_field_player>::iterator it=c.begin();
for (; it != c.end();) {
if ((*it)->name == name) {
delete* it; //通过基类指针删除
it = c.erase(it);
}
else { ++it; }
}
}
void delete_(vector<Player*>& c, bool i) {
string name;
cout << "请输入需删除的运动员的姓名" << endl;
cin >> name;
int n = 0;
vector<Player*>::iterator it = c.begin();
vector<Player*>::iterator it1 = it;
for (it = c.begin(); it != c.end(); it++) {
if ((*it)->name == name) {
n++;
if (n == 1) {
it1 = it;
}
(*it)->display();
}
}
if (n == 0) {
cout << "该运动员信息不存在或已删除" << endl;
}
else if (n >= 1) {
int d = 0;
cout << "确定删除以上信息吗?\n删除请键入1,否则键入0" << endl;
cin >> d;
if (d == 1) {
delete_name(c, it1, name);
}
cout << "删除成功!" << endl;
}
}
void total(vector<Player*>& c, bool i) {
if (i == 0) cout << "田径运动员总人数为" << c.size() << endl;
else cout << "足球运动员总人数为" << c.size() << endl;
}
void first(vector<Player*>& player, bool& i) {
cout << "进入田径运动员系统请键入0\n进入足球运动员系统请键入1" << endl;
cin >> i;
read(player, i);
bool circle = false;
while (circle == false) {
cout << "您可以选择:1.查询运动员信息 2.新增运动员信息 3.删除运动员信息 4.查询运动员总人数" << endl;
cout << "请键入" << endl;
int a;
cin >> a;
switch (a) {
case 1:
find(player, i);
break;
case 2:
Insert(player, 0, i);
break;
case 3:
delete_(player, i);
break;
case 4:
total(player, i);
break;
default:
break;
}
if (i == 0) { cout << "是否退出田径运动员系统,退出请键入1,否则键入0" << endl; }
else { cout << "是否退出足球运动员系统,退出请键入1,否则键入0" << endl; }
cin >> circle;
}
save(player, i); //保存数据
bool I;
cout << "是否退出该运动管理系统,退出键入1,否则键入0" << endl;
cin >> I;
if (I == 1) {
cout << "成功退出!" << endl;
return;
}
else {
player.clear();
first(player, i);
} //递归
}
int main() {
vector<Player*> player;
bool i = 1;
cout << "欢迎打开运动员管理系统" << endl;
first(player, i);
return 0;
}
4.实现功能
1、信息查询功能:(先选择进入田径运动员管理系统还是足球运动员管理系统,)田径运动员信息可以按名字或田径项目查询,足球运动员信息可以按名字和球员号码查询。
2、新增运动员信息功能:(先选择进入田径运动员管理系统还是足球运动员管理系统,)选择新增运动员选项,系统提示输入后,用户输入运动员信息,即可新增运动员信息。
3、删除运动员信息功能:(先选择进入田径运动员管理系统还是足球运动员管理系统,)按照运动员名字找到需要删除的运动员,选择确认删除,即可删除运动员信息。
4、人数统计功能:(先选择进入田径运动员管理系统还是足球运动员管理系统,),选择查询运动员总人数的选项,即可统计人数。
总结
这是本科大一C++课程的期末实验,放在这里供大家参考,有问题可以写评论哈
另外我的老师给的建议是,函数设计那里可以用一个类进行封装,但是我懒得改了,看的朋友们可以自己再改一下代码