逆向软件工程

一、来源
该项目来源于csdn上的一位博主在大学时期写的c++大作业
二、代码与运行结果截图

点击查看代码
#include<iostream>
#include<cstring>
#include<fstream>//新增了office类、重载运算符、查看文件信息函数 、根据名字删除信息 
#include<iomanip>
#pragma warning(disable : 4996)
using namespace std;
class Employee;
//定义全局变量,每个函数都要使用此链表 
Employee* head = NULL, * p1 = NULL, * p2 = NULL;//head记录头,p1记录当前,p2开辟 
class Employee {
private:
	int id;           //记录工号
	char name[10];    //记录名字 
	char sex[5];     //记录性别 
	char number[12];    //记录电话号码 
	char office[20];  //科室 
	double wage;     //记录工资 
public:
	Employee* next;  //链表节点
	Employee()  //无参构造 
	{}
	Employee(int id, char na[], char sex[], char num[], char of[], double wa);
	friend istream& operator>>(istream& cin, Employee& x);//重载运算符">>" 
	void SaveInfo(); //输出信息 
	char* get_name();//获取名字 
	int get_id();//获取工号 
	char* get_office();//获取科室
	double get_wage();//获取工资 
};
class office {
private:
	double sum = 0;//记录总工资 
	int count = 0;//记录该科室人数 
	char off[20]; //记录该科室名称 
public:
	office* next;
	office() //无参构造初始化 
	{
		strcpy(off, "  ");
	}
	office(double s, char o[]);//有参构造初始化 
	void display();//输出信息 
	char* get_off(); //获得科室名字 
	void add(double s);//新增该科室成员数据 
};
Employee::Employee(int id, char na[], char se[], char num[], char of[], double wa) //有参构造函数 
{
	strcpy(name, na);
	strcpy(sex, se);
	strcpy(number, num);
	strcpy(office, of);
	this->id = id;
	this->wage = wa;
}
istream& operator>>(istream& in, Employee& x)//重载运算符">>"
{
	//cout<<"请依次输入工号、名字、性别、电话号码、所在科室 、工资:"<<endl;
	in >> x.id >> x.name >> x.sex >> x.number >> x.office >> x.wage;
	return in;
}
void Employee::SaveInfo() {
	cout << "员工姓名:" << setw(10) << left << name << "性别:" << setw(10) << sex << "工号:" << setw(5) << id << endl;
	cout << "所在科室:" << setw(10) << office << "工资:" << setw(10) << wage << "电话号码:" << setw(15) << number << endl;
}
char* Employee::get_name() {
	return name;
}
char* Employee::get_office() {
	return office;
}
int Employee::get_id() {
	return id;
}
double Employee::get_wage() {
	return wage;
}
office::office(double s, char o[]) {
	sum += s;
	count++;
	strcpy(off, o);
}
void office::display() {
	cout << off << "科室的平均工资为:" << sum / count << endl;
}
char* office::get_off() {
	return off;
}
void office::add(double s) {
	sum += s;
	count++;
}
void menu_show() {
	cout << "***********************************************************" << 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 << "              ***    0.退出职工信息管理系统   ***          " << endl;
	cout << "              ***********************************          " << endl;
	cout << "请输入一个数字选择功能: ";
}
void ad()  //将链表数据存入文件 
{
	ofstream o;
	o.open("员工信息.dat", ios::out);
	if (!o)
	{
		cout << "员工信息文件打开失败!" << endl;
		return;
	}
	Employee* p = head;
	while (p)
	{
		o.write((char*)p, sizeof(Employee));
		p = p->next;
	}
	o.close();
}
void ap() //将文件信息读入链表 
{
	ifstream it;
	it.open("员工信息.dat", ios::in);
	if (!it)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	it.seekg(0, ios::end);
	int filelen, i = 1;
	filelen = it.tellg();
	it.seekg(0, ios::beg);
	while (it.tellg() != filelen)
	{
		p2 = new Employee();
		it.read((char*)p2, sizeof(Employee));
		if (i == 1)
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
	}
	//p1->next=NULL;  加上会运行错误 
	//head->SaveInfo();
	it.close();
	Employee* p = head; //遍历链表代码 
	if (head)
		while (p)
		{
			p->SaveInfo();
			p = p->next;
		}
	else cout << "暂无信息!" << endl;
}
void AddEmployee() {//1.增加职工信息函数
	cout << "请输入您要添加的职工数量:" << endl;
	int n, i = 1;
	cin >> n;
	Employee* p = head; //遍历链表代码 
	if (head)
		while (p->next)
		{
			p = p->next;
		}  //获取尾节点 ,经删除函数后,p1会变化,此处重新获取 
	p1 = p;
	while (i <= n) {
		p2 = new Employee();
		p2->next = NULL;
		cout << "请依次第" << i << "个输入工号、名字、性别、电话号码、所在科室 、工资:" << endl;
		cin >> *p2;
		if (!head)//头指针为空 
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
		cout << "添加信息成功!" << endl;
	};
	ad();//刷新文件 
	// head->next->SaveInfo();
	 //p1->SaveInfo();
	// p2->SaveInfo();
}

