C++ 面向对象版本通讯录

Person.h定义Person类对象,以及Person类的比较,赋值,和输出重载

#pragma once

#include<string>
#include<ostream>
using namespace std;

class Person
{
	friend ostream &operator<<(ostream &out, Person &person);      // 输出运算符重载

public:
	string name;
	string phone;
	Person & operator=(Person &person);                            // 赋值运算符重载
	bool operator==(Person &person);                               // 判断相等运算符重载

};

Person.cpp实现了Person.h中的重载运算符函数

#include "Person.h"

ostream &operator<<(ostream &out, Person &person)
{
	out << person.name <<" "<< person.phone<<endl;
	return out;
}

Person & Person::operator=(Person &person)   // 赋值运算符重载
{
	this->name = person.name;
	this->phone = person.phone;
	return *this;
}

bool Person::operator==(Person &person)
{
	if (this->name == person.name)
	{
		return true;
	}
	return false;
}

Node.h定义了节点类

#pragma once

#include "Person.h"

class Node
{
public:
	Person data;
	Node *next;
	void printNode();
	
};

Node.cpp实现成员函数

#include "Node.h"
#include<iostream>

void Node::printNode()
{
	std::cout << data << std::endl;
}

List.h定义了链表的头指针和操作函数

#pragma once
#include"Node.h"
#include<fstream>
using namespace std;
class List
{
private:
	Node * m_pList;
	int m_iLength;

public:
	List();
	~List();
	void ClearList();                                    // 清空整个链表
	bool ListEmpty();                                    // 判断链表是否为空
	bool ListInsertHead(Node *pNode);                    // 头部插入一个节点
	bool ListInsertTail(Node *pNode);                    // 尾部插入一个节点
	bool ListInsert(int i, Node *pNode);                 // 从任意位置插入节点
	bool ListDelete(int i, Node *pNode);                 // 从任意位置删除结点
	bool ListGetElem(int i, Node *pNode);                // 查找第i个元素
	int  LocateElem(Node *pNode);                        // 查找结点的位序
	bool PriorElem(Node *pCurrentNode, Node *pPreNode);  // 找指针的前驱指针
	bool NextElem(Node *pCurrentNode, Node *pNextNode);  // 找指针的后继指针
	void ListTraverse();                                 // 链表的遍历
	void ListSaveData(char *file);                       // 将链表数据保存到文件中
	
};

List.cpp实现了链表的操作函数

#include "List.h"


List::List()
{
	m_pList = new Node;
	// m_pList->data = 0;
	m_pList->next = NULL;
	m_iLength = 0;
}

