50

(大学生课设)如何从异质链表读入数据并读出数据?以及为什么delete node全部会出现断点错误

#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;
class person
{
private:
	friend class list;
protected:				//级别
	virtual void insert() {};
	virtual void print() {};
public:
	char name[30];
	int id;
	char sex[5];
	char department[30];   //部门   
	int level;
    static person* ptr;
	double basicmoney;
	person* next;
	person(const char* _name = "name", int _id = 000, const char* _sex = "sex", const char* _department = "department", int _level = 0) :
		id(_id), level(_level)
	{
		strcpy_s(name, _name);
		strcpy_s(sex, _sex);
		strcpy_s(department, _department);
	}
};
class list
{
public:
	person* root;
	list() :root(0) {}
	void add(person* node);
	void delet(char* _name);
	void delet(int _id);
	void show();
	void search(char* _name);
	void search(int _id);
};

void list::add(person* node)
{
	person* curr_node = root;
	node->insert();
	node->ptr->next = curr_node;
	root = node->ptr;//根指针指向新节点
}
void list::delet(char* _name)
{
	person* node = root;
	person* previous = 0;
	while (node != 0)
	{
		if (strcmp(node->name, _name) == 0)
		{
			if (previous == 0)
			{
				root = node->next;//借用previous,并非previous用法
				//delete node;
				cout << "删除完成" << endl;
				break;
			}
			else
			{
				previous->next = node->next;
				//delete node;
				cout << "删除完成" << endl;
				break;
			};
		}
		previous = node;
		node = node->next;

	}
}
void list::delet(int _id)
{
	person* node = root;
	person* previous = 0;
	while (node != 0)
	{
		if (node->id == _id)
		{
			if (previous == 0)
			{
				root = node->next;//借用previous,并非previous用法
				//delete node;
				cout << "删除完成" << endl;
				break;
			}
			else
			{
				previous->next = node->next;
				//delete node;
				cout << "删除完成" << endl;
				break;
			}
		}
		previous = node;
		node = node->next;

	}
}
void list::show()
{
	person* node = root;
	while (node != 0)
	{
		node->print();
		node = node->next;
	}
}
void list::search(char* _name)
{
	person* node = root;
	while (node != 0)
	{
		if (strcmp(node->name, _name) == 0)
		{
			node->print();
		}
		node = node->next;
	}
}
void list::search(int _id)
{
	person* node = root;
	while (node != 0)
	{
		if (node->id == _id)
		{
			node->print();
		}
		node = node->next;
	}
}
class manager :virtual public person			//增加了基本工资这个成员变量  
{
public:
	friend class list;
	void insert()
	{
		ptr = new manager(name, id, sex, department, level, basicmoney);
	}
	void print()
	{
		cout << "名字:" << name << endl;
		cout << "编号:" << id << endl;
		cout << "性别:" << sex << endl;
		cout << "部门:" << department << endl;
		cout << "级别:" << level << endl;
		cout << "工资:" << basicmoney << endl;
		cout << endl;
		cout << endl;
	}
	double basicmoney;								//基本工资
	manager(const char* _name = "name", int _id = 000, const char* _sex = "sex", const char* _department = "department", int _level = 0, double bm = 0) :person(_name, _id, _sex, _department, _level)
	{
		basicmoney = bm;					
	}
};

//经理拿固定月薪,因此基本工资就是最后的工资
class salesman : virtual public person
{
public:
	friend class list;
	void insert()
	{
		ptr = new salesman(name, id, sex, department, level, salesamount);
	}
	void print()
	{
		cout << "名字:" << name << endl;
		cout << "编号:" << id << endl;
		cout << "性别:" << sex << endl;
		cout << "部门:" << department << endl;
		cout << "级别:" << level << endl;
		cout << "销售额:" << salesamount << endl;
		cout << "工资:" << money1() << endl;
		cout << endl;	cout << endl;
	}
	double salesamount;							//销售额
	salesman(const char* _name = "name", int _id = 000, const char* _sex = "sex", const char* _department = "department", int _level = 0, double sa = 0) :person(_name, _id, _sex, _department, _level)
	{
		salesamount = sa;
	}

	double money1();								//计算总工资
};
//工资=销售额*提成率
double salesman::money1()
{
	double CommissionRate = 0.2;				//提成率
	return(salesamount * CommissionRate);
}