void DeleteEmployee()  //2.删除员工信息 
{
	if (!head) {
		cout << "没有可供删除的人员信息! 请先添加人员信息!" << endl;
		return;
	}
	cout << "输入名字删除输入0,输入工号删除输入1:" << endl;
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		cout << "请输入您要删除的员工工号:" << endl;
		int id, flag = 0;
		cin >> id;
		Employee* p = head; //遍历链表代码 
		if (head->get_id() == id)//头链表时 
		{
			cout << "删除信息成功!" << endl;
			flag = 1;
			head = head->next;
		}
		while (p->next)
		{
			if (p->next->get_id() == id)
			{
				Employee* pp = new Employee();
				pp = p->next;
				p->next = pp->next;
				delete pp;
				flag = 1;
				cout << "删除信息成功!" << endl;
				break; //删完之后退出 
			}
			p = p->next;
		}
		if (flag == 0) cout << "文件中不包含此人信息!" << endl;
	}
	else if (choice == 0) {
		cout << "请输入您要删除的员工姓名:" << endl;
		char* na = new char[10];
		cin >> na;
		int flag = 0;
		Employee* p = head; //遍历链表代码 
		if (strcmp(head->get_name(), na) == 0)//头链表时 
		{
			cout << "删除信息成功!" << endl;
			flag = 1;
			head = head->next;
		}
		while (p->next)
		{
			if (strcmp(p->next->get_name(), na) == 0)
			{
				Employee* pp = new Employee();
				pp = p->next;
				p->next = pp->next;
				delete pp;
				flag = 1;
				cout << "删除信息成功!" << endl;
				break; //删完之后退出 
			}
			p = p->next;
		}
		if (flag == 0) cout << "文件中不包含此人信息!" << endl;
		delete[]na;
	}
	//if(!head->next) head->next=p1; //**当只有表头时,链接起来 
	ad();//刷新文件 
}
void  UpdateEmployee()///修改职工信息函数
{
	if (!head) {
		cout << "没有可供修改的人员信息! 请先添加人员信息!" << endl;
		return;
	}
	cout << "输入名字修改输入0,输入工号修改输入1:" << endl;
	int choice;
	cin >> choice;
	if (choice == 0)
	{
		cout << "请输入要修改的人名字:" << endl;
		char* na = new char[10];
		cin >> na;
		Employee* p = head; //遍历链表代码 
		while (p)
		{
			if (strcmp(p->get_name(), na) == 0)
			{
				cout << "请依次输入修改后的工号、名字、性别、电话号码、所在科室 、工资:" << endl;
				cin >> *(p);
				cout << "修改信息成功!" << endl;
				break;
			}
			p = p->next;
		}
		if (!p) cout << "未查询到修改人信息!" << endl;
		delete[]na;
	}
	else if (choice == 1) {
		cout << "请输入要修改的人工号:" << endl;
		int* id = new int;
		cin >> *id;
		Employee* p = head; //遍历链表代码 
		while (p)
		{
			if (p->get_id() == *id)
			{
				cout << "请依次输入修改后的工号、名字、性别、电话号码、所在科室 、工资:" << endl;
				cin >> *(p);
				cout << "修改信息成功!" << endl;
				break;
			}
			p = p->next;
		}
		if (!p) cout << "未查询到修改人信息!" << endl;
		delete id;
	}
	// 
	ad();//刷新文件 
}
void reseachStudentByName()//4.按姓名查询职工信息函数
{
	if (!head) {
		cout << "查询失败! 请先添加人员信息!" << endl;
		return;
	}
	cout << "请输入要查询的人名字:" << endl;
	char* na = new char[10];
	cin >> na;
	Employee* p = head; //遍历链表代码 
	while (p)
	{
		if (strcmp(p->get_name(), na) == 0)
		{
			cout << na << "的个人信息如下:" << endl;
			p->SaveInfo();
			break;
		}
		p = p->next;
	}
	if (!p) cout << "未查询到此人信息!" << endl;
	delete[]na;
}
void reseachStudentByOffice()//按科室查询职工信息函数 
{
	if (!head) {
		cout << "查询失败! 请先添加人员信息!" << endl;
		return;
	}
	cout << "请输入要查询的科室名字:" << endl;
	char* of = new char[10];
	cin >> of;
	Employee* p = head; //遍历链表代码 
	int* flag = new int(0);
	while (p)
	{
		if (strcmp(p->get_office(), of) == 0)
		{
			if (*flag == 0) cout << "所查询的科室职工如下:" << endl;
			p->SaveInfo();
			(*flag)++;
		}
		p = p->next;
	}
	if (!(*flag)) cout << "未查询到该科室人员信息!" << endl;
	delete[]of;
	delete flag;
}
void add()//按科室统计职工平均工资函数 
{
	if (!head) {
		cout << "统计失败! 请先添加人员信息!" << endl;
		return;
	}
	office* head_ = NULL, * p1_ = NULL, * p2_ = NULL;//前三者同之前 
	Employee* p = head; //遍历链表代码 
	office* p_;
	int i = 1;
	while (p)
	{
		int f = 0;//作为标记 
		for (p_ = head_; p_; p_ = p_->next) //遍历链表head_ 
		{
			if (strcmp(p_->get_off(), p->get_office()) == 0)
			{
				p_->add(p->get_wage()); //添加 
				f = 1;
				break;
			}
		}
		if (f == 0)  //新增,存入链表head_ 
		{
			p2_ = new office(p->get_wage(), p->get_office());
			if (i == 1)
			{
				head_ = p1_ = p2_;
			}
			else
			{
				p1_->next = p2_;
			}
			p1_ = p2_;
			p1_->next = NULL;
			i++;
		}
		p = p->next;
	}
	p_ = head_; //遍历链表代码 
	while (p_)
	{
		p_->display();
		p_ = p_->next;
	}
}
void show() {
	int n;
	while (1) {
		menu_show();
		cin >> n;
		switch (n) {
		case (0): {
			exit(1);  //退出职工工资管理系统
		}
		case (1): {
			AddEmployee();//增加职工信息函数
			break;
		}
		case (2): {
			DeleteEmployee();//删除职工信息函数
			break;
		}
		case (3): {
			UpdateEmployee();//修改职工信息函数 
			break;
		}
		case(4): {
			reseachStudentByName();// 按姓名查询职工信息函数 
			break;
		}
		case(5): {
			reseachStudentByOffice();//按科室查询职工信息函数 
			break;
		}
		case (6): {
			add();//按科室统计职工平均工资函数 
			break;
		}
		case(7): {
			ap();//显示文件信息函数 
			break;
		}
		}
	}
}
int main() {
	//getdate(); 
	ofstream ot;
	ot.open("员工信息.dat", ios::out | ios::app);//1.文件不存在时创建文件2.存在时不覆盖原内容 
	if (!ot)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	//ot.write((char*)&a1,sizeof(Employee));
	ot.close();
	ifstream it;
	it.open("员工信息.dat", ios::in);
	if (!it)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	it.seekg(0, ios::end);
	int filelen, i = 1;
	filelen = it.tellg ();
	it.seekg(0, ios::beg);
	while (it.tellg() != filelen)
	{
		p2 = new Employee();
		it.read((char*)p2, sizeof(Employee));
		if (i == 1)
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
	}
	//head->SaveInfo();
	it.close();
	show();
}

