c++简易员工管理系统(从txt文件读取数据到结构体)
1.步骤
请用Visual Studio 编写一个控制台程序<程序名Members.exe>,步骤如下
**第一阶段:**依次输入姓名,性别,工号,电话,然后即时的显示出来。
**第二阶段:**把输入信息以下面格式保存在文本中,并且可以增加人员信息并保存:这个阶段程序完成后请存入6名以上同仁信息。
Index 1
Name: Steven
Sex: Male
ID: W0104572
Number: 63135
Index 2
Name: XXX
Sex: XXX
ID: XXX
Number: XXX
…
Index n
…
**第三阶段:**查询人员信息:
输入"Members.exe -find Index1",“Members.exe -find Steven”, “Members.exe -find 4572”, “Members.exe -find 63135” 均直接显示这个人员的所有信息。
输入"Members.exe -find Male" 即显示出Sex为Male 的所有人信息。
**第四阶段:**把所有人员信息按照ID的大小排序。并把排序后的结果保存在原文本里。
2.功能模块展示
3.代码
members.cpp:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
struct Members
{
char IndexN[10];
char Name[10];
char Sex[10];
char ID[10];
char Number[11];
};
const int maxN = 100;
int currentN; //全局变量,记录当前人数
void Input(Members* person, int num)
{
int i = 0;
string str;
if (num == 0) {
while (1)
{
cout << "IndexN:";
cin >> person[i].IndexN;
cout << "Name:";
cin >> person[i].Name;
cout << "Sex:";
cin >> person[i].Sex;
cout << "ID:";
cin >> person[i].ID;
cout << "Number:";
cin >> person[i].Number;
i++;
num = i;
currentN = num;
cout << "继续输入人员信息请按y/Y,否则输出除y/Y的任意数退出输入";
cout << "/n";
cin >> str;
if (i == maxN)
{
cout << "人数已达到" << maxN << "上限,无法继续输入!" << endl;
return;
}
if (str[0] == 'y' || str[0] == 'Y')
{
continue;
}
else
{
break;
}
}
}
else if (num == maxN)
{
cout << "人数已到达" << maxN << "上限,无法继续输入!" << endl;
return;
}
else
{
i = num;
while (1)
{
cout << "IndexN:";
cin >> person[i].IndexN;
cout << "Name:";
cin >> person[i].Name;
cout << "Sex:";
cin >> person[i].Sex;
cout << "ID:";
cin >> person[i].ID;
cout << "Number:";
cin >> person[i].Number;
i++;
num = i;
cout << "继续输入人员信息请按y/Y,否则输出除y/Y的任意数退出输入";
cout << "\n";
cin >> str;
if (i == maxN)
{
cout << "人数已达到" << maxN << "上限,无法继续输入!" << endl;
break;
}
if (str[0] == 'y' || str[0] == 'Y')
{
continue;
}
else
{
break;
}
}
}
}
void writePerson(Members* person, int num)
{
ofstream ofs;
ofs.open("C:/Users/1/Desktop/fileFirstWeek/Memberend/text.txt", ios::out | ios::app);
if (!ofs)
{
cout << "Open file error" << endl;
return;
}
for (int i = 0; i < currentN; i++)
{
ofs << person[i].IndexN << "\n" << person[i].Name << "\n" << person[i].Sex << "\n" << person[i].ID << "\n" << person[i].Number;
ofs << endl;
}
ofs << "\n";
ofs.close();
return;
}
//从txt文件读取数据到结构体
void readPerson(Members* person, int num)
{
int n = 0;
ifstream ifs;
ifs.open("C:/Users/1/Desktop/file/FirstWeek/Memberend/text.txt", ios::in);
if (!ifs)
{
cout << "Open file error" << endl;
return;
}
while (!ifs.eof() && n < maxN)
{
ifs.getline(person[n].IndexN, sizeof(person[n].IndexN));
ifs.getline(person[n].Name, sizeof(person[n].Name));
ifs.getline(person[n].Sex, sizeof(person[n].Sex));
ifs.getline(person[n].ID, sizeof(person[n].ID));
ifs.getline(person[n].Number, sizeof(person[n].Number));
n++;
}
// num += n;
currentN = n;
cout << "read file success" << endl;
ifs.close();
return;
}
//结构体遍历
void display(Members* person, int num)
{
if (currentN == 0)
{
cout << "当前暂无人员信息,请添加" << endl;
}
for (int i = 0; i < currentN; i++)
{
cout << person[i].IndexN << "\n" << person[i].Name << "\n" << person[i].Sex << "\n" << person[i].ID << "\n" << person[i].Number;
cout << endl;
}
}
//查询
//查询人员信息indexN
void SearchIndexN(Members* person, int num)
{
string index;
cout << "输入需要查询的INdexN:";
cin >> index;
bool flag = 0;
int pos = 0;
for (int i = 0; i < currentN; i++)
{
if (person[i].IndexN == index)
{
flag = 1;
pos = i;
break;
}
}
if (flag == 0)
{
cout << "未查询到人员信息" << "\n";
}
else
{
cout << "\n index: " << person[pos].IndexN << "\n Name: " << person[pos].Name << "\n Sex: "
<< person[pos].Sex << "\n ID: " << person[pos].ID << "\n Number: " << person[pos].Number;
cout << endl;
}
return;
}
// 据姓名查询
//void SearchName(Members* person, int num, char* FullName)
void SearchName(Members* person, int num)
{
string name;
//name = FullName;
cout << "输入需要查询的名字:";
cin >> name;
bool flag = 0;
int pos = 0;
for (int i = 0; i < currentN; i++)
{
if (person[i].Name == name)
{
flag = 1;
pos = i;
break;
}
}
if (flag == 0)
{
cout << "未查询到人员信息" << "\n";
}
else
{
cout << "\n index: " << person[pos].IndexN << "\n Name: " << person[pos].Name << "\n Sex: "
<< person[pos].Sex << "\n ID: " << person[pos].ID << "\n Number: " << person[pos].Number << endl;
}
return;
}
// 据性别查询
void SearchSex(Members* person, int num)
{
string gender;
cout << "输入需要查询的性别(male/female):";
cin >> gender;
bool flag = 0;
int pos = 0;
for (int i = 0; i < currentN; i++)
{
if (person[i].Sex == gender)
{
flag = 1;
pos = i;
cout << "\n index: " << person[pos].IndexN << "\n Name: " << person[pos].Name << "\n Sex: "
<< person[pos].Sex << "\n ID: " << person[pos].ID << "\n Number: " << person[pos].Number << endl;
}
}
if (flag == 0)
{
cout << "未查询到人员信息" << "\n";
}
return;
}
// 根据员工号查询
void SearchID(Members* person, int num)
{
string id;
cout << "输入需要查询的工号:";
cin >> id;
bool flag = 0;
int pos = 0;
for (int i = 0; i < currentN; i++)
{
if (person[i].ID == id)
{
flag = 1;
pos = i;
break;
}
}
if (flag == 0)
{
cout << "未查询到人员信息" << "\n";
}
else
{
cout << "\n index: " << person[pos].IndexN << "\n Name: " << person[pos].Name << "\n Sex: "
<< person[pos].Sex << "\n ID: " << person[pos].ID << "\n Number: " << person[pos].Number << endl;
}
return;
}
// 根据电话号码查询
void SearchNumber(Members* person, int num)
{
string tel;
cout << "输入需要查询的号码:";
cin >> tel;
bool flag = 0;
int pos = 0;
for (int i = 0; i < currentN; i++)
{
if (person[i].Number == tel)
{
flag = 1;
pos = i;
break;
}
}
if (flag == 0)
{
cout << "未查询到人员信息" << "\n";
}
else
{
cout << "\n index: " << person[pos].IndexN << "\n Name: " << person[pos].Name << "\n Sex: "
<< person[pos].Sex << "\n ID: " << person[pos].ID << "\n Number: " << person[pos].Number;
cout << endl;
}
return;
}
//插入排序,按照工号ID排序
void InsertionSort(Members* person, int num)
{
for (int i = 1; i < currentN; ++i)
{
for (int j = 0; j < i; ++j)
{
if (strcmp(person[j].ID, person[i].ID) < 0)
{
Members temp = person[i];//这样的话,根据工号,调整工号所在对象的位置,整个Member对象 都会随着学号的升序而跟着改变
for (int k = i; k > j; --k)
person[k] = person[k - 1];
person[j] = temp;
}
}
}
}
//清空文本信息
void fileEmpty()
{
fstream file;
fstream open("C:/Users/1/Desktop/file/FirstWeek/Memberend/text.txt", ios::out | ios::binary);
cout << "文本内容已成功清除内容" << endl;
return;
}
//帮助
void help()
{
//cout << "当前输入命令错误" << endl;
cout << "正确输入命令如下:" << endl;
cout << "inputInfo:手动输入员工信息" << endl;
cout << "findIndex:根据index索引员工信息" << endl;
cout << "findName:根据姓名查找员工信息" << endl;
cout << "findSex:根据性别查找员工信息" << endl;
cout << "findID:根据工号查找员工信息" << endl;
cout << "findNumber:根据电话号码查询员工信息" << endl;
cout << "sort:排序" << endl;
cout << "fileEmpty:清空员工信息" << endl;
cout << "display:打印当前员工列表" << endl;
return;
}
int main(int argc, char* argv[])
{
struct Members person[maxN];
readPerson(person, currentN);
// cout << "未知值" << person->Name << endl;
if (argc == 2)
{
if (strcmp(argv[1], "inputInfo") == 0)
{
Input(person, currentN);
writePerson(person, currentN);
display(person, currentN);
system("pause");
return -1;
}
else if (strcmp(argv[1], "findIndex") == 0)
{
SearchIndexN(person, currentN);
}
else if (strcmp(argv[1], "findName") == 0)
{
SearchName(person, currentN);
}
else if (strcmp(argv[1], "findSex") == 0)
{
SearchSex(person, currentN);
}
else if (strcmp(argv[1], "findID") == 0)
{
SearchID(person, currentN);
}
else if (strcmp(argv[1], "findNumber") == 0)
{
SearchNumber(person, currentN);
}
else if (strcmp(argv[1], "sort") == 0)
{
//readPerson(person, currentN);
cout << currentN << endl;
cout << "排序前" << endl;
display(person, currentN);
cout << "\n";
InsertionSort(person, currentN);
cout << "排序后" << endl;
cout << "\n";
display(person, currentN);
// writePerson(person, currentN);
system("pause");
return -1;
}
else if (strcmp(argv[1], "display") == 0)
{
display(person, currentN);
system("pause");
return -1;
}
else if (strcmp(argv[1], "fileEmpty") == 0)
{
fileEmpty();
return -1;
}
else if (strcmp(argv[1], "help") == 0)
{
help();
}
else
{
cout << "输入错误,请按照showhelp提示输入" << endl;
help();
return -1;
}
}
else
{
help();
}
}
设计过程中遇到最大的问题,如何按着设计的样式从txt文本中读取信息到结构体。
难点:如何从存储员工信息的txt文本,读取数据到结构体。
解决方案:
在存储时,规范存储数据格式,在读取时候,读取每一行的信息到不同的结构体成员,并且使用ifs.eof()函数判定是否以及读取全部文本完成。
设计源码如下:
while (!ifs.eof() && n < maxN)
{
ifs.getline(person[n].IndexN, sizeof(person[n].IndexN));
ifs.getline(person[n].Name, sizeof(person[n].Name));
ifs.getline(person[n].Sex, sizeof(person[n].Sex));
ifs.getline(person[n].ID, sizeof(person[n].ID));
ifs.getline(person[n].Number, sizeof(person[n].Number));
n++;
}
编程中主函数部分测试代码:
/*-----------------以下为编程中测试部分功能代码------------*/
/*else if (argc == 3)
{
if (strcmp(argv[1], "find") == 0)
{
if (strcmp(argv[2], person->Name) == 0)
{
cout << person->Name << endl;
SearchName(person, currentN, person->Name);
system("pause");
return -1;
}
else if (strcmp(argv[2], person->IndexN) == 0)
{
SearchIndexN(person, currentN);
system("pause");
return -1;
}
else
{
cout << "输入错误1,请按照showhelp提示输入" << endl;
return -1;
}
}
else
{
cout << "输入错误2,请按照showhelp提示输入" << endl;
return -1;
}
}
else
{
cout << "输入错误3,请按照showhelp提示输入" << endl;
return -1;
}
cout << "未知值" << person->Name << endl;*/
//--------------------------------------------
//控制台查找
//readPerson(person, currentN);
//if(argc == 2)
//{
// if (strcmp(argv[1], "input") == 0)
// {
//写入信息到txt文件
//Input(person, currentN);
//writePerson(person, currentN);
//display(person, currentN);
// }
//
//控制台查找
/* else if (strcmp(argv[1], "findIndex") == 0)
{
SearchIndexN(person, currentN);
}
else if (strcmp(argv[1], "findName") == 0)
{
SearchName(person, currentN);
}
else if (strcmp(argv[1], "findSex") == 0)
{
SearchSex(person, currentN);
}
else if (strcmp(argv[1], "findID") == 0)
{
SearchID(person, currentN);
}
else if (strcmp(argv[1], "findNumber") == 0)
{
SearchNumber(person, currentN);
}*/
//排序
//else if (strcmp(argv[1], "sort") == 0)
//{
// //排序后写入原文本
// /*readPerson(person, currentN);
// cout << currentN << endl;
// cout << "排序前" << endl;
// display(person, currentN);
// cout << "\n";
// InsertionSort(person, currentN);
// cout << "排序后" << endl;
// cout << "\n";
// display(person, currentN);
// writePerson(person, currentN);*/
//}
// else
// {
// help();
//
// }
//}
//else
//{
// cout << "error" << endl;
//}
//----------------------------------
//排序后写入原文本
/*readPerson(person, currentN);
cout << currentN << endl;
cout << "排序前" << endl;
display(person, currentN);
cout << "\n";
InsertionSort(person, currentN);
cout << "排序后" << endl;
cout << "\n";
display(person, currentN);
writePerson(person, currentN);*/
这段代码参考意义很小,但是在编程过程中,对每个功能函数都进行了一个测试,或是对一部分进行测试,读者可忽略。
4.运行结果展示
代码使用c++主函数控制台,需要在.exe文件目录输入cmd按回车键进入进入控制台。
进入控制台后,运行*==memberend.exe*==文件(可执行文件.exe,可使用tab键进行切换文件找到)以下展示部分运行结果示例,感兴趣其它执行结果,可自行运行代码。
根据性别进行查询,Menberend.exe findSex,其它指令格式也是如此此,如输入员工信息Menberend.exe inputInfo
排序,这里使用的是插入排序,时间复杂度为O(n^2),读者有其它更好的方式的话,还请不吝赐教。
看到这里就结束了,祝你今天也是如此美好。