AddressBook.h
#pragma once
#include
#include
//#define FILE_PATH "./Contact.txt"
#define FLIE_PATH2 "D://学习工作/我的代码/作业/数据结构/AddressBook/Contact.DAT"
//定义两个结构体
#define NAME_SIZE 200
#define PHONE_SIZE 200
#define ADDR_SIZE 200
typedef struct PersonInfo {
char name[NAME_SIZE];
char phone[PHONE_SIZE];
char addr[ADDR_SIZE];
}PersonInfo;
typedef struct AddrBook {
PersonInfo* data;
size_t size;
size_t capacity;
}AddrBook;
#define DEFSULT_SZ 5
//初始化通讯录
void InitContact(AddrBook* addr);
//对通讯录进行扩容
void CheckCapacity(AddrBook* addr);
//在通讯录中增加条目
void AddContact(AddrBook* addr);
//通过姓名查找,如果有返回数组下标
int FindContactWithName(AddrBook* addr, char* name);
//通过电话号码查找,如果有就返回下标
int FindContactWithPhoneNumber(AddrBook* addr, char* phonenumber);
//删除通讯录中的联系人信息
void DeleteContact(AddrBook* addr);
//打印通讯录中所有联系人信息
void AddrBookDisplay(AddrBook* addr);
//打印单个联系人
void PrintSingleContact(AddrBook* addr, int index);
//按规则查找指定联系人
void AddrBookFind(AddrBook* addr);
//销毁通讯录
void AddrBookDestory(AddrBook* addr);
//修改通讯录中某个联系人的信息
void AddrBookModify(AddrBook* addr);
//把联系人保存到文件流中
void FlieSave(AddrBook addr);
/*从 文件中读取已保存的联系人信息*/
void FileRead(AddrBook addr);
AddressBook.c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include"AddressBook.h"
void InitContact(AddrBook* addr) {
if (addr == NULL) {
return;
}
addr->size = 0;
addr->data = (AddrBook*)malloc(DEFSULT_SZ *sizeof(AddrBook));
if (addr->data == NULL) {
printf("初始化失败!");
return;
}
addr->capacity = DEFSULT_SZ;
}
void CheckCapacity(AddrBook* addr) {
if (addr == NULL) {
return;
}
//通讯录的大小已经等于容量了
if (addr->size == addr->capacity) {
//这个时候需要扩容
addr->capacity = addr->capacity * 2 + 1;
AddrBook* ptr = realloc(addr->data, (sizeof(AddrBook)* addr->capacity));
if (ptr == NULL) {
printf("扩容失败!\n");
return;
}
else{
addr->data = ptr;
}
printf("扩容成功!\n");
}
}
void AddContact(AddrBook* addr) {
if (addr == NULL) {
return;
}
//增加联系人之前判断通讯录容量是否已满,如果是则扩容
CheckCapacity(addr);
printf("请开始添加联系人信息->\n");
printf("请输入联系人姓名:");
scanf("%s \n", addr->data->name);
printf("请输入联系人电话号码: ");
scanf("%s \n", addr->data->phone);
printf("请输入联系人地址:");
scanf("%s \n", addr->data->addr);
++addr->size;
printf("添加成功!\n");
}
//通过姓名查找,如果有返回数组下标
int FindContactWithName(AddrBook* addr, const char* name) {
if (addr == NULL) {
return;
}
int i = 0;
for (; i < addr->size; i++) {
if (strcmp(addr->data[i].name, name) == 0) {
return i;
}
}
return -1;
}
//通过电话号码查找,如果有就返回下标
int FindContactWithPhoneNumber(AddrBook* addr,char phonenumber) {
if (addr == NULL) {
return;
}
int i = 0;
for (; i < addr->size; i++) {
if (strcmp(addr->data[i].phone, phonenumber) == 0) {
return i;
}
}
return -1;
}
//删除联系人
void DeleteContact(AddrBook* addr) {
if (addr == NULL) {
return;
}
input:
printf("请选择删除方式:\n\1.通过联系人姓名删除\n");
printf("2.通过联系人电话号码删除\n");
int n = 0;
scanf("%d\n", &n);
if (n == 1) {
// system("cls");
char name[NAME_SIZE];
printf("请输入您所需要删除的联系人姓名: ");
scanf("%s", name);
int ret = FindContactWithName(addr, name);
if (ret == -1) {
printf("您查找的联系人姓名不存在\n");
return;
}
else {
for (; ret < addr->size; ret++) {
addr->data[ret] = addr->data[ret + 1];
}
addr->size--;
printf("删除成功!");
}
}
else if (n == 2) {
system("cls");
char phone[PHONE_SIZE];
printf("请输入您所需要删除的联系人电话号码:");
scanf("%s", phone);
int ret = FindContactWithPhoneNumber(addr, phone);
if (ret == -1) {
printf("电话号码未与任何一个联系人匹配!");
return;
}
else {
int i = ret;
for (; i < addr->size; i++) {
addr->data[i] = addr->data[i + 1];
}
addr->size;
printf("删除成功");
}
}
else {
printf("输入错误,请重新输入:");
goto input;
}
}
//打印联系人信息
void AddrBookDisplay(AddrBook* addr) {
if (addr == NULL) {
return;
}
printf("%15s\t%11s \t%20s\t\n", "Name", "Phone", "Address");
int i = 0;
for (; i < addr->size; i++) {
printf("\t%5s\t %11s\t %20s\n",
addr->data[i].name,
addr->data[i].phone,
addr->data[i].addr);
}
}
//打印单个联系人
void PrintSingleContact(AddrBook* addr, int index) {
if (addr == NULL) {
return;
}
printf("%15s\t%11s \t%20s\t\n", "Name", "Phone", "Address");
printf("\t%5s\t %11s\t %20s\n",
addr->data[index].name,
addr->data[index].phone,
addr->data[index].addr);
}
//按照规则查找指定的联系人
void AddrBookFind(AddrBook* addr) {
if (addr == NULL) {
return;
}
printf("请选择查找联系人的方式:\n");
printf("1.通过姓名查找\n");
printf("1.通过电话号码查找\n");
int n = 0;
input2:
scanf("%d", &n);
if (n == 1) {
char Name[NAME_SIZE];
printf("请输入需要查找的联系人姓名:");
scanf("%s\n", Name);
int ret = FindContactWithName(addr, Name);
if (ret == -1) {
printf("没有查找到相应的联系人!\n");
return;
}
else {
PrintSingleContact(addr, ret);
printf("查找成功!");
}
}
else if (n == 2) {
char Phone[PHONE_SIZE];
printf("请输入需要查找的联系人电话号码:");
scanf("%s\n", Phone);
int index = FindContactWithPhoneNumber(addr, Phone);
if (index == -1) {
printf("没有查找到相应的联系人!");
}
else {
PrintSingleContact(addr, index);
printf("查找成功!");
}
}
else {
printf("选择错误,请重新输入:");
goto input2;
}
}
//销毁通讯录
void AddrBookDestory(AddrBook* addr) {
if (addr == NULL) {
return;
}
//内存空间被释放后,一定要将指针设为NULL,
//否则万一内存空间未被重新分配,就可能会被指针调用,差不多是这么个意思
free(addr->data);
addr->data = NULL;
addr->size = 0;
addr->capacity = 0;
}
//修改通讯录中某个联系人的信息
void AddrBookModify(AddrBook* addr) {
if (addr == NULL) {
return;
}
printf("请输入您想要修改的联系人姓名:");
char name[NAME_SIZE];
scanf("%s", name);
int index = FindContactWithName(addr, name);
if (index == NULL) {
//查无此人
return;
}
else {
printf("请开始修改->:\n");
printf("请重新输入联系人姓名:");
scanf("%s\n", addr->data[index].name);
printf("请重新输入联系人电话号码: ");
scanf("%s\n", addr->data[index].phone);
printf("请重新输入联系人地址:");
scanf("%s\n", addr->data[index].addr);
printf("修改成功!");
}
}
//把联系人保存到文件流中
void FlieSave(AddrBook addr) {
int i = 0;
FILE* pf = fopen(FLIE_PATH2, "w");
if (pf == NULL) {
perror("error");
exit(EXIT_FAILURE);
}
for (; i < addr.size; i++) {
fwrite(&(addr.data[i]), sizeof(PersonInfo), 1, pf);
}
fclose(pf);
}
/*从 文件中读取已保存的联系人信息*/
void FileRead(AddrBook addr) {
FILE* pfread = fopen(FLIE_PATH2, "r");
if (pfread == NULL) {
perror("open file for read");
exit(EXIT_FAILURE);
}
PersonInfo per = { 0 };
int i = 0;
while (fread(&per, sizeof(PersonInfo), 1, pfread)) {
addr.data[i] = per;
i++;
addr.size++;
}
fclose(pfread);
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include"AddressBook.h"
void menu() {
printf("//////////////////////////////////////\n");
printf("1.添加联系人.\n");
printf("2.删除联系人.\n");
printf("3.查找联系人.\n");
printf("4.显示所有的联系人.\n");
printf("5.修改联系人信息.\n");
printf("6.清空所有联系人信息.\n");
printf("7.把联系人保存至文件.\n");
printf("8.退出.\n");
printf("//////////////////////////////////////\n\n");
printf("欢迎使用通讯录小程序\n");
}
void test() {
AddrBook addr;
InitContact(&addr);
FileRead(addr);
int choice = 1;
do {
menu();
printf("请选择->");
scanf("%d\n", &choice);
// system("cls");
switch (choice) {
case 1:
AddContact(&addr);
break;
case 2:
DeleteContact(&addr);
break;
case 3:
AddrBookFind(&addr);
break;
case 4:
AddrBookDisplay(&addr);
break;
case 5:
AddrBookModify(&addr);
break;
case 6:
AddrBookDestory(&addr);
break;
case 7:
FlieSave(addr);
break;
case 8:
exit(0);
break;
default:
break;
}
} while (choice);
}
int main() {
test();
system("pause");
return 0;
}