#pragma warning(default : 4996)

运行结果截图
主界面

添加职工信息

修改职工信息

按姓名查询信息

按科室查询信息

计算科室平均薪资

查询现有职工信息

删除职工信息

文件存储

三、主要问题分析
该系统功能比较完善,包块增删改查以及计算平均薪资的功能,但是在使用这个系统时,会发现每次选择功能都会输出一遍菜单,显得繁杂,由此我将其改进为每使用一个功能时都会清屏一遍,这样会使系统看起来就像进入了新的界面一样,具体方法是在每个函数前加入清屏命令,并且在函数结尾加上_getch()函数,等待用户按下一个按键,再返回主菜单,另外,我添加了一个排序功能,可以按照工号、姓名、薪资三种方式进行排序
四、改进后的新代码

点击查看代码
#include<iostream>
#include<cstring>
#include<fstream>//新增了office类、重载运算符、查看文件信息函数 、根据名字删除信息 
#include<iomanip>
#include <conio.h> // 用于_getch()
#pragma warning(disable : 4996)
using namespace std;
class Employee;
//定义全局变量,每个函数都要使用此链表 
Employee* head = NULL, * p1 = NULL, * p2 = NULL;//head记录头,p1记录当前,p2开辟 
class Employee {
private:
	int id;           //记录工号
	char name[10];    //记录名字 
	char sex[5];     //记录性别 
	char number[12];    //记录电话号码 
	char office[20];  //科室 
	double wage;     //记录工资 
public:
	Employee* next;  //链表节点
	Employee()  //无参构造 
	{}
	Employee(int id, char na[], char sex[], char num[], char of[], double wa);
	friend istream& operator>>(istream& cin, Employee& x);//重载运算符">>" 
	void SaveInfo(); //输出信息 
	char* get_name();//获取名字 
	int get_id();//获取工号 
	char* get_office();//获取科室
	double get_wage();//获取工资 
	static bool CompareById(const Employee* a, const Employee* b) {
		return a->id < b->id;
	}