class salesmanager : public person
{
public:
	double moneyy, saa, bmm;
	friend class list;
	void insert()
	{
		ptr = new salesmanager(name, id, sex, department, level, bmm, saa);
	}
	void print()
	{
		cout << "名字:" << name << endl;
		cout << "编号:" << id << endl;
		cout << "性别:" << sex << endl;
		cout << "部门:" << department << endl;
		cout << "级别:" << level << endl;
		cout << "基础工资:" << bmm << endl;
		cout << "销售额:" << saa << endl;
		cout << "总工资:" << moneyy << endl;
		cout << endl;	cout << endl;
	}
	salesmanager(const char* _name = "name", int _id = 000, const char* _sex = "sex", const char* _department = "department", int _level = 0, double bm = 0, double sa = 0) :person(_name, _id, _sex, _department, _level)
	{
		saa = sa;
		bmm = bm;
		double CommissionRate = 0.2;
		moneyy = sa * CommissionRate + bm;
	}
};
class technician : public person
{
public:
	void insert()
	{
		ptr = new technician(name, id, sex, department, level, workhour);
	}
	void print()
	{
		cout << "名字:" << name << endl;
		cout << "编号:" << id << endl;
		cout << "性别:" << sex << endl;
		cout << "部门:" << department << endl;
		cout << "级别:" << level << endl;
		cout << "工作时长:" << workhour << endl;
		cout << "总工资:" << moneyy << endl;		cout << endl; cout << endl;
	}
	friend class list;
	double workhour;//工作时间
	double moneyy;
	technician(const char* _name = "name", int _id = 000, const char* _sex = "sex", const char* _department = "department", int _level = 0, double hour = 0) :person(_name, _id, _sex, _department, _level)
	{
		workhour = hour;
		moneyy = 20 * workhour;
	};
};
list _list; //创建链表
//**************************界面
void save()
{
	ofstream Data("data1.dat", ios::binary);
	person* node =_list.root;
	while (node != 0)
	{
		Data.write((char*)node, sizeof(person));
		
		node = node->next;
	}
	Data.close();
}
void read()
{
	ifstream Data1("data1.dat", ios::binary);
	person *node=new person;
	while(!Data1.eof())
	{
		
	    Data1.read((char*)node, sizeof(class person));
		(person*)node;
	   _list.add(node);
	}
	//delete node;
	Data1.close();
}
void addinformation();			//增加信息界面
void searchinformation();		//查询信息界面
void cancelinformation();		//删除信息界面
void showinformation();			//显示全部信息

void face1();
void face2();
void log()			//登录界面 管理者密码:123456 用户密码:456789
{
	system("cls");
	int n;
	cout << "========================职工信息管理系统===========================" << endl;
	cout << "===================================================================" << endl;
	cout << "|                                                                  |" << endl;
	cout << "|请输入登录密码:		        			   |" << endl;
	cout << "|                                                                  |" << endl;
	cout << "===================================================================" << endl;
	cin >> n;
	if (n == 123456)
		face1();
	if (n == 456789)
		face2();
	else
	{
		cout << "密码错误" << endl;
		system("pause");
		log();
	}
}
void face1()
{
	system("cls");//清屏
	int i;
	cout << "========================职工信息管理系统===========================" << endl;
	cout << "===================================================================" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                      欢迎使用本职工信息管理系统                 |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|             1、添加职工信息         2、查询职工信息             |" << endl;
	cout << "|             3、删除职工信息                                     |" << endl;
	cout << "|             4、显示所有职工信息		                  |" << endl;
	cout << "|             5、 保存文件                6、读取文件             |" << endl;
	cout << "|                        请选择相应编号:                          |" << endl;
	cout << "===================================================================" << endl;
	cin >> i;
	switch (i)
	{
	case 1:
		addinformation();
		break;
	case 2:
		searchinformation();
		break;
	case 3:
		cancelinformation();
		break;
	case 4:
		showinformation();
		break;
	case 5:
		save();
		cout << "保存完成" << endl;
		system("pause");
		face1();
		break;
	case 6:
		read();
		cout << "读取完成" << endl;
		system("pause");
		face1();
		break;
	default:
		cout << "输入错误" << endl;
		system("pause");
		face1();
		break;
	}
}
void face2()
{
	system("cls");
	int i;
	cout << "========================职工信息管理系统===========================" << endl;
	cout << "===================================================================" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                      欢迎使用本职工信息管理系统                 |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                      1、查询职工信息                            |" << endl;
	cout << "|													               |" << endl;
	cout << "|                      2、显示所有职工信息					       |" << endl;
	cout << "|															       |" << endl;
	cout << "|             													   |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                        请选择相应编号:                          |" << endl;
	cout << "===================================================================" << endl;
	cin >> i;
	switch (i)
	{
	case 1:
		searchinformation();
		break;
	case 2:
		showinformation();
		break;
	default:
		cout << "输入错误" << endl;
		system("pause");
		face2();
		break;
	}
}
void addinformation()
{
	char name[30];
	int id;
	char sex[5];
	char department[30];
	int level;
	double basicmoney;
	double salemoney;
	double hour;
	system("cls");
	int i;
	cout << "===================================================================" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                        请选择增加的员工种类                     |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|            1、增加经理                 2、增加销售人员          |" << endl;
	cout << "|            3、增加技术人员             4、增加销售经理          |" << endl;
	cout << "|                         5、返回上级菜单                         |" << endl;
	cout << "|                          请选择相应编号:                        |" << endl;
	cout << "===================================================================" << endl;
	cin >> i;
	switch (i)
	{
	case 1:
	{
		cout << "请依次输入姓名,编号,性别,部门,级别,基本工资:" << endl;
		cin >> name >> id >> sex >> department >> level >> basicmoney;
		manager AA(name, id, sex, department, level, basicmoney);
		_list.add(&AA);
	}
	system("pause");
	addinformation();
	break;
	case 2:
	{
		cout << "请依次输入姓名,编号,性别,部门,级别,销售额:" << endl;
		cin >> name >> id >> sex >> department >> level >> salemoney;
		salesman AA(name, id, sex, department, level, salemoney);
		_list.add(&AA);
	}
	system("pause");
	addinformation();
	break;
	case 3:
	{
		cout << "请依次输入姓名,编号,性别,部门,级别,工作时长:" << endl;
		cin >> name >> id >> sex >> department >> level >> hour;
		technician AA(name, id, sex, department, level, hour);
		_list.add(&AA);
	}
	system("pause");
	addinformation();
	break;
	case 4:
	{
		cout << "请依次输入姓名,编号,性别,部门,级别,基本工资:" << endl;
		cin >> name >> id >> sex >> department >> level >> basicmoney >> salemoney;
		salesmanager AA(name, id, sex, department, level, basicmoney, salemoney);
		_list.add(&AA);
	}
	system("pause");
	addinformation();
	break;
	case 5:
		face1();
		break;
	default:
		cout << "输入错误" << endl;
		system("pause");
		addinformation();
		break;
	}
}