bool List::ListEmpty()
{
	if (m_iLength == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void List::ClearList()
{
	Node *currentNode = m_pList->next; // 指向头指针后第一个元素
	while (currentNode != NULL)
	{
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;               // 头指针下一个为空
	
}

List::~List()
{
	ClearList();
	delete m_pList;
	m_pList = NULL;
}

bool List::ListInsertHead(Node *pNode)
{
	Node *temp = m_pList->next;            // 头节点后第一个元素
	Node *newNode = new Node;              // 创建一个新的节点
	if (newNode == NULL)
		return false;
	newNode->data = pNode->data;
	m_pList->next = newNode;
	newNode->next = temp;
	m_iLength++;
	return true;
}

bool List::ListInsertTail(Node *pNode)
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
		currentNode = currentNode->next;   // 遍历到尾结点
	Node *newNode = new Node;
	if (newNode == NULL)
		return false;
	newNode->data = pNode->data;
	newNode->next = NULL;
	currentNode->next = newNode;
	m_iLength++;
	return true;
}

bool List::ListInsert(int i, Node *pNode)
{
	if (i<0 || i>m_iLength)
		return false;
	Node *currentNode = m_pList;
	for (int k = 0; k < i; k++)
	{
		currentNode = currentNode->next;
	}

	Node *newNode = new Node;
	if (newNode == NULL)
		return false;
	newNode->data = pNode->data;
	newNode->next = currentNode->next;
	currentNode->next = newNode;
	m_iLength++;
	return true;

}

bool List::ListDelete(int i, Node *pNode)
{
	if (i < 0 || i >m_iLength)
		return false;
	Node *currentNode = m_pList;
	Node *currentNodeBefore = NULL;
	for (int k = 0; k <= i; k++)
	{
		currentNodeBefore = currentNode;
		currentNode = currentNode->next;
	}
	currentNodeBefore->next = currentNode->next;
	pNode->data = currentNode->data;
	delete currentNode;
	currentNode = NULL;
	m_iLength--;
	return true;
}

bool List::ListGetElem(int i, Node *pNode)
{
	if (i < 0 || i >m_iLength)
		return false;
	Node *currentNode = m_pList;
	for (int k = 0; k <= i; k++)
	{
		currentNode = currentNode->next;
	}
	pNode->data = currentNode->data;
	return true;
}

int List::LocateElem(Node *pNode)
{
	Node *currentNode = m_pList;
	int count = 0;
	while (currentNode->next!= NULL)
	{
		currentNode = currentNode->next;
		if (currentNode->data == pNode->data)
		{
			return count;
		}
		count++;
	}
	return -1;
}

bool List::PriorElem(Node *pCurrentNode, Node *pPreNode)
{
	Node *currentNode = m_pList;
	Node *tempNode = NULL;
	while (currentNode->next != NULL)
	{
		tempNode = currentNode;
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (tempNode == m_pList)
				return false;
			else
			{
				pPreNode->data = tempNode->data;
				return true;
			}
			
		}
	}
	return false;
}

bool List::NextElem(Node *pCurrentNode, Node *pNextNode)
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		if (currentNode->data == pCurrentNode->data)
		{
			if (currentNode->next == NULL)
			{
				return false;
			}
			else
			{
				pNextNode->data = currentNode->next->data;
				return true;
			}

		}
	}
	return false;
}

void List::ListTraverse()
{
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		currentNode->printNode();
	}

}

void List::ListSaveData(char *file)
{
	ofstream destFile(file, ios::app);
	Node *currentNode = m_pList;
	while (currentNode->next != NULL)
	{
		currentNode = currentNode->next;
		destFile << currentNode->data;
	}
	destFile.close();
}

demo.cpp对list进行调用,实现了功能,文件的输入输出使用C++的<fstream>库

#include "List.h"
#include<iostream>
#include<fstream>
using namespace std;


void ShowOrder();
void createPersonTail(List *pList);
void createPersonHead(List *pList);
void createPerson(List *pList);
void searchPerson(List *pList);
void deletePerson(List *pList);
void ReadStuFile(List *pList);
void SaveStuFile(List *pList);
//void ModifyPerson(List *pList);

int main()
{
	// 显示操作界面
	int nOrder;
	ShowOrder();
	bool flag = true;
	List *pList = new List();
	// 读取文件中学生信息
	// ReadStuFile(pList);
	
	while (flag)
	{
		printf("请输入指令,输入0查看指令:\n");
		cin >> nOrder;
		switch (nOrder)
		{
		case 1:
			// 添加一个学生的信息(尾添加)
			createPersonTail(pList);
			break;
		case 11:
			// 添加一个学生的信息(头添加)
			createPersonHead(pList);
			break;
		case 111:
			// 添加一个学生的信息(从指定位置)
			createPerson(pList);
		case 2:
			// 根据学号查找学生信息
			searchPerson(pList);
			break;
		case 3:
			// 修改指定学生的信息
			// 根据学号查找学生信息
			// ModifyPerson(pList);
			break;
		case 4:
			SaveStuFile(pList);
			cout << "成功写入文件" << endl;
			break;
		case 5:
			// 从文件读取数据到链表
			ReadStuFile(pList);
			cout << "加载数据库成功" << endl;
			break;
		case 6:
			// 根据学号,删除指定的学生信息
			deletePerson(pList);
			break;
		case 7:
			// 从文件恢复删除的学生的信息
			pList->ClearList();
			ReadStuFile(pList);
			break;

		case 8:
			// 显示链表内容
			pList->ListTraverse();
			break;

		case 9:
			// 退出程序
			flag = false;
			break;
		case 0:
			// 显示指令
			ShowOrder();
			break;
		default:
			printf("输入指令不正确\n");
			break;
		}
	}

	// 释放链表
	pList->ClearList();
	return 0;
}


