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 + ad