void searchinformation()
{
	int i;
	system("cls");
	cout << "===================================================================" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                            请选择查询方式                       |" << endl;
	cout << "|               1、按照编号                 2、按照姓名           |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|               3、返回上级菜单				          |" << endl;
	cout << "|                             请选择相应编号:                     |" << endl;
	cout << "===================================================================" << endl;
	cin >> i;
	switch (i)
	{
	case 1:
		int p;
		cout << "请输入编号:" << endl;
		cin >> p;
		_list.search(p);
		system("pause");
		searchinformation();
		break;
	case 2:
		char n[30];
		cout << "请输入姓名:" << endl;
		cin >> n;
		_list.search(n);
		system("pause");
		searchinformation();
		break;
	case 3:
		face1();
	default:
		cout << "输入错误" << endl;
		system("pause");
		searchinformation();
		break;
		
	}
}
void cancelinformation()
{
	int i;
	system("cls");
	cout << "===================================================================" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                            请选择删除方式                       |" << endl;
	cout << "|               1、按照编号                 2、按照姓名           |" << endl;
	cout << "|                          3、返回上级菜单                        |" << endl;
	cout << "|                                                                 |" << endl;
	cout << "|                             请选择相应编号:                     |" << endl;
	cout << "===================================================================" << endl;
	cin >> i;
	switch (i)
	{
	case 1:
		int p;
		cout << "请输入编号:" << endl;
		cin >> p;
		_list.delet(p);
		system("pause");
		cancelinformation();
		break;
	case 2:
		char n[30];
		cout << "请输入姓名:" << endl;
		cin >> n;
		_list.delet(n);
		system("pause");
		cancelinformation();
		break;

	case 3:
		face1();
		break;
	default:
		cout << "输入错误" << endl;
		system("pause");
		cancelinformation();
		break;
	}
}

void showinformation()
{
	int i;
	system("cls");
	cout << "*************************显示所有职工信息**************************" << endl;
	_list.show();
	cout << endl;
	cout << endl;
	cout << "**************************输入1返回主界面**************************" << endl;
	cin >> i;
	if (i == 1)
		face1();
	else
	{
		cout << "错误" << endl;
		showinformation();
	}

}
person* person::ptr = 0;
 
int main()
{
	log();
	return 0;
}

 

查看全部
qq_51010379
qq_51010379
2021/01/13 23:53
  • c++
  • 点赞
  • 收藏
  • 回答
    私信

7个回复