C/C++ 同学通讯录系统

这篇博客介绍了作者使用C++通过单链表实现通讯录系统的尝试,包括增删改查功能。虽然作者认为代码较为粗糙,但还是选择保留作为学习历程的一部分,并强调将继续巩固基础知识。
摘要由CSDN通过智能技术生成

刚学完C++多态,简单实现一个通讯录

main.cpp

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

int main(void)
{
	funcClass user;
	int choice = 0; 
	while (true)	
	{
		user.funcMenu();
		cout << "请输入您的选择:\n";
		cin >> choice;
		switch (choice)
		{
		case 0:						// 0.退出通讯录
			user.funcExit();
			break;
		case 1:						// 1.添加联系人
			user.addContacts();
			break;
		case 2:						// 2.删除联系人
			user.DelContacts();
			break;
		case 3:						// 3.修改联系人
			user.modContacts();
			break;
		case 4:						// 4.查找联系人
			user.findContacts();
			break;
		case 5:						// 5.显示联系人
			user.showContacts();
			break;
		case 6:						// 6.排序联系人
			user.sortContacts();
		case 7:						// 7.清空联系人
			user.cleanFile();
			break;
		default:
			system("cls");
			break;
		}
	}
	return 0;
}

Contact.h

#pragma once //防止头文件重复

#include <iostream>
#include <string>

using namespace std;

class Contacts
{
public:
	virtual void showInfo() = 0;//显示联系人信息

	string name;			//同学姓名
	string addr;			//家庭住址
	string phone;			//联系电话
	string workfor;			//工作单位
};

class newContacts :public Contacts
{
public:
	//构造函数 传入的信息,需传四个
	newContacts(string name, string addr, string phone, string workfor);

	void showInfo();		//重写 显示个人信息(不加virtual也可以)
};

// 功能实现类
class funcClass
{
public:
	funcClass();

	int m_num;				//记录通讯录人数
	Contacts** m_cttArray;	//通讯录 数组指针
	bool m_fileIsEmpty;		//判断文件是否为空 标志

	void funcMenu();		// 展示功能菜单函数	
	
	void funcExit();		// 退出通讯录系统
	
	void addContacts();		// 添加联系人
	
	void fileSave();		//文件保存
	
	int getNum();			//统计文件中人数
	
	void init();			//初始化
	
	void showContacts();	//显示联系人

	void DelContacts();		//删除联系人

	int IsExit(string name);//判断联系人是否存在 存在返回在数组中的位置,不存在返回-1

	void modContacts();		//修改联系人信息

	void findContacts();	//按已有信息查找 

	void sortContacts();	//按照姓名排序

	void cleanFile();		//清空文件

	~funcClass();
};

Contact.cpp

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

#define FILENAME "phone.txt"

//构造函数 传入的信息,需传四个
newContacts::newContacts(string name, string addr, string phone, string workfor)
{
	this->name = name;
	this->addr = addr;
	this->phone = phone;
	this->workfor = workfor;
}
// 显示个人信息
void newContacts::showInfo()
{
	cout << "同学姓名:" << this->name
		<< "\t家庭住址:" << this->addr
		<< "\t联系电话:" << this->phone
		<< "\t工作单位:" << this->workfor << endl;
}

funcClass::funcClass()
{
	//1.文件不存在
	ifstream fi;
	fi.open(FILENAME, ios::in); //读文件

	if (!fi.is_open())
	{	//初始化属性
		//初始化记录人数
		this->m_num = 0;
		//初始化数组指针
		this->m_cttArray = nullptr;
		//初始化文件是否为空
		this->m_fileIsEmpty = true;
		fi.close();
		return;
	}
	//2.文件存在但数据为空
	char ch;
	fi >> ch;
	if (fi.eof())
	{
		//初始化记录人数
		this->m_num = 0;
		//初始化数组指针
		this->m_cttArray = nullptr;
		//初始化文件是否为空
		this->m_fileIsEmpty = true;
		fi.close();
		return;
	}
	//3.当文件存在,且保存联系人数据
	int num = this->getNum();
	this->m_num = num;

	//开辟空间
	this->m_cttArray = new Contacts * [this->m_num];
	//将文件中的数据,存到数组中
	this->init();
}

// 展示功能菜单函数
void funcClass::funcMenu()
{
	cout << "0.退出通讯录\n";
	cout << "1.添加联系人\n";
	cout << "2.删除联系人\n";
	cout << "3.修改联系人\n";
	cout << "4.查找联系人\n";
	cout << "5.显示联系人\n";
	cout << "6.排序联系人\n";
	cout << "7.清空联系人\n";
}
// 退出通讯录系统
void funcClass::funcExit()
{
	cout << "欢迎下次使用!\n";
	system("pause");	// 按键退出
	exit(0);			// 退出程序
}
void funcClass::addContacts()
{
	cout << "请输入添加联系人数量:\n";

	int addNum = 0; //保存用户的输入数量
	cin >> addNum;
	if (addNum > 0)
	{
		//添加
		//计算添加新空间大小
		int newSize = this->m_num + addNum; //新空间人数 = 原来记录人数+新增人数
		
		//开辟新空间
		Contacts ** newSpace = new Contacts* [newSize];

		//将原来空间下的数据,拷贝到新空间下
		if (this->m_cttArray != nullptr)
		{
			for (int i = 0; i < this->m_num; i++
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值