// Database.cpp
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <windows.h> //Sleep()
#include "Database.h"
int index = 0;
namespace emp
{
using std::cout;
using std::endl;
using std::cin;
using std::cerr;
using std::vector;
using std::ios_base;
using std::ifstream;
using std::fstream;
using std::ofstream;
////////////////////////////////////////////
// 初始化数据库
Database::Database()
{
}
/////////////////////////////////////////////
// 建立员工数据库
void Database::CreateDatabase()
{
system ("cls");
cout << "正在建立员工数据库,这可能要花费几分钟,请稍后";
for (size_t i = 0; i <= 5; i++)
{
Sleep (800);
cout << ".";
}
ofstream outfile;
outfile.open ("c:\\EmployeeDatabase.dat", ios_base::out | ios_base::binary);
if (!outfile.is_open())
{
cerr << "\a建立员工数据库失败,请检察其它故障!" << endl;
cout << "------------------------------------" << endl;
for (size_t i = 1; i <=5; i++)
{
Sleep (800);
cout << "5秒将自动退出! " << "[" << i++ << "]" << "\r";
}
exit(1);
}
cout << endl << "建立员工数据库成功!";
Sleep (800);
outfile.close();
outfile.clear();
system ("cls");
return;
}
////////////////////////////////////////////
// 录入员工数据库
void Database::Input_Database()
{
system ("cls");
fstream iofile;
iofile.open ("c:\\EmployeeDatabase.dat", ios_base::in | ios_base::binary);
if (!iofile.is_open())
{
cerr << "\a链接员工数据库失败,请确认已成功建立数据库!";
cin.get();
system ("cls");
return ;
}
else
{
cout << "正在链接到数据库,请稍后";
for (size_t i = 0; i <= 5; i++)
{
Sleep(800);
cout << ".";
}
cout << endl;
cout << "已成功链接到数据库!" << endl;
iofile.seekg(0);
while (iofile.read ((char *) &employee[index], sizeof (Employee)))
{
index++;
}
iofile.close();
iofile.clear();
if (index >= maxemployee)
{
cout << "数据库记录已满!";
cin.get();
system ("cls");
return ;
}
iofile.open ("c:\\EmployeeDatabase.dat", ios_base::out | ios_base::app
| ios_base::binary);
cout << "--------------------" << endl;
cout << " 员工信息登记表" << endl;
cout << "--------------------" << endl;
while (index < maxemployee)
{
employee[index].Input_Employee();
iofile.write((char *) &employee[index], sizeof (Employee));
cout << "录入下一位(y/n): ";
char ch;
cin.get(ch).get();
if ('y' == ch)
index++;
else
break;
}
iofile.close();
iofile.clear();
system ("cls");
}
}
///////////////////////////////////////////////
// 修改数据库内容
void Database::Mod_Database()
{
system ("cls");
fstream iofile;
iofile.open ("c:\\EmployeeDatabase.dat", ios_base::in
| ios_base::out | ios_base::binary);
if (!iofile.is_open())
{
cerr << "链接员工数据库失败,请确认已成功建立数据库!";
cin.get();
system ("cls");
return ;
}
else
{
cout << "正在链接数据库,请稍后";
for (size_t i = 0; i < 5; i++)
{
Sleep(800);
cout << ".";
}
cout << endl;
cout << "已成功链接到数据库!" << endl;
cout << "请输入需要修改的员工姓名: ";
char name[20];
cin.getline(name, 19);
int index = 0; //在数据库中定位文件位置
iofile.seekg(0);
while (iofile.read ((char *) &employee[index], sizeof (Employee)))
{
if (!strcmp (name, employee[index].getempname()))
{
iofile.close();
iofile.clear();
iofile.open ("c:\\EmployeeDatabase.dat", ios_base::out | ios_base::in
| ios_base::binary);
iofile.seekp (index * sizeof (Employee), ios_base::beg);
cout << "请重新输入数据!" << endl;
employee[index].Input_Employee();
iofile.write ((char *) &employee[index], sizeof (Employee));
cout << "修改成功! " << endl;
iofile.close();
iofile.clear();
cin.get();
system ("cls");
return ;
}
index++;
}
iofile.close();
iofile.clear();
cout << "在数据库没有搜索到" << name << "的相关信息" << endl;
cin.get();
system ("cls");
return ;
}
}
////////////////////////////////////////////////
// 搜索指定员工的相关信息
void Database::Sea_Database()
{
system ("cls");
ifstream infile;
infile.open ("c:\\EmployeeDatabase.dat", ios_base::in | ios_base::binary);
if (!infile.is_open())
{
cerr << "链接员工数据库失败,请确认已成功建立数据库!";
cin.get();
system ("cls");
return ;
}
else
{
cout << "正在链接员工数据库,请稍后";
for (size_t i = 0; i < 5; i++)
{
Sleep(800);
cout << ".";
}
cout << endl;
cout << "已成功链接到员工数据库!" << endl;
cout << "请输入需要搜索的关建字(姓名有关): ";
char name[20];
cin.getline (name, 19);
name[19] = '\0';
int index = 0;
int fcount = 1;
vector <int> count; //记录搜索到的员工个数
infile.seekg(0);
while (infile.read ((char *) &employee[index], sizeof (Employee)))
{
if (strstr (employee[index].getempname(), name))
{
count.push_back(index); //保存搜索到的下标
cout << "当前搜索到 " << "[ " << fcount++ << " ]"
<< " 条与" << name << "的相关信息!" << "\r";
index++;
Sleep(500); //休眠一会
continue;
}
index++;
}
infile.close();
infile.clear();
if (fcount == 1)
{
cout << "没有搜索到与" << name << "的相关信息!" << endl;
cin.get();
system ("cls");
return ;
}
else
{
cout << endl << "----------------------------------" << endl;
for (vector<int>::iterator p = count.begin(); p != count.end(); p++)
{
employee[*p].View_Employee();
}
cin.get();
system ("cls");
return ;
}
}
}
///////////////////////////////////////////////////////
// 删除数据库指定的员工信息
void Database::Del_Database()
{
system ("cls");
fstream iofile;
iofile.open ("c:\\EmployeeDatabase.dat", ios_base::in | ios_base::out
| ios_base::binary);
if (!iofile.is_open())
{
cerr << "不能链接到员工数据库,请确认已成功建立!";
cin.get();
system ("cls");
return ;
}
else
{
cout << "正在链接员工数据库,请稍后";
for (size_t i = 0; i < 5; i++)
{
Sleep (800);
cout << ".";
}
cout << endl <<"已成功链接到员工数据库!" << endl;
iofile.seekg(0);
cout << "请输入要删除的员工姓名:";
char name[20];
cin.getline (name, 19);
int index = 0;
while (iofile.read ((char *) &employee[index], sizeof (Employee)))
{
if (!strcmp (name, employee[index].getempname()))
{
while (iofile)
{
iofile.seekg((index + 1) * sizeof (Employee));
if (!(iofile.read ((char *) &employee[index + 1], sizeof (Employee))))
{
iofile.close();
iofile.clear();
system ("cls");
return ;
}
iofile.seekp(index * sizeof (Employee));
iofile.write ((char *) &employee[index + 1], sizeof (Employee));
index++;
}
iofile.close();
iofile.clear();
system ("cls");
return ;
}
index++;
}
iofile.close();
iofile.clear();
cout << "数据库中没有搜索到" << name << "的相关信息!" << endl;
system ("cls");
return ;
}
}
//////////////////////////////////////////////////////
// 显示数所有数据库信息
void Database::Show_Database()
{
system ("cls");
ifstream infile;
infile.open ("c:\\EmployeeDatabase.dat", ios_base::in | ios_base::binary);
if (!infile.is_open())
{
cerr << "不能链接到员工数据库,请确认已成功建立!";
cin.get();
system ("cls");
return ;
}
else
{
cout << "正在链接数据库,请稍后";
for (size_t i = 0; i < 5; i++)
{
Sleep(800);
cout << ".";
}
cout << endl;
cout << "已成功链接到员工数据库!" << endl;
infile.seekg(0);
int index = 0;
cout << "------------------------------" << endl;
cout << " 以下是数据库搜索到的所有信息!" << endl;
cout << "------------------------------" << endl;
while (infile.read ((char *) &employee[index], sizeof (Employee)))
{
employee[index].View_Employee();
index++;
if (!(index % 3))
{
cout << "---------------------------" << endl;
system ("pause");
continue;
}
}
if (!index)
{
cout << "数据库没有文件,可能已损坏或不存在!";
infile.close();
infile.clear();
cin.get();
system ("cls");
return ;
}
}
infile.close();
infile.clear();
cin.get();
system ("cls");
return ;
}
}