	static bool CompareByName(const Employee* a, const Employee* b) {
		return strcmp(a->name, b->name) < 0;
	}

	static bool CompareByWage(const Employee* a, const Employee* b) {
		return a->wage < b->wage;
	}
};
void QuickSort(Employee** arr, int low, int high, bool (*compare)(const Employee*, const Employee*)) {
	if (low < high) {
		Employee* pivot = arr[high];
		int i = (low - 1);

		for (int j = low; j <= high - 1; j++) {
			if (compare(arr[j], pivot)) {
				i++;
				swap(arr[i], arr[j]);
			}
		}
		swap(arr[i + 1], arr[high]);
		int pi = i + 1;

		QuickSort(arr, low, pi - 1, compare);
		QuickSort(arr, pi + 1, high, compare);
	}
}
class office {
private:
	double sum = 0;//记录总工资 
	int count = 0;//记录该科室人数 
	char off[20]; //记录该科室名称 
public:
	office* next;
	office() //无参构造初始化 
	{
		strcpy(off, "  ");
	}
	office(double s, char o[]);//有参构造初始化 
	void display();//输出信息 
	char* get_off(); //获得科室名字 
	void add(double s);//新增该科室成员数据 
};
Employee::Employee(int id, char na[], char se[], char num[], char of[], double wa) //有参构造函数 
{
	strcpy(name, na);
	strcpy(sex, se);
	strcpy(number, num);
	strcpy(office, of);
	this->id = id;
	this->wage = wa;
}
istream& operator>>(istream& in, Employee& x)//重载运算符">>"
{
	//cout<<"请依次输入工号、名字、性别、电话号码、所在科室 、工资:"<<endl;
	in >> x.id >> x.name >> x.sex >> x.number >> x.office >> x.wage;
	return in;
}
void Employee::SaveInfo() {
	cout << "员工姓名:" << setw(10) << left << name << "性别:" << setw(10) << sex << "工号:" << setw(5) << id << endl;
	cout << "所在科室:" << setw(10) << office << "工资:" << setw(10) << wage << "电话号码:" << setw(15) << number << endl;
}
char* Employee::get_name() {
	return name;
}
char* Employee::get_office() {
	return office;
}
int Employee::get_id() {
	return id;
}
double Employee::get_wage() {
	return wage;
}
office::office(double s, char o[]) {
	sum += s;
	count++;
	strcpy(off, o);
}
void office::display() {
	cout << off << "科室的平均工资为:" << sum / count << endl;
}
char* office::get_off() {
	return off;
}
void office::add(double s) {
	sum += s;
	count++;
}
void menu_show() {
	system("cls"); // 清屏
	cout << "***********************************************************" << 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 << "              ***    8.排序职工信息           ***          " << endl;
	cout << "              ***    0.退出职工信息管理系统   ***          " << endl;
	cout << "              ***********************************          " << endl;
	cout << "请输入一个数字选择功能: ";
}
void ad()  //将链表数据存入文件 
{
	ofstream o;
	o.open("员工信息.dat", ios::out);
	if (!o)
	{
		cout << "员工信息文件打开失败!" << endl;
		return;
	}
	Employee* p = head;
	while (p)
	{
		o.write((char*)p, sizeof(Employee));
		p = p->next;
	}
	o.close();
}
void ap() //将文件信息读入链表 
{
	ifstream it;
	it.open("员工信息.dat", ios::in);
	if (!it)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	it.seekg(0, ios::end);
	int filelen, i = 1;
	filelen = it.tellg();
	it.seekg(0, ios::beg);
	while (it.tellg() != filelen)
	{
		p2 = new Employee();
		it.read((char*)p2, sizeof(Employee));
		if (i == 1)
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
	}
	//p1->next=NULL;  加上会运行错误 
	//head->SaveInfo();
	it.close();
	Employee* p = head; //遍历链表代码 
	if (head)
		while (p)
		{
			p->SaveInfo();
			p = p->next;
		}
	else cout << "暂无信息!" << endl;
}
void AddEmployee() {//1.增加职工信息函数
	system("cls"); // 清屏
	cout << "请输入您要添加的职工数量:" << endl;
	int n, i = 1;
	cin >> n;
	Employee* p = head; //遍历链表代码 
	if (head)
		while (p->next)
		{
			p = p->next;
		}  //获取尾节点 ,经删除函数后,p1会变化,此处重新获取 
	p1 = p;
	while (i <= n) {
		p2 = new Employee();
		p2->next = NULL;
		cout << "请依次第" << i << "个输入工号、名字、性别、电话号码、所在科室 、工资:" << endl;
		cin >> *p2;
		if (!head)//头指针为空 
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
		cout << "员工信息添加成功!按下任意键返回主菜单..." << endl;
		_getch(); // 等待用户按下任意键
	};
	ad();//刷新文件 
	// head->next->SaveInfo();
	 //p1->SaveInfo();
	// p2->SaveInfo();
}

