(英雄池管理系统)
Champion.h
#ifndef Champion_H
#define Champion_H
#include <iostream>
#include <string>
using namespace std;
class Champion
{
private:
int num; //编号
string name; //名字
string title; //称号
string camp; //阵营
string position; //位置
string role; //角色
string type; //控制或者爆发等信息
public:
Champion(int, string, string, string, string, string, string);
Champion();
~Champion(){};
int get_num();
string get_name();
string get_title();
string get_camp();
string get_position();
string get_role();
string get_type();
void set_num(int );
void set_name(string );
void set_title(string );
void set_camp(string );
void set_position(string );
void set_role(string );
void set_type(string );
};
class pool
{
private:
Champion Champions[200];
static int n_champions;
public:
pool(){};
~pool(){};
void readFromfile();
void addChampion();
void delChampion();
void editChampion();
void displayAll();
void searchByName();
void searchByRole();
void searchByNum();
};
#endif
Champion.cpp
#include <iostream>
#include "Champion.h"
#include <Windows.h>
#include <fstream>
using namespace std;
Champion::Champion(int num0, string name0, string title0, string camp0, string position0, string role0,string type0)
{
num = num0;
name = name0;
title = title0;
camp = camp0;
position = position0;
role = role0;
type = type0;
}
Champion::Champion()
{
num = 0;
name = "Null";
title = "Null";
camp = "Null";
position ="Null";
role = "Null";
type = "Null";
}
int Champion::get_num()
{
int a;
a = num;
return a;
}
string Champion::get_name()
{
string s;
s = name;
return s;
}
string Champion::get_title()
{
string s;
s = title;
return s;
}
string Champion::get_camp()
{
string s;
s = camp;
return s;
}
string Champion::get_position()
{
string s;
s = position;
return s;
}
string Champion::get_role()
{
string s;
s = role;
return s;
}
string Champion::get_type()
{
string s;
s = type;
return s;
}
void Champion::set_num(int n)
{
num = n;
}
void Champion::set_name(string s)
{
name = s;
}
void Champion::set_title(string s)
{
title = s;
}
void Champion::set_camp(string s)
{
camp = s;
}
void Champion::set_position(string s)
{
position = s;
}
void Champion::set_role(string s)
{
role = s;
}
void Champion::set_type(string s)
{
type = s;
}
void pool::readFromfile()
{
ifstream fin;
int num = -1;
string name;
string title;
string camp;
string position;
string role;
string type;
char a;
long x, y, z;
fin.open("text.txt", ios::in);
if (!fin)
{
cout << "不能打开英雄池文件读取英雄信息" << endl;
}
cout << "正在读取英雄池数据……" << endl;
Sleep(1500);
while (!fin.eof()){
fin >> num >> name >> title >> camp >> position >> role >> type;
if (num < 0)
{
cout << "读取英雄池数据成功!" << endl;
fin.close();
Sleep(1000);
system("cls");
break;
}
Champion someone(num, name, title, camp, position, role, type);
Champions[num] = someone;
n_champions++;
//cout << num << ' ' << title << ' ' << camp << ' ' << position << ' ' << role << ' ' << type << endl;
num = -1;
}
/*
fin >> a;
if (fin.eof())
{
cout << "当前英雄池中没有任何数据,请添加至少一个英雄信息" << endl;
fin.close();
}
else
{
fin.open("text.txt", ios::in);
//while (!fin.eof())
//{
int num;
string name ;
string title;
string camp;
string position;
string role;
string type;
x = fin.tellg(); cout << x << endl;
fin >> num >> name >> title >> camp >> position >> role >> type;
y = fin.tellg(); cout << y << endl;
fin.seekg(2, ios::cur);
z = fin.tellg(); cout << z << endl;
cout << num << ' ' << title << ' ' << camp << ' ' << position << ' ' << role << ' ' << type << endl;//' ' << endl;
Champion someone(num, name, title, camp, position, role, type);
Champions[num] = someone;
n_champions++;
//}
cout << "读取英雄池数据成功!" << endl;
*/
fin.close();
}
void pool::addChampion()
{
int num;
string name;
string title;
string camp;
string position;
string role;
string type;
ofstream fout;
fout.open("text.txt", ios::app);
if (!fout)
{
cout << "不能打开输出文件以便保存新英雄信息" << endl;
}
n_champions++;
cout << "----------正在添加新英雄……----------"<< endl;
cout << "英雄的编号是:" ;
cin >> num;
if (Champions[num].get_num() != 0)
cout << "此编号已经被注册" << endl;
else
{
cout << "英雄的名字是:";
cin >> name;
cout << "英雄的称号是:";
cin >> title;
cout << "英雄的阵营是:";
cin >> camp;
cout << "英雄适合的位置是:";
cin >> position;
cout << "英雄的定位是:";
cin >> role;
cout << "英雄的类型:";
cin >> type;
cout << "添加完毕。你可以查找到这个英雄了" << endl;
cout << "---------------------------------------" << endl;
}
fout << num << ' ' << name << ' ' << title << ' ' << camp << ' ' << position << ' ' << role << ' ' << type <<endl;//' '<<'%'<<endl;
fout.close();
Champion someone(num,name,title,camp,position,role,type);
Champions[num] = someone;
}
void pool::delChampion()
{
int num;
cout << "----------正在删除某英雄……----------" << endl;
if (n_champions == 0)
{
cout << "\n英雄池为空!没有可以删除的英雄。\n" << endl;
}
else
{
cout << "英雄池中有" << n_champions << "个英雄" << endl;
cout << "要删除的英雄编号:";
cin >> num;
Champion someone(0, "Null", "Null", "Null", "Null", "Null", "Null");
Champions[num] = someone;
cout << "该英雄已经被移出英雄池" << endl;
}
cout << "--------------------------------------" << endl;
ofstream fout;
fout.open("text.txt", ios::out);
if (!fout)
{
cout << "不能打开输出文件以便保存英雄信息" << endl;
}
for (int i = 1; i < 200; i++)
{
if (Champions[i].get_num() != 0)
{
fout << Champions[i].get_num() << ' '
<< Champions[i].get_name() << ' '
<< Champions[i].get_title() << ' '
<< Champions[i].get_camp() << ' '
<< Champions[i].get_position() << ' '
<< Champions[i].get_role() << ' '
<< Champions[i].get_type() << endl;//' '<<'%'<<endl;
}
}
fout.close();
n_champions--;
}
void pool::editChampion()
{
int num;
int new_num;
string name;
string title;
string camp;
string position;
string role;
string type;
char x;
int operation1;
cout << "----------正在编辑某英雄信息……------" << endl;
cout << "要编辑的英雄编号:";
cin >> num;
if (Champions[num].get_num() == 0)
cout << "此编号下没有英雄信息" << endl;
else
{
cout << "英雄编号:" << Champions[num].get_num() << endl;
cout << "名字:" << Champions[num].get_name() << endl;
cout << "称号:" << Champions[num].get_title() << endl;
cout << "阵营:" << Champions[num].get_camp() << endl;
cout << "适合的位置:" << Champions[num].get_position() << endl;
cout << "定位:" << Champions[num].get_role() << endl;
cout << "类型:" << Champions[num].get_type() << endl;
cout << "--------------------------------------" << endl;
cout << "请输入要修改的信息选项" << endl;
cout << "1 英雄编号" << endl;
cout << "2 名字" << endl;
cout << "3 称号" << endl;
cout << "4 阵营" << endl;
cout << "5 适合的位置" << endl;
cout << "6 定位" << endl;
cout << "7 类型" << endl;
cout << "输入端:";
cin >> operation1;
switch (operation1)
{
case 1:cout << "新的英雄编号为:"; cin >> new_num;
if (Champions[new_num].get_num() == 0)
{
Champions[new_num] = Champions[num];
Champions[new_num].set_num(new_num);
Champion someone(0, "Null", "Null", "Null", "Null", "Null", "Null");
Champions[num] = someone;
}
else
{
cout << "该编号已经被使用" << endl;
return;
}
break;
case 2:cout << "新的名字为:"; cin >> name; Champions[num].set_name(name); break;
case 3:cout << "新的称号为:"; cin >> title; Champions[num].set_title(title); break;
case 4:cout << "新的阵营为:"; cin >> camp; Champions[num].set_camp(camp); break;
case 5:cout << "新的适合的位置为:"; cin >> position; Champions[num].set_position(position); break;
case 6:cout << "新的定位为:"; cin >> role; Champions[num].set_role(role); break;
case 7:cout << "新的类型为:"; cin >> type; Champions[num].set_type(type); break;
default:cout << "输入错误!" << endl;
}
}
cout << "--------------------------------------" << endl;
cout << "修改成功!" << endl;
ofstream fout;
fout.open("text.txt", ios::out);
if (!fout)
{
cout << "不能打开输出文件以便保存英雄信息" << endl;
}
for (int i = 1; i < 200; i++)
{
if (Champions[i].get_num() != 0)
{
fout << Champions[i].get_num() << ' '
<< Champions[i].get_name() << ' '
<< Champions[i].get_title() << ' '
<< Champions[i].get_camp() << ' '
<< Champions[i].get_position() << ' '
<< Champions[i].get_role() << ' '
<< Champions[i].get_type() << endl;//' '<<'%'<<endl;
}
}
fout.close();
}
void pool::displayAll()
{
cout << "----------正在列出所有英雄信息……----" << endl;
if (n_champions == 0)
{
cout << "英雄池为空!无法列出相关信息。" << endl;
return;
}
else
cout << "英雄池中有" << n_champions << "个英雄" << endl;
for (int i = 1; i < 200; i++)
{
if (Champions[i].get_num() != 0)
{
cout << "--------------------------------------" << endl;
cout << "英雄编号:" << Champions[i].get_num() << endl;
cout << "称号:" << Champions[i].get_title() << endl;
cout << "名字:" << Champions[i].get_name() << endl;
cout << "阵营:" << Champions[i].get_camp() << endl;
cout << "适合的位置:" << Champions[i].get_position() << endl;
cout << "定位:" << Champions[i].get_role() << endl;
cout << "类型:" << Champions[i].get_type() << endl;
}
}
cout << "--------------------------------------" << endl;
}
void pool::searchByName()
{
string s,name;
cout << "要查找的英雄名字:";
cin >> s;
int x;
for (int i = 1; i < 200; i++)
{
if (Champions[i].get_num() != 0)
{
name = Champions[i].get_name();
x = name.find(s);
if (x != name.npos)
{
cout << "--------------------------------------" << endl;
cout << "英雄编号:" << Champions[i].get_num() << endl;
cout << "名字:" << Champions[i].get_name() << endl;
cout << "称号:" << Champions[i].get_title() << endl;
cout << "阵营:" << Champions[i].get_camp() << endl;
cout << "适合的位置:" << Champions[i].get_position() << endl;
cout << "定位:" << Champions[i].get_role() << endl;
cout << "类型:" << Champions[i].get_type() << endl;
}
}
}
cout << "--------------------------------------" << endl;
}
void pool::searchByRole()
{
string s;
cout << "英雄池定位分类:刺客 战士 法师 辅助 坦克 射手" << endl;
cout << "请选择要查找的定位:";
cin >> s;
for(int i = 0; i < 200; i++ )
{
if (Champions[i].get_role() == s)
{
cout << "--------------------------------------" << endl;
cout << "英雄编号:" << Champions[i].get_num() << endl;
cout << "名字:" << Champions[i].get_name() << endl;
cout << "称号:" << Champions[i].get_title() << endl;
cout << "阵营:" << Champions[i].get_camp() << endl;
cout << "适合的位置:" << Champions[i].get_position() << endl;
cout << "定位:" << Champions[i].get_role() << endl;
cout << "类型:" << Champions[i].get_type() << endl;
}
}
cout << "--------------------------------------" << endl;
}
void pool::searchByNum()
{
int num;
cout << "请输入要查询的英雄编号:";
cin >> num;
if (Champions[num].get_num() == num)
{
cout << "---------------------------------------" << endl;
cout << "英雄编号:" << Champions[num].get_num() << endl;
cout << "名字:" << Champions[num].get_name() << endl;
cout << "阵营:" << Champions[num].get_camp() << endl;
cout << "适合的位置:" << Champions[num].get_position() << endl;
cout << "定位:" << Champions[num].get_role() << endl;
cout << "类型:" << Champions[num].get_type() << endl;
}
else
{
cout << "编号为" << num << "的英雄不存在" << endl;
}
cout << "---------------------------------------" << endl;
}
main.cpp
#include<iostream>
#include "Champion.h"
#include <fstream>
using namespace std;
int pool::n_champions = 0;
int main()
{
int operation = 0;
pool League;
League.readFromfile();
while (true)
{
cout << "--------------------------------------------" << endl;
cout << "| 1.添加英雄以及信息 |" << endl;
cout << "| 2.删除英雄以及信息 |" << endl;
cout << "| 3.编辑英雄信息 |" << endl;
cout << "| 4.列出所有英雄信息 |" << endl;
cout << "| 5.按名字模糊查找英雄 |" << endl;
cout << "| 6.按英雄定位查找英雄 |" << endl;
cout << "| 7.按英雄编号查找英雄 |" << endl;
cout << "| 0.退出系统 |" << endl;
cout << "--------------------------------------------" << endl;
cout << "输入端:";
cin >> operation;
system("cls");
switch (operation)
{
case 0: return 0;
case 1: League.addChampion(); break;
case 2: League.delChampion(); break;
case 3: League.editChampion(); break;
case 4: League.displayAll(); break;
case 5: League.searchByName(); break;
case 6: League.searchByRole(); break;
case 7: League.searchByNum(); break;
default:cout << "wrong operation" << endl;
}
cout << "回到主界面?回车返回主界面,否则退出系统" << endl;
getchar(); //吸收掉回车字符
if (getchar() == '\n')
{
system("cls");
}
else
{
return 0;
}
}
return 0;
}