要实现通讯录的增删查改,动态管理,文件操作,你首先需要掌握以下内容:
所需头文件:
#include //用于标准输入输出
#include //用于动态内存开辟
#include //用于返回bool类型
#include //用于使用断言
#include //用于字符串操作函数
为了方便后续的修改,我们定义几个全局变量来确定每个成员信息的长度大小
#define Max_Number_Init 3 //初始可容纳成员个数
#define Max_Name 20 //成员姓名的最大长度
#define Max_Sex 5 //成员性别的最大长度
#define Max_PhoneNumber 15 //成员电话的最大长度
#define Max_Address 12 //成员地址的最大长度
定义成员信息的结构体:
typedef struct PeopleInformation
{
char name[Max_Name]; //姓名
int age; //年龄
char sex[Max_Sex]; //性别
char PhoneNumber[Max_PhoneNumber]; //电话
char address[Max_Address]; //地址
}PeoInfo;
定义通讯录的结构体:
typedef struct Contact
{
PeoInfo* nums; //储存成员信息的数组
int size; //记录通讯录已经容纳的成员个数
int capacity; //记录通讯录的最大容量
}Con;
void meau()
{
printf("*******************************\n");
printf("****1->Add 2->Del******\n");
printf("****3->Search 4->Modify***\n");
printf("****5->Show 6->Claer****\n");
printf("****0->Exit **********\n");
}
为了提高代码的可读性,我们可以定义一个枚举类型,来分别代表这些选项:
enum Choice
{
Exit, //0
Add, //1
Del, //2
Search, //3
Modify, //4
Show, //5
Clear //6
};
int main()
{
int input = 0;
//为通讯录申请内存
Con* con = (Con*)malloc(sizeof(Con));
ContactInit(con); //初始化通讯录
do
{
meau();
printf("请输入需要的选项:");
scanf("%d", &input);
switch (input)
{
case Add:
ContactPush(con); //增
break;
case Del:
ContactPop(con); //删
break;
case Search:
ContactSearch(con); //查
break;
case Modify:
ContactModify(con); //改
break;
case Show:
ContactShow(con); //展示通讯录
break;
case Clear:
ContactClear(con); //清空通讯录
break;
case Exit:
ContactDataInput(con); //退出通讯录
printf("退出通讯录\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
//释放动态内存
if(con->nums)
free(con->nums);
free(con);
return 0;
}
void ContactClear(Con* con)
清空通讯录,就是将保存成员信息的数组con->nums
释放,同时置空,并且将size
置0
void ContactClear(Con* con)
{
free(con->nums);
con->nums = NULL;
con->size = 0;
}
void ContactInit(Con* con);
通讯录的初始化就是为储存成员信息的数组申请内存,确定最大容量以及将size
置0
void ContactInit(Con* con)
{
con->capacity = Max_Number_Init;
con->nums = (PeoInfo*)malloc(sizeof(PeoInfo) * con->capacity);
con->size = 0;
}
void ContactPush(Con* con)
在加入成员之前,我们需要检查是否还有空间可以储存,如果已经满了,就要增容
我们可以单独写一个函数来进行检查和增容:
bool ContactFull(const Con* con)
{
assert(con);
return con->size == con->capacity;
}
void ContactCheak(Con* con)
{
assert(con);
if (ContactFull(con))
{
con->capacity += 2; //如果满了,每次就将最大容量加2
//增容
PeoInfo* temp = (PeoInfo*)realloc(con->nums, con->capacity * sizeof(PeoInfo));
if (temp == NULL)
{
perror("realloc");
exit(0);
}
con->nums = temp;
}
}
void ContactPush(Con* con)
{
assert(con);
ContactCheak(con);
//如果通讯录之前被清空过,要想重新使用,就先要为nums申请内存
if (NULL == con->nums)
{
con->nums = (PeoInfo*)malloc(sizeof(PeoInfo) * Max_Number_Init);
if (NULL == con->nums)
{
perror("malloc");
exit(1);
}
}
printf("请输入添加的联系人姓名:");
scanf("%s", (con->nums + con->size)->name);
printf("请输入该联系人的年龄:");
scanf("%d", &(con->nums + con->size)->age);
printf("请输入该联系人的性别:");
scanf("%s", (con->nums + con->size)->sex);
printf("请输入该联系人的电话:");
scanf("%s", (con->nums + con->size)->PhoneNumber);
printf("请输入该联系人的地址:");
scanf("%s", (con->nums + con->size)->address);
con->size++;
printf("成功添加联系人\n");
}
void ContactPop(Con* con)
这里通过姓名来删除联系人
最开始,要判断通讯录是否为空,如果为空,就不能删除
在删除之前,要现在通讯录中查找是否有这个联系人,如果没有,就不要删除(我们用一个函数ContactFind
来实现查找成员,并返回成员下标)
为了保证删除效率,我们找到需要删除的联系人的下标后,将其和最后一个成员交换位置,再利用size--
删除最后一个成员即可
bool ContactEmpty(const Con* con)
{
assert(con);
return con->size == 0;
}
void Swap(PeoInfo* peo1, PeoInfo* peo2)
{
PeoInfo temp = *peo1;
*peo1 = *peo2;
*peo2 = temp;
}
int ContactFind(const Con* con, const char* str)
{
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->nums[i].name, str) == 0)
return i;
}
return -1;
}
void ContactPop(Con* con)
{
assert(con); //判断指针有效性
assert(!ContactEmpty(con)); //通讯录不为空
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
//打印将被删除的成员信息
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
//清空缓冲区
while (getchar() != '\n');
printf("确定删除(Y|N): ");
char ch = getchar();
if (ch == 'Y')
{
Swap(&con->nums[index], &con->nums[con->size - 1]);
con->size--;
printf("删除成功\n");
}
else
{
printf("删除失败\n");
}
}
else
{
printf("未找到该联系人\n");
}
}
void ContactSearch(const Con* con)
这里统一用姓名来查找联系人,找到便打印联系人信息
void ContactSearch(const Con* con)
{
assert(con);
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
}
else
{
printf("未找到该联系人\n");
}
}
void ContactModify(Con* con)
这里统一用姓名查找联系人,然后再进行修改
void ContactModify(Con* con)
{
assert(con);
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
char ch;
//展示联系人姓名
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
printf("是否修改年龄(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人年龄:");
scanf("%d", &con->nums[index].age);
}
printf("是否修改性别(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人性别:");
scanf("%s", con->nums[index].sex);
}
printf("是否修改电话(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人电话:");
scanf("%s", con->nums[index].PhoneNumber);
}
printf("是否修改住址(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人地址:");
scanf("%s", con->nums[index].address);
}
}
else
{
printf("未找到该联系人\n");
}
}
int compare_by_name(const void* data1, const void* data2)
{
return strcmp(((PeoInfo*)data1)->name, ((PeoInfo*)data2)->name);
}
void ContactSort(Con* con)
{
//用qsort进行排序
qsort(con->nums, con->size, sizeof(PeoInfo), compare_by_name);
}
void ContactShow(const Con* con)
{
assert(con);
ContactSort(con);
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n","姓名","年龄","性别","电话","地址");
for (int i = 0; i < con->size; i++)
{
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + i)->name,
(con->nums + i)->age,
(con->nums + i)->sex,
(con->nums + i)->PhoneNumber,
(con->nums + i)->address);
}
}
要实现文件操作,我们再加入两个函数就可以了
ContactDataInput()
)ContactDataCopy()
)void ContactDataInput(Con* con)
{
assert(con);
FILE* fp = fopen("data.txt", "w");
if (NULL == fp)
{
perror("fopen");
exit(1);
}
for (int i = 0; i < con->size; i++)
fprintf(fp, "%s %d %s %s %s %c", con->nums[i].name, con->nums[i].age, con->nums[i].sex, con->nums[i].PhoneNumber, con->nums[i].address, '\n');
fclose(fp);
fp = NULL;
}
void ContactDataCopy(Con* con)
{
assert(con);
FILE* fp = fopen("data.txt", "r");
if (NULL == fp)
{
perror("fopen");
exit(1);
}
PeoInfo temp = {0};
while (fscanf(fp, "%s %d %s %s %s", temp.name, &temp.age, temp.sex, temp.PhoneNumber, temp.address)!=EOF)
{
//插入之前先检查容量
ContactCheak(con);
con->nums[con->size] = temp;
con->size++;
}
fclose(fp);
fp = NULL;
}
#pragma once
#include
#include
#include
#include
#include
#define Max_Number_Init 3
#define Max_Name 20
#define Max_Sex 5
#define Max_PhoneNumber 15
#define Max_Address 12
typedef struct PeopleInformation
{
char name[Max_Name];
int age;
char sex[Max_Sex];
char PhoneNumber[Max_PhoneNumber];
char address[Max_Address];
}PeoInfo;
typedef struct Contact
{
PeoInfo* nums;
int size;
int capacity;
}Con;
void ContactInit(Con* con);
bool ContactEmpty(const Con* con);
bool ContactFull(const Con* con);
void ContactPush(Con* con);
void ContactPop(Con* con);
void ContactSearch(const Con* con);
void ContactModify(Con* con);
void ContactShow(const Con* con);
void ContactClear(Con* con);
void ContactDataInput(Con* con);
void ContactDataCopy(Con* con);
void ContactCheak(Con* con)
{
assert(con);
if (ContactFull(con))
{
con->capacity += 2;
PeoInfo* temp = (PeoInfo*)realloc(con->nums, con->capacity * sizeof(PeoInfo));
if (temp == NULL)
{
perror("realloc");
exit(0);
}
con->nums = temp;
}
}
void ContactDataCopy(Con* con)
{
assert(con);
FILE* fp = fopen("data.txt", "r");
if (NULL == fp)
{
perror("fopen");
exit(1);
}
PeoInfo temp = {0};
while (fscanf(fp, "%s %d %s %s %s", temp.name, &temp.age, temp.sex, temp.PhoneNumber, temp.address)!=EOF)
{
ContactCheak(con);
con->nums[con->size] = temp;
con->size++;
}
fclose(fp);
fp = NULL;
}
int compare_by_name(const void* data1, const void* data2)
{
return strcmp(((PeoInfo*)data1)->name, ((PeoInfo*)data2)->name);
}
void ContactSort(Con* con)
{
qsort(con->nums, con->size, sizeof(PeoInfo), compare_by_name);
}
int ContactFind(const Con* con, const char* str)
{
for (int i = 0; i < con->size; i++)
{
if (strcmp(con->nums[i].name, str) == 0)
return i;
}
return -1;
}
void ContactInit(Con* con)
{
con->capacity = Max_Number_Init;
con->nums = (PeoInfo*)malloc(sizeof(PeoInfo) * con->capacity);
con->size = 0;
ContactDataCopy(con);
}
bool ContactEmpty(const Con* con)
{
assert(con);
return con->size == 0;
}
bool ContactFull(const Con* con)
{
assert(con);
return con->size == con->capacity;
}
void ContactShow(const Con* con)
{
assert(con);
ContactSort(con);
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n","姓名","年龄","性别","电话","地址");
for (int i = 0; i < con->size; i++)
{
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + i)->name,
(con->nums + i)->age,
(con->nums + i)->sex,
(con->nums + i)->PhoneNumber,
(con->nums + i)->address);
}
}
void ContactPush(Con* con)
{
assert(con);
ContactCheak(con);
if (NULL == con->nums)
{
con->nums = (PeoInfo*)malloc(sizeof(PeoInfo) * Max_Number_Init);
if (NULL == con->nums)
{
perror("malloc");
exit(1);
}
}
printf("请输入添加的联系人姓名:");
scanf("%s", (con->nums + con->size)->name);
printf("请输入该联系人的年龄:");
scanf("%d", &(con->nums + con->size)->age);
printf("请输入该联系人的性别:");
scanf("%s", (con->nums + con->size)->sex);
printf("请输入该联系人的电话:");
scanf("%s", (con->nums + con->size)->PhoneNumber);
printf("请输入该联系人的地址:");
scanf("%s", (con->nums + con->size)->address);
con->size++;
printf("成功添加联系人\n");
}
void Swap(PeoInfo* peo1, PeoInfo* peo2)
{
PeoInfo temp = *peo1;
*peo1 = *peo2;
*peo2 = temp;
}
void ContactPop(Con* con)
{
assert(con);
assert(!ContactEmpty(con));
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
while (getchar() != '\n');
printf("确定删除(Y|N): ");
char ch = getchar();
if (ch == 'Y')
{
Swap(&con->nums[index], &con->nums[con->size - 1]);
con->size--;
printf("删除成功\n");
}
else
{
printf("删除失败\n");
}
}
else
{
printf("未找到该联系人\n");
}
}
void ContactSearch(const Con* con)
{
assert(con);
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
}
else
{
printf("未找到该联系人\n");
}
}
void ContactModify(Con* con)
{
assert(con);
char name[20];
printf("请输入被查找人的姓名:");
scanf("%s", name);
int index = ContactFind(con, name);
if (index != -1)
{
char ch;
printf("%-20s\t%-4s\t%-5s\t%-15s\t%-12s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-15s\t%-12s\n\n",
(con->nums + index)->name,
(con->nums + index)->age,
(con->nums + index)->sex,
(con->nums + index)->PhoneNumber,
(con->nums + index)->address);
printf("是否修改年龄(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人年龄:");
scanf("%d", &con->nums[index].age);
}
printf("是否修改性别(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人性别:");
scanf("%s", con->nums[index].sex);
}
printf("是否修改电话(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人电话:");
scanf("%s", con->nums[index].PhoneNumber);
}
printf("是否修改住址(Y|N): ");
while (getchar() != '\n');
ch = getchar();
if (ch == 'Y')
{
printf("请输入新的联系人地址:");
scanf("%s", con->nums[index].address);
}
}
else
{
printf("未找到该联系人\n");
}
}
void ContactClear(Con* con)
{
free(con->nums);
con->nums = NULL;
con->size = 0;
}
void ContactDataInput(Con* con)
{
assert(con);
FILE* fp = fopen("data.txt", "w");
if (NULL == fp)
{
perror("fopen");
exit(1);
}
for (int i = 0; i < con->size; i++)
fprintf(fp, "%s %d %s %s %s %c", con->nums[i].name, con->nums[i].age, con->nums[i].sex, con->nums[i].PhoneNumber, con->nums[i].address, '\n');
fclose(fp);
fp = NULL;
}
void meau()
{
printf("*******************************\n");
printf("****1->Add 2->Del******\n");
printf("****3->Search 4->Modify***\n");
printf("****5->Show 6->Claer****\n");
printf("****0->Exit **********\n");
}
enum Choice
{
Exit,
Add,
Del,
Search,
Modify,
Show,
Clear
};
int main()
{
int input = 0;
Con* con = (Con*)malloc(sizeof(Con));
ContactInit(con);
do
{
meau();
printf("请输入需要的选项:");
scanf("%d", &input);
switch (input)
{
case Add:
ContactPush(con);
break;
case Del:
ContactPop(con);
break;
case Search:
ContactSearch(con);
break;
case Modify:
ContactModify(con);
break;
case Show:
ContactShow(con);
break;
case Clear:
ContactClear(con);
break;
case Exit:
ContactDataInput(con);
printf("退出通讯录\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
if(con->nums)
free(con->nums);
free(con);
return 0;
}