void DeleteEmployee()  //2.删除员工信息 
{
	system("cls"); // 清屏
	if (!head) {
		cout << "没有可供删除的人员信息! 请先添加人员信息!" << endl;
		return;
	}
	cout << "输入名字删除输入0,输入工号删除输入1:" << endl;
	int choice;
	cin >> choice;
	if (choice == 1)
	{
		cout << "请输入您要删除的员工工号:" << endl;
		int id, flag = 0;
		cin >> id;
		Employee* p = head; //遍历链表代码 
		if (head->get_id() == id)//头链表时 
		{
			cout << "删除信息成功!" << endl;
			flag = 1;
			head = head->next;
		}
		while (p->next)
		{
			if (p->next->get_id() == id)
			{
				Employee* pp = new Employee();
				pp = p->next;
				p->next = pp->next;
				delete pp;
				flag = 1;
				cout << "删除信息成功!" << endl;
				break; //删完之后退出 
			}
			p = p->next;
		}
		if (flag == 0) cout << "文件中不包含此人信息!" << endl;
	}
	else if (choice == 0) {
		cout << "请输入您要删除的员工姓名:" << endl;
		char* na = new char[10];
		cin >> na;
		int flag = 0;
		Employee* p = head; //遍历链表代码 
		if (strcmp(head->get_name(), na) == 0)//头链表时 
		{
			cout << "删除信息成功!" << endl;
			flag = 1;
			head = head->next;
		}
		while (p->next)
		{
			if (strcmp(p->next->get_name(), na) == 0)
			{
				Employee* pp = new Employee();
				pp = p->next;
				p->next = pp->next;
				delete pp;
				flag = 1;
				cout << "删除信息成功!" << endl;
				break; //删完之后退出 
			}
			p = p->next;
		}
		if (flag == 0) cout << "文件中不包含此人信息!" << endl;
		delete[]na;
	}
	//if(!head->next) head->next=p1; //**当只有表头时,链接起来 
	ad();//刷新文件 
	cout << "员工信息删除成功!按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void  UpdateEmployee()///修改职工信息函数
{
	system("cls"); // 清屏
	if (!head) {
		cout << "没有可供修改的人员信息! 请先添加人员信息!" << endl;
		return;
	}
	cout << "输入名字修改输入0,输入工号修改输入1:" << endl;
	int choice;
	cin >> choice;
	if (choice == 0)
	{
		cout << "请输入要修改的人名字:" << endl;
		char* na = new char[10];
		cin >> na;
		Employee* p = head; //遍历链表代码 
		while (p)
		{
			if (strcmp(p->get_name(), na) == 0)
			{
				cout << "请依次输入修改后的工号、名字、性别、电话号码、所在科室 、工资:" << endl;
				cin >> *(p);
				cout << "修改信息成功!" << endl;
				break;
			}
			p = p->next;
		}
		if (!p) cout << "未查询到修改人信息!" << endl;
		delete[]na;
	}
	else if (choice == 1) {
		cout << "请输入要修改的人工号:" << endl;
		int* id = new int;
		cin >> *id;
		Employee* p = head; //遍历链表代码 
		while (p)
		{
			if (p->get_id() == *id)
			{
				cout << "请依次输入修改后的工号、名字、性别、电话号码、所在科室 、工资:" << endl;
				cin >> *(p);
				cout << "修改信息成功!" << endl;
				break;
			}
			p = p->next;
		}
		if (!p) cout << "未查询到修改人信息!" << endl;
		delete id;
	}
	// 
	ad();//刷新文件 
	cout << "按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void reseachStudentByName()//4.按姓名查询职工信息函数
{
	system("cls"); // 清屏
	if (!head) {
		cout << "查询失败! 请先添加人员信息!" << endl;
		return;
	}
	cout << "请输入要查询的人名字:" << endl;
	char* na = new char[10];
	cin >> na;
	Employee* p = head; //遍历链表代码 
	while (p)
	{
		if (strcmp(p->get_name(), na) == 0)
		{
			cout << na << "的个人信息如下:" << endl;
			p->SaveInfo();
			break;
		}
		p = p->next;
	}
	if (!p) cout << "未查询到此人信息!" << endl;
	delete[]na;
	cout << "按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void reseachStudentByOffice()//按科室查询职工信息函数 
{
	system("cls"); // 清屏
	if (!head) {
		cout << "查询失败! 请先添加人员信息!" << endl;
		return;
	}
	cout << "请输入要查询的科室名字:" << endl;
	char* of = new char[10];
	cin >> of;
	Employee* p = head; //遍历链表代码 
	int* flag = new int(0);
	while (p)
	{
		if (strcmp(p->get_office(), of) == 0)
		{
			if (*flag == 0) cout << "所查询的科室职工如下:" << endl;
			p->SaveInfo();
			(*flag)++;
		}
		p = p->next;
	}
	if (!(*flag)) cout << "未查询到该科室人员信息!" << endl;
	delete[]of;
	delete flag;
	cout << "按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void add()//按科室统计职工平均工资函数 
{
	system("cls"); // 清屏
	if (!head) {
		cout << "统计失败! 请先添加人员信息!" << endl;
		return;
	}
	office* head_ = NULL, * p1_ = NULL, * p2_ = NULL;//前三者同之前 
	Employee* p = head; //遍历链表代码 
	office* p_;
	int i = 1;
	while (p)
	{
		int f = 0;//作为标记 
		for (p_ = head_; p_; p_ = p_->next) //遍历链表head_ 
		{
			if (strcmp(p_->get_off(), p->get_office()) == 0)
			{
				p_->add(p->get_wage()); //添加 
				f = 1;
				break;
			}
		}
		if (f == 0)  //新增,存入链表head_ 
		{
			p2_ = new office(p->get_wage(), p->get_office());
			if (i == 1)
			{
				head_ = p1_ = p2_;
			}
			else
			{
				p1_->next = p2_;
			}
			p1_ = p2_;
			p1_->next = NULL;
			i++;
		}
		p = p->next;
	}
	p_ = head_; //遍历链表代码 
	while (p_)
	{
		p_->display();
		p_ = p_->next;
	}
	cout << "按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void SortEmployees() {
	system("cls"); // 清屏
	if (!head) {
		cout << "没有可供排序的人员信息! 请先添加人员信息!" << endl;
		return;
	}

	cout << "选择排序方式:" << endl;
	cout << "1. 按工号排序" << endl;
	cout << "2. 按姓名排序" << endl;
	cout << "3. 按工资排序" << endl;
	int choice;
	cin >> choice;

	// 将链表转换为数组
	int count = 0;
	Employee* p = head;
	while (p) {
		count++;
		p = p->next;
	}
	Employee** arr = new Employee * [count];
	p = head;
	for (int i = 0; i < count; i++) {
		arr[i] = p;
		p = p->next;
	}

	// 根据选择进行排序
	switch (choice) {
	case 1:
		QuickSort(arr, 0, count - 1, Employee::CompareById);
		break;
	case 2:
		QuickSort(arr, 0, count - 1, Employee::CompareByName);
		break;
	case 3:
		QuickSort(arr, 0, count - 1, Employee::CompareByWage);
		break;
	default:
		cout << "无效的选择!" << endl;
		delete[] arr;
		return;
	}

	// 输出排序后的结果
	cout << "排序后的员工信息:" << endl;
	for (int i = 0; i < count; i++) {
		arr[i]->SaveInfo();
	}

	// 释放数组内存
	delete[] arr;
	cout << "按下任意键返回主菜单..." << endl;
	_getch(); // 等待用户按下任意键
}
void show() {
	int n;
	while (1) {
		menu_show();
		cin >> n;
		switch (n) {
		case (0): {
			exit(1);  //退出职工工资管理系统
		}
		case (1): {
			AddEmployee();//增加职工信息函数
			break;
		}
		case (2): {
			DeleteEmployee();//删除职工信息函数
			break;
		}
		case (3): {
			UpdateEmployee();//修改职工信息函数 
			break;
		}
		case(4): {
			reseachStudentByName();// 按姓名查询职工信息函数 
			break;
		}
		case(5): {
			reseachStudentByOffice();//按科室查询职工信息函数 
			break;
		}
		case (6): {
			add();//按科室统计职工平均工资函数 
			break;
		}
		case(7): {
			ap();//显示文件信息函数 
			break;
		}
		case (8): {
			SortEmployees();//排序职工信息函数
			break;
		}
		}
	}
}
int main() {
	//getdate(); 
	ofstream ot;
	ot.open("员工信息.dat", ios::out | ios::app);//1.文件不存在时创建文件2.存在时不覆盖原内容 
	if (!ot)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	//ot.write((char*)&a1,sizeof(Employee));
	ot.close();
	ifstream it;
	it.open("员工信息.dat", ios::in);
	if (!it)
	{
		cout << "员工信息文件打开失败!" << endl;
		exit(1);
	}
	it.seekg(0, ios::end);
	int filelen, i = 1;
	filelen = it.tellg ();
	it.seekg(0, ios::beg);
	while (it.tellg() != filelen)
	{
		p2 = new Employee();
		it.read((char*)p2, sizeof(Employee));
		if (i == 1)
		{
			head = p1 = p2;
		}
		else
		{
			p1->next = p2;
		}
		p1 = p2;
		p1->next = NULL;
		i++;
	}
	//head->SaveInfo();
	it.close();
	show();
}

#pragma warning(default : 4996)

五、重构项目运行结果截图
新的界面,其他功能类似

新增的排序功能

六、总结
总而言之,逆向软件工程是一个很好的提升自己的途径,再分析别人的代码中学习,也可以增强自己的代码能力,使得以后写出功能更加完善的代码,不断的进步

posted @   张铭洋  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示