void ShowOrder()
{
	printf("**********************通讯录管理系统********************\n");
	printf("********************本系统操作指令如下******************\n");
	printf("***              1. 增加一个好友信息(从表尾)         ***\n");
	printf("***             11. 增加一个好友信息(从表头)         ***\n");
	printf("***            111. 增加一个好友信息(从指定位置)     ***\n");
	printf("***              2. 查找指定好友的信息(姓名/学号)  ***\n");
	printf("***              3. 修改指定好友的信息               ***\n");
	printf("***              4. 保存业主的信息到文件中           ***\n");
	printf("***              5. 读取文件中的好友的信息           ***\n");
	printf("***              6. 删除指定好友的信息               ***\n");
	printf("***              7. 恢复删除的好友的信息             ***\n");
	printf("***              8. 显示所有的好友的信息             ***\n");
	printf("***              9. 退出系统                         ***\n");
	printf("********************************************************\n");
}


void createPersonTail(List *pList)
{
	Node node;
	Person person;
	cout << "请输入姓名:";
	cin >> person.name;
	cout << "请输入电话:";
	cin >> person.phone;
	node.data = person;
	if (pList->ListInsertTail(&node))
		cout << "添加成功" << endl;
	else
		cout << "添加失败" << endl;
}

void createPersonHead(List *pList)
{
	Node node;
	Person person;
	cout << "请输入姓名:";
	cin >> person.name;
	cout << "请输入电话:";
	cin >> person.phone;
	node.data = person;
	if (pList->ListInsertTail(&node))
		cout << "添加成功" << endl;
	else
		cout << "添加失败" << endl;
}

void createPerson(List *pList)
{
	Node node;
	Person person;
	int i;
	cout << "请输入姓名:";
	cin >> person.name;
	cout << "请输入电话:";
	cin >> person.phone;
	cout << "请输入添加的位置:";
	cin >> i;
	node.data = person;
	if (pList->ListInsert(i,&node))
		cout << "添加成功" << endl;
	else
		cout << "添加失败" << endl;
}

void searchPerson(List *pList)
{
	Node node, ans;
	Person person;
	int i;
	cout << "请输入要查找好友的姓名:";
	cin >> person.name;
	person.phone = "";
	node.data = person;
	i = pList->LocateElem(&node);
	if (pList->ListGetElem(i, &ans))
		ans.printNode();
	else
		printf("没有该好友信息\n");
}

void deletePerson(List *pList)
{
	Node node;
	Person person;
	int i;
	cout << "请输入要删除好友的姓名";
	cin >> person.name;
	person.phone = "";
	node.data = person;
	i = pList->LocateElem(&node);
	if (pList->ListDelete(i, &node))
		cout << "删除好友" << person.name << "成功" << endl;
	else
		cout << "删除好友" << person.name << "失败" << endl;

}

void ReadStuFile(List *pList)
{
	ifstream srcFile("AddressList.txt", ios::in);
	Person person;
	Node node;
	while (srcFile >> person.name >> person.phone)
	{
		node.data = person;
		pList->ListInsertTail(&node);
	}
	srcFile.close();
}

void SaveStuFile(List *pList)
{
	char file[30] = "AddressList.txt";
	cout << file << endl;
	pList->ListSaveData(file);
	
}

猜你喜欢

转载自blog.csdn.net/wwxy1995/article/details/83420798