功能描述:将文件中的内容读取到程序中
构造函数初始化数据的情况分为三种:
1,第一次使用,文件未创建
2,文件存在,但没有数据
3,文件存在,并且存在所有数据
在StatisticalManage.h中添加新的成员属性m_FileIsEmpty标志文件是否为空
//标志文件是否为空
bool m_FileIsEmpty;
修改StatisticalManage.cpp中构造函数代码
StatisticalManage::StatisticalManage()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
this->m_EmpNum = 0; //初始化人数
this->m_EmpArray = NULL; //初始化数组
this->m_FileIsEmpty = true; //初始化文件为空标志
ifs.close();//关闭文件
return;
}
}
在StatisticalManage.cpp中构造函数中追加下方代码
//文件存在,并且记录为空
char ch;
ifs >> ch;
if (ifs.eof())
{
cout << "文件为空" << endl;
this->m_EmpNum = 0; //初始化人数
this->m_EmpArray = NULL; //初始化数组
this->m_FileIsEmpty = true; //初始化文件为空标志
ifs.close();//关闭文件
return;
}
在add_Emp()成员函数中添加
this->m_FileIsEmpty = false;
在头文件中添加成员函数int get_EmpNum()
//统计数量
int get_Num();
在StatisticalManage.cpp中实现
int StatisticalManage::get_Num()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
string date;
int raiseNumber;
int totalNumber;
int cureNumber;
int deathNumber;
int regionId;
int num = 0;
while (ifs >> date && ifs >> raiseNumber && ifs >> totalNumber && ifs >> cureNumber
&& ifs >> deathNumber && ifs >> regionId)
{
num++;
}
ifs.close();
return num;
}
在构造函数中继续追加代码
int num = this->get_Num();
//cout << "数据个数为:" << num << endl;
this->m_EmpNum = num;
头文件中添加成员函数void init_Emp();
并且实现
void StatisticalManage::init_Emp()
{
ifstream ifs;
ifs.open(FILENAME,ios::in);
string date;
int raiseNumber;
int totalNumber;
int cureNumber;
int deathNumber;
int regionId;
int index = 0;
while (ifs >> date && ifs >> raiseNumber && ifs >> totalNumber && ifs >> cureNumber
&& ifs >> deathNumber && ifs >> regionId)
{
Region *region = NULL;
if (regionId == 1)
{
region = new Hubei(date, raiseNumber, totalNumber, cureNumber, deathNumber, regionId);
}
else if (regionId == 2)
{
region = new Guangdong(date, raiseNumber, totalNumber, cureNumber, deathNumber, regionId);
}
else
{
region = new Zhejiang(date, raiseNumber, totalNumber, cureNumber, deathNumber, regionId);
}
//存放再数组中
this->m_EmpArray[index] = region;
index++;
}
}
构造函数中继续追加代码
this->m_EmpArray = new Region *[this->m_EmpNum];
init_Emp(); //初始化统计数量
在头文件中添加成员函数void show_Emp();
在StatisticalManage.cpp中实现
void StatisticalManage::show_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
for (int i = 0; i < this->m_EmpNum; i++)
{
this->m_EmpArray[i]->showInfo();
}
}
system("pause");
system("cls");
}
在头文件中添加成员函数 void del_Emp();
//删除数据
void del_Emp();
在头文件中添加成员函数 int isExist(string date, int regionId);
/根据日期判断和省份编号判断数据是否存在,若存在就返回数据在数组中的位置,不存在就返回-1
在StatisticalManage.cpp中实现isExist函数
int StatisticalManage::isExist(string date, int regionId)
{
int index = -1;
for (int i = 0; i < this->m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_date == date && this->m_EmpArray[i]->m_regionId == regionId)
{
index = i;
break;
}
}
return index;
}
在StatisticalManage.cpp中实现del_Emp函数
void StatisticalManage::del_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入想要删除的日期与省份:" << endl;
string date;
cin >> date;
cout << "1,湖北" << endl;
cout << "2,广东" << endl;
cout << "3,浙江" << endl;
int regionid;
cin >> regionid;
int index = this->isExist(date, regionid);
if (index != -1)
{
for (int i = index; i < this->m_EmpNum - 1; i++)
{
this->m_EmpArray[i] = this->m_EmpArray[i + 1];
}
this->m_EmpNum--;
this->save();
cout << "删除成功!" << endl;
}
else
{
cout << "删除失败,未找到该数据" << endl;
}
}
system("pause");
system("cls");
}
在头文件中添加成员函数void mod_Emp();
//修改数据
void mod_Emp();
在StatisticalManage.cpp中实现
void StatisticalManage::mod_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入想要修改的日期与省份:" << endl;
string date;
cin >> date;
cout << "1,湖北" << endl;
cout << "2,广东" << endl;
cout << "3,浙江" << endl;
int regionid;
cin >> regionid;
int ret = this->isExist(date, regionid);
if (ret != -1)
{
delete this->m_EmpArray[ret];
cout << "查到身份编号为:" << regionid << "日期为:" << date << "的数据" << endl;
//日期
string date;
//新增确诊人数
int raiseNumber;
//累计确诊人数
int totalNumber;
//累计治愈人数
int cureNumber;
//累计死亡人数
int deathNumber;
int dSelect;
cout << "请输入新的日期:" << endl;
cin >> date;
cout << "请输入新的新增确诊人数" << endl;
cin >> raiseNumber;
cout << "请输入新的累计确诊人数" << endl;
cin >> totalNumber;
cout << "请输入新的累计治愈人数" << endl;
cin >> cureNumber;
cout << "请输入新的累计死亡人数" << endl;
cin >> deathNumber;
cout << "请选择省份:" << endl;
cout << "1,湖北" << endl;
cout << "2,广东" << endl;
cout << "3,浙江" << endl;
cin >> dSelect;
Region * region = NULL;
switch (dSelect)
{
case 1: //湖北
region = new Hubei(date, raiseNumber, totalNumber, cureNumber, deathNumber, 1);
break;
case 2: //广东
region = new Guangdong(date, raiseNumber, totalNumber, cureNumber, deathNumber, 2);
break;
case 3: //浙江
region = new Zhejiang(date, raiseNumber, totalNumber, cureNumber, deathNumber, 3);
break;
default:
break;
}
//更改数据到数组中
this->m_EmpArray[ret] = region;
cout << "修改成功!" << endl;
//保存到文件
this->save();
}
else
{
cout << "修改失败,查无此数据!" << endl;
}
}
system("pause");
system("cls");
}
功能描述:提供三种查找方式,一种按照日期查找,一种按照省份编号查找,一种按照日期+省份编号查找
在头文件中添加成员函数void find_Emp()
//查找数据
void find_Emp();
在StatisticalManage.cpp中实现
void StatisticalManage::find_Emp()
{
if (this->m_FileIsEmpty)
{
cout << "文件不存在或记录为空!" << endl;
}
else
{
cout << "请输入查找方式:" << endl;
cout << "1,按日期查找" << endl;
cout << "2,按省份编号查找" << endl;
cout << "3,按省份编号+日期查找" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
string date;
cout << "请输入查找的日期:" << endl;
cin >> date;
bool flag = false;
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_date == date)
{
cout << "查找成功,日期为:" << m_EmpArray[i]->m_date
<< "的数据如下" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
//查无此数据
cout << "查找失败,查无此数据" << endl;
}
}
else if (select == 2)
{
int regionid;
cout << "请输入省份编号:" << endl;
cout << "1,湖北" << endl;
cout << "2,广东" << endl;
cout << "3,浙江" << endl;
cin >> regionid;
bool flag = false; //查找的标志
for (int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray[i]->m_regionId == regionid)
{
cout << "查找成功,省份编号为:" << m_EmpArray[i]->m_regionId
<< "的数据如下" << endl;
flag = true;
this->m_EmpArray[i]->showInfo();
}
}
if (flag == false)
{
//查无此数据
cout << "查找失败,查无此数据" << endl;
}
}
else if (select == 3)
{
cout << "请输入想要查找的日期与省份编号:" << endl;
string date;
cin >> date;
cout << "1,湖北" << endl;
cout << "2,广东" << endl;
cout << "3,浙江" << endl;
int regionid;
cin >> regionid;
int ret = this->isExist(date, regionid);
if (ret != -1)
{
cout << "查找成功!该数据信息如下:" << endl;
this->m_EmpArray[ret]->showInfo();
}
else
{
cout << "查找失败,查无此人!" << endl;
}
}
else
{
cout << "输入选项有误!" << endl;
}
}
system("pause");
system("cls");
}
在头文件中添加成员函数void clean_File();
//清空文件数据
void clean_Emp();
在StatisticalManage.cpp中实现
void StatisticalManage::clean_Emp()
{
cout << "确认清空?" << endl;
cout << "1,确认" << endl;
cout << "2,返回" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
//打开模式,ios::trunc 如果村子啊,删除文件并重新创建
ofstream ofs(FILENAME, ios::trunc);
ofs.close();
if (this->m_EmpArray != NULL)
{
for(int i = 0; i < m_EmpNum; i++)
{
if (this->m_EmpArray != NULL)
{
delete this->m_EmpArray[i];
}
}
this->m_EmpNum = 0;
delete[] this->m_EmpArray;
this->m_EmpArray = NULL;
this->m_FileIsEmpty = true;
}
cout << "清空成功!" << endl;
}
system("pause");
system("cls");
}