大二仿照黑马程序员 学生管理系统做的机甲管理系统
实现效果
附上源码 供学习交流
#include <iostream>
using namespace std;
#include<string>
#include <fstream>
#define FILENAME "mechFile.txt"
#include <time.h>
#include <stdlib.h>
#include <vector>
// 原始机甲 衍生机甲
// 运算符重载
// 类模板
// 功能 1 录入数据
// 2 展示数据
// 3 修改数据
// 4 战斗数据
// 5 机甲升级
// 6 随机宝箱
// 7 数据排序
// 文件操作 写入数据 写出数据
class BaseMech {
public:
int m_Id; // 编号
int m_Did; // 机甲类型
string m_Name; // 名字
int nPower; // 攻击力
int nValuelife; // 生命值&耐久度
// 抽象类 虚函数
virtual void attack(BaseMech* p) =0 ;
virtual void hurt(int nPower) =0 ;
virtual void frightback(BaseMech* p) =0 ;
virtual void showInf() {} // 展示信息
virtual int improveMech(int a) { return 0; } // 提升机甲
virtual int improvelifeMech(int a) { return 0; } // 提升机甲生命值
};
class IceMech :public BaseMech { // 冰霜机甲
public:
IceMech(int id, string name, int life, int power,int did);
void attack(BaseMech* p)
{
p->hurt(nPower);
p->frightback(this);
}
void hurt(int nPower)
{
nValuelife -= nPower ;
}
void frightback(BaseMech* p)
{
p->hurt(nPower / 2); // 反击比攻击力度小
}
virtual void showInf(); // 展示信息
virtual int improveMech(int a); // 提升机甲
virtual int improvelifeMech(int a); // 提升机甲生命值
};
void IceMech::showInf() // 展示信息
{
cout << " 机甲名字 " << this->m_Name
<< " 机甲编号 " << this->m_Id
<< " 机甲攻击力 " << this->nPower
<< " 机甲生命值 " << this->nValuelife << endl;
}
IceMech::IceMech(int id, string name, int life, int power,int did)
{
this->m_Id = id;
this->m_Name = name;
this->nPower = power;
this->nValuelife = life;
this->m_Did = did;
}
int IceMech::improveMech(int a) // 提升机甲
{
this->nPower = this->nPower + a;
return this->nPower;
}
int IceMech::improvelifeMech(int a) // 提升机甲
{
this->nValuelife = this->nValuelife + a;
return this->nValuelife;
}
class FireMech :public BaseMech{ // 火焰机甲
public:
FireMech(int id, string name, int life,int power,int did);
void attack(BaseMech * p)
{
p->hurt(nPower);
p->frightback(this);
}
void hurt(int nPower)
{
nValuelife -= nPower;
}
void frightback(BaseMech * p)
{
p->hurt(nPower / 2); // 反击比攻击力度小
}
virtual void showInf(); // 展示信息
virtual int improveMech(int a); // 提升机甲
virtual int improvelifeMech(int a); // 提升机甲生命值
};
void FireMech::showInf() // 展示信息
{
cout << " 机甲名字 " << this->m_Name
<< " 机甲编号 " << this->m_Id
<< " 机甲攻击力 " << this->nPower
<< " 机甲生命值 " << this->nValuelife << endl;
}
int FireMech::improveMech(int a) // 提升机甲
{
this->nPower = this->nPower + a;
return this->nPower;
}
int FireMech::improvelifeMech(int a) // 提升机甲
{
this->nValuelife = this->nValuelife + a;
return this->nValuelife;
}
FireMech::FireMech(int id, string name, int life, int power,int did)
{
this->m_Id = id;
this->m_Name = name;
this->nPower = power;
this->nValuelife = life;
this->m_Did = did;
}
class RayMech :public BaseMech { // 雷电机甲
public:
RayMech(int id, string name, int life, int power,int did);
void attack(BaseMech* p)
{
p->hurt(nPower);
p->frightback(this);
}
void hurt(int nPower)
{
nValuelife -= nPower;
}
void frightback(BaseMech* p)
{
p->hurt(nPower / 2); // 反击比攻击力度小
}
virtual void showInf(); // 展示信息
virtual int improveMech(int a); // 提升机甲
virtual int improvelifeMech(int a); // 提升机甲生命值
};
int RayMech::improveMech(int a) // 提升机甲
{
this->nPower = this->nPower + a;
return this->nPower;
}
int RayMech::improvelifeMech(int a) // 提升机甲
{
this->nValuelife = this->nValuelife + a;
return this->nValuelife;
}
void RayMech::showInf() // 展示信息
{
cout << " 机甲名字 " << this->m_Name
<< " 机甲编号 " << this->m_Id
<< " 机甲攻击力 " << this->nPower
<< " 机甲生命值 " << this->nValuelife << endl;
}
RayMech::RayMech(int id, string name, int life, int power,int did)
{
this->m_Id = id;
this->m_Name = name;
this->nPower = power;
this->nValuelife = life;
this->m_Did = did;
}
class MechManager
{
public:
int m_MechNum; // 记录文件中机甲个数
BaseMech** m_MechArray; //机甲数组的指针
bool m_FileIsEmpty;
// 构造函数
MechManager();
int get_MechNum(); // 获取数量
void init_Mech(); // 初始化机甲
void Save(); // 保存数据
int If_Exist(int id); // 判断机甲是否存在
void show_Meau(); // 展示菜单
void exit_System(); // 退出系统
void Add_Mech(); // 增加机甲
void Del_Mech(); // 删除机甲
void Show_Mech(); // 展示机甲
void Mod_Mech(); // 修改数据
void Find_Mech(); // 查找机甲
void Sort_Mech() ; // 攻击力排序
void Improve_Mech(); // 随机提升攻击力
void Fright_Mech(); // 机甲战斗
void Clear_Mech(); //文件清零
};
MechManager::MechManager()
{
//1 文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);
if (!ifs.is_open())
{
// cout << "文件打开失败" << endl;
// 初始化人数
this->m_MechNum = 0;
// 初始化指针
this->m_MechArray = NULL;
this->m_FileIsEmpty = true;
return;
}
//2 文件存在但是为空 // 判断eof
char ch;
ifs >> ch;
if (ifs.eof())
{
// cout << "文件为空" << endl;
// 初始化人数
this->m_MechNum = 0;
// 初始化指针
this->m_MechArray = NULL;
this->m_FileIsEmpty = true;
return;
}
// 3 文件存在且记录数据 // 先判断有多少个人 录入数据 并且展示数据
int num = this->get_MechNum();
// cout << "现有职工数量为" << num<< endl;
this->m_MechNum = num;
// 开辟需要的空间 // ??不加*号不行
// 根据职工数创建数组
// this->m_EmpNum = this->get_EmpNum(); (和38 行上面代码重复)
this->m_MechArray = new BaseMech * [ num]; // worker* 而不是 *worker
// 初始化数组
this->init_Mech();
// 测试数据 // 姓名乱码 打开文件所在位置 txt文件 选择另存为 之后在下面编码格式该为ASII
//for (int i = 0; i < num; i++) // ??如何打出的
//{
// cout << " 职工编号 " << this->m_EmpArray[i]->m_Id
// << " 职工姓名 " << this->m_EmpArray[i]->m_Name
// << " 职工部门编号 " << this->m_EmpArray[i]->m_DeptId << endl;
//}
ifs.close();
}
void MechManager::Fright_Mech() // 机甲战斗
{
cout << "请输入2个机甲序号" << endl;
int id1;
int id2;
cin >> id1;
cin >> id2;
int ret1 = this->If_Exist(id1);
int ret2 = this->If_Exist(id2);
if (ret1 != -1 && ret2 != -1)
{
this->m_MechArray[ret1]->showInf();
this->m_MechArray[ret2]->showInf();
this->m_MechArray[ret1]->attack(this->m_MechArray[ret2]);
if (this->m_MechArray[ret1]->nValuelife <= 0 && this->m_MechArray[ret2] <= 0)
cout << "两机甲同归于尽 " << endl;
else
{
if (this->m_MechArray[ret1] <= 0)
{
cout << this->m_MechArray[ret2]->m_Name << "获胜" << endl;
}
else
{
cout << this->m_MechArray[ret1]->m_Name << "获胜" << endl;
}
this->m_MechArray[ret1]->showInf();
this->m_MechArray[ret2]->showInf();
}
}
else
{
cout << "输入序号至少有一个错误" << endl;
}
}
void MechManager:: Improve_Mech()
{
cout << "请输入操作机甲序号" << endl;
int id;
cin >> id;
int ret = this->If_Exist(id);
if (ret == -1)
{
cout << "输入序号不存在" << endl;
}
else
{
this->m_MechArray[ret]->showInf();
time_t t;
/* 初始化随机数发生器 */
srand((unsigned)time(&t));
vector<int> v;
/* 输出 0 到 1000 之间的 3 个随机数 */
for (int i = 0; i < 3; i++) {
v.push_back(rand() % 1000);
}
cout << "随机三个数据" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << " "<< *it << " " ;
}
cout << endl;
cout << " 输入 1 选择第1个数据" << endl;
cout << " 输入 2 选择第2个数据" << endl;
cout << " 输入 3 选择第3个数据" << endl;
cout << " 输入 -1 选择第1个数据" << endl;
cout << " 输入 -2 选择第2个数据" << endl;
cout << " 输入 -3 选择第3个数据" << endl;
cout << " 输入负数增加生命值 "
<< " 输入正数增加攻击力" << endl;
int dselect=0;
cin >> dselect;
switch (dselect)
{
case 1:
this->m_MechArray[ret]->improveMech(v[0]);
break;
case 2:
this->m_MechArray[ret]->improveMech(v[1]);
break;
case 3:
this->m_MechArray[ret]->improveMech(v[2]);
break;
case -1:
this->m_MechArray[ret]->improvelifeMech(v[2]);
break;
case -2:
this->m_MechArray[ret]->improvelifeMech(v[2]);
break;
case -3:
this->m_MechArray[ret]->improvelifeMech(v[2]);
break;
}
if (dselect == 1 || dselect == 2 || dselect == 3)
{
cout << "随机增加攻击力已完成" << endl;
}
else
cout << "随机增加生命值已完成" << endl;
this->m_MechArray[ret]->showInf();
}
}
void MechManager::Clear_Mech() //文件清零
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
int select = 0;
cout << "是否删除 文件" << endl;
cout << "1, 确认删除" << endl;
cout << "2, 返回" << endl;
cin >> select;
if (select == 1)
{
ofstream ofs(FILENAME, ios::trunc);
if (this->m_MechArray != NULL)
{
for (int i = 0; i < this->m_MechNum; i++)
{
delete this->m_MechArray[i];
this->m_MechArray[i] = NULL;
}
this->m_MechNum = 0;
delete[]this->m_MechArray;
this->m_MechArray = NULL;
}
}
}
system("pause");
system("cls");
}
void MechManager::Sort_Mech() // 攻击力排序
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
system("pause");
system("cls");
}
else
{
cout << "请选择排序方式" << endl;
cout << "1, 按照攻击力升序排列" << endl;
cout << "2,按照攻击力降序排列" << endl;
int select = 0;
cin >> select;
// 选择排序 两个两个比较 最后输出
for (int i = 0; i < this->m_MechNum; i++)
{ // 选择排序 两个两个比较 最后输出
int minOrMax = i; // 声明最大值和最小值下标 先找到最小值 放到第一个位置不再变化
// 之后再比较其余的数据
for (int j = i + 1; j < this->m_MechNum; j++)
{
if (select == 1) // 升序
{
if (this->m_MechArray[minOrMax]->nPower > this->m_MechArray[j]->nPower)
{
minOrMax = j;
}
}
else if (select == 2)
{
if (this->m_MechArray[minOrMax]->nPower < this->m_MechArray[j]->nPower)
{
minOrMax = j;
}
}
else
{
cout << "输入有误" << endl;
}
}
//判定一开始认定的最大值 最小值 是否是 计算下的最大值最小值 如果不是交换数据
if (minOrMax != i)
{
BaseMech* temp = this->m_MechArray[minOrMax];
this->m_MechArray[minOrMax] = this->m_MechArray[i];
this->m_MechArray[i] = temp;
}
}
cout << "排序成功!排序结果为" << endl;
this->Save(); // 存入磁盘
this->Show_Mech();
}
}
void MechManager::Find_Mech() //查找机甲
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空" << endl;
}
else
{
cout << "请输入查找方式" << endl;
cout << "1. 按编号查找" << endl;
cout << "2. 按照名称查找" << endl;
int sel = 0;
cin >> sel;
if (sel == 1)
{
int ret = 0;
int id1 = 0;
cout << "请输入编号" << endl;
cin >> id1;
ret = id1;
ret = this->If_Exist(ret);
if (ret != -1)
{
cout << "编号为" << id1
<< "已经被查到" << endl;
this->m_MechArray[ret]->showInf();
}
else
{
cout << "查无此机甲" << endl;
}
}
else if (sel == 2)
{
//按照姓名查找
string name;
cout << "请输入名称" << endl;
cin >> name;
bool flag = false;
for (int i = 0; i < this->m_MechNum; i++)
{
if (this->m_MechArray[i]->m_Name == name)
{
cout << "查找成功" << endl;
this->m_MechArray[i]->showInf();
flag = true;
}
}
if (!flag) // if(flag == false)
{
cout << "查找失败" << endl;
}
}
else
{
cout << "输入错误,请重新输入" << endl;
}
system("pause");
system("cls");
}
}
void MechManager::Mod_Mech() //修改数据
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或者记录为空" << endl;
}
else
{
int id;
cout << "请输入机甲编号" << endl;
cin >> id;
int ret = this->If_Exist(id);
if (ret != -1)
{
delete this->m_MechArray[ret];
int newid;
string name;
int life;
int power;
int dSlect;
BaseMech* basemech = NULL;
cout << "已查到" << id << "的机甲" << endl;
cout << "请输入新的机甲编号" << endl;
cin >> newid;
cout << "请输入新的机甲名字" << endl;
cin >> name;
cout << "请输入新机甲类型" << endl;
cin >> dSlect;
cout << "请输入新机甲生命值" << endl;
cin >> life;
cout << "请输入新机甲攻击力" << endl;
cin >> power;
switch (dSlect)
{
case 1:
basemech = new FireMech(newid, name, life, power, 1); // 构造函数赋初值
break;
case 2:
basemech = new IceMech(newid, name, life, power, 2);
break;
case 3:
basemech = new RayMech(newid, name, life, power, 3);
break;
default:
break;
}
this->m_MechArray[ret] = basemech;
}
else
{
cout << "修改失败,查无此机甲" << endl;
}
}
system("pause");
system("cls");
}
void MechManager::Save()
{
ofstream ofs;
ofs.open(FILENAME, ios::out);
for (int i = 0; i < this->m_MechNum; i++)
{
ofs << this->m_MechArray[i]->m_Id << " "
<< this->m_MechArray[i]->m_Name << " "
<< this->m_MechArray[i]->nValuelife << " "
<< this->m_MechArray[i]->nPower<< " "
<< this->m_MechArray[i]->m_Did << " " << endl;
}
ofs.close();
}
void MechManager::init_Mech()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int id;
string name;
int dId;
int life;
int power;
int index = 0;
while (ifs >> id && ifs >> name && ifs >> life && ifs >> power && ifs >> dId)
{
BaseMech* basemech = NULL;
if (dId == 1) // 火焰
{
basemech = new FireMech(id, name,life ,power ,dId);
}
if (dId == 2) //冰霜
{
basemech = new IceMech(id, name, life, power, dId);
}
if (dId == 3) // 雷电
{
basemech = new RayMech(id, name, life, power, dId);
}
this->m_MechArray[index] = basemech;
index++;
}
}
int MechManager::get_MechNum() // 获取数量
{
ifstream ifs(FILENAME, ios::in); // 打开文件 读
int id;
string name;
int dId;
int life;
int power;
int num = 0;
while (ifs >> id && ifs >> name && ifs >> life && ifs >> power &&ifs >> dId)
{
num++;
}
ifs.close();
return num;
}
int MechManager::If_Exist(int id) // 判断机甲是否存在
{
int index = -1;
for (int i = 0; i < this->m_MechNum; i++)
{
if (this->m_MechArray[i]->m_Id == id)
{
index = i;
break;
}
}
return index;
}
void MechManager::Del_Mech() // 删除机甲
{
if (this->m_FileIsEmpty)
{
cout << "文档不存在或者数据为空" << endl;
}
else
{
cout << "请输入删除机甲编号" << endl;
int id;
cin >> id;
int index = this->If_Exist(id);
if (index != -1)
{
for (; index < this->m_MechNum - 1; index++)
{
this->m_MechArray[index] = this->m_MechArray[index + 1];
}
cout << "删除该机甲操作成功" << endl;
this->Save();
this->m_MechNum--;
}
else
cout << "删除失败,无此编号机甲" << endl;
}
system("pause");
system("cls");
}
void MechManager::Show_Mech() // 展示机甲
{
int num = this->m_MechNum;
for (int i = 0; i < num; i++)
{
this->m_MechArray[i]->showInf();
}
}
void MechManager::Add_Mech()
// 判断加入数量
{ // 计算空间存放数量
// 开辟新空间 判断是否旧空间为NULL 添加新成员
// 释放旧空间 指向新空间
cout << "请输入添加机甲数量" << endl;
int add_Num = 0;
cin >> add_Num;
//this->m_MechNum=0 ; // 补充 初始化
// 先初始化 文件操作在修改
//this->m_MechArray = NULL;
if (add_Num > 0)
{
int newSize;
//计算空间大小
newSize = add_Num + this->m_MechNum;
//创建新空间
BaseMech** newSpace = new BaseMech*[newSize]; //数组指针 + new = > 二级指针
//将旧数据移到新空间 添加新数据
if (this->m_MechArray != NULL)
{
for (int i = 0; i < this->m_MechNum; i++)
{
newSpace[i] = this->m_MechArray[i];
}
}
for (int i = 0; i < add_Num; i++)
{
int id;
string name;
int dSelect;
int life;
int power;
cout << "请输入机甲编号" << endl;
cin >> id;
cout << "请输入机甲名称" << endl;
cin >> name;
cout << "请输入机甲生命值" << endl;
cin >> life;
cout << "请输入机甲战斗力" << endl;
cin >> power;
cout << "请输入机甲类型" << endl;
cout << "1 firemech" << endl;
cout << "2 icemech" << endl;
cout << "3 raymech" << endl;
cin >> dSelect;
BaseMech* basemech = NULL; // 全局变量
switch (dSelect)
{
case 1:
basemech = new FireMech( id, name, life, power,1); // 构造函数赋初值
break;
case 2:
basemech = new IceMech(id, name, life, power,2);
break;
case 3:
basemech = new RayMech(id, name, life, power,3);
break;
default:
break;
}
//插入到合适位置
newSpace[this->m_MechNum + i] = basemech;
}
//删除旧空间
delete[]this->m_MechArray;
// 记录新机甲数
this->m_MechNum = newSize;
//指向新空间
this->m_MechArray = newSpace;
//提示信息
cout << "成功添加" << add_Num << "个新机甲" << endl;
this->m_FileIsEmpty = false; // 防止之后判断出错
this->Save();
}
else
{
cout << "输入有误" << endl;
}
system("pause");
system("cls");
}
void MechManager::show_Meau()
{
cout << "********************************" << endl;
cout << "*****欢迎来到机甲管理系统*******" << endl;
cout << "**********0 退出系统***********" << endl;
cout << "**********1 增加机甲***********" << endl;
cout << "**********2 展示数据***********" << endl;
cout << "**********3 删除机甲***********" << endl;
cout << "**********4 修改数据***********" << endl;
cout << "**********5 查找机甲***********" << endl;
cout << "**********6 战斗排序***********" << endl;
cout << "**********7 机甲战斗***********" << endl;
cout << "**********8 机甲随机加点升级***" << endl;
// cout << "**********9 机甲复制***********" << endl;
cout << "**********9 数据清零***********" << endl;
}
void MechManager::exit_System()
{
exit(0); // 退出系统
}
/*
void test01()
{
IceMech i0;
FireMech f0;
cout << "请输入冰霜名字 攻击力 生命值";
cin >> i0.m_Name >> i0.nPower >> i0.nValuelife;
cout << "请输入火焰名字 攻击力 生命值";
cin >> f0.m_Name >> f0.nPower >> f0.nValuelife;
while (i0.nValuelife >= 0 && f0.nValuelife >= 0)
{
i0.attack(&f0);
f0.attack(&i0);
}
if (f0.nValuelife <= 0 && i0.nValuelife <= 0)
{
cout << "同归于尽" << endl;
}
else {
if (f0.nValuelife <= 0)
{
cout << "冰霜获胜" << endl;
}
if (i0.nValuelife <= 0)
{
cout << "火焰获胜" << endl;
}
}
cout << "程序结束" << endl;
}
*/
int main()
{
MechManager mm;
int choice = 0;
while (true)
{
mm.show_Meau();
cout << "请输入序号选择" << endl;
cin >> choice;
switch (choice)
{
case 0:
mm.exit_System();
break;
case 1:
mm.Add_Mech();
break;
case 2:
mm.Show_Mech();
break;
case 3:
mm.Del_Mech();
break;
case 4:
mm.Mod_Mech();
break;
case 5:
mm.Find_Mech();
break;
case 6:
mm.Sort_Mech();
break;
case 7:
mm.Fright_Mech();
break;
case 8:
mm.Improve_Mech();
break;
case 9:
mm.Clear_Mech();
break;
default:
break;
}
system("pause");
system("cls");
}
// test01();
return 0;
}