目录
文件通讯录::
contact.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
#define DEFAULT_SZ 3
#define INC_SZ 2
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
//结构体类型的声明
//人的信息
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;
//通讯录
typedef struct Contact
{
PeoInfo* data;//存放人的信息(结构体嵌套)
int count;//记录当前通讯录中实际人数
//当前通讯录的容量
int capacity;
}Contact;
//初始化通讯录
void InitContact(Contact* pc);
//销毁通讯录
void DestroyContact(Contact* pc);
//增加联系人到通讯录
void AddContact(Contact* pc);
//打印通讯录的信息
void ShowContact(Contact* pc);
//删除指定联系人
void DelContact(Contact* pc);
//查找指定联系人
void SearchContact(Contact* pc);
//修改指定联系人
void ModifyContact(Contact* pc);
//排序通讯录中的内容 按照名字来排序
void SortContact(Contact* pc);
//保存通讯录到文件
void SaveContact(Contact* pc);
//加载文件信息到通讯录
void LoadContact(Contact* pc);
contact.c
#include "contact.h"
void InitContact(Contact* pc)
{
assert(pc);
pc->count = 0;
pc->data = (PeoInfo*)calloc(3, sizeof(PeoInfo));
if (pc->data = NULL)
{
printf("InitContact:%s\n", strerror(errno));
exit(-1);
}
pc->capacity = DEFAULT_SZ;
//加载文件信息到通讯录中
LoadContact(pc);
}
void DestoryContact(Contact* pc)
{
assert(pc);
free(pc->data);
pc->data = NULL;
}
void CheckCapacity(Contact* pc)
{
if (pc->count == pc->capacity)
{
PeoInfo* tmp = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo));
if (tmp == NULL)
{
printf("AddContact:%s\n", strerror(errno));
exit(-1);
}
pc->data = tmp;
pc->capacity += INC_SZ;
printf("增容成功\n");
}
}
void AddContact(Contact* pc)
{
assert(pc);
//增容
CheckCapacity(pc);
printf("请输入名字:->");
scanf("%s", pc->data[pc->count].name);
printf("请输入年龄:->");
scanf("%d", &(pc->data[pc->count].age));
printf("请输入性别:->");
scanf("%s", pc->data[pc->count].sex);
printf("请输入电话:->");
scanf("%s", pc->data[pc->count].tele);
printf("请输入地址:->");
scanf("%s", pc->data[pc->count].addr);
pc->count++;
printf("增加成功\n");
}
void ShowContact(Contact* pc)
{
assert(pc);
int i = 0;
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < pc->count; i++)
{
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
static int FindByName(Contact* pc, char name[])
{
assert(pc);
int i = 0;
for (i = 0; i < pc->count; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}//static关键字修饰 该函数只能在.c文件中看到
void DelContact(Contact* pc)
{
char name[MAX_NAME] = { 0 };
assert(pc);
if (pc->count == 0)
{
printf("通讯录为空,没有信息可以删除\n");
}
printf("请输入要删除人的名字:->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要删除的人不存在\n");
return;
}
int i = 0;
for (i = pos; i < pc->count - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->count--;
printf("删除成功\n");
}
void SearchContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
printf("请输入要查找人的名字:->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
return;
}
printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-5d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = { 0 };
printf("请输入要修改人的名字:->");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要修改的人不存在\n");
return;
}
printf("要修改的人的信息已经查找到,接下来开始修改\n");
printf("请输入名字:->");
scanf("%s", pc->data[pos].name);
printf("请输入年龄:->");
scanf("%d", &(pc->data[pos].age));
printf("请输入性别:->");
scanf("%s", pc->data[pos].sex);
printf("请输入电话:->");
scanf("%s", pc->data[pos].tele);
printf("请输入地址:->");
scanf("%s", pc->data[pos].addr);
printf("修改成功\n");
}
int cmp_peo_by_name(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data, pc->count, sizeof(PeoInfo), cmp_peo_by_name);
printf("排序成功\n");
}
void SaveContact(Contact* pc)
{
assert(pc);
FILE* pfWrite = fopen("contact.txt", "wb");
if (pfWrite == NULL)
{
perror("SaveContact fail");
exit(-1);
}
int i = 0;
for (i = 0; i < pc->count; i++)
{
fwrite(pc->data + i, sizeof(PeoInfo), 1, pfWrite);
}
fclose(pfWrite);
pfWrite = NULL;
}
void LoadContact(Contact* pc)
{
FILE* pfRead = fopen("contact.txt", "rb");
if (pfRead == NULL)
{
perror("LoadContact fail");
exit(-1);
}
PeoInfo tmp = { 0 };
while (fread(&tmp, sizeof(PeoInfo), 1, pfRead) == 1)
{
CheckCapacity(pc);
pc->data[pc->count] = tmp;
pc->count++;
}
fclose(pfRead);
pfRead = NULL;
}
test.c
#include"contact.h"
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
void menu()
{
printf("***************************************\n");
printf("******** 1.add 2.del ******\n");
printf("******** 3.search 4.modify ******\n");
printf("******** 5.show 6.sort ******\n");
printf("******** 0.exit ******\n");
printf("***************************************\n");
}
int main()
{
int input = 0;
Contact con;
InitContact(&con);
LoadContact(&con);
do
{
menu();
printf("请选择:->");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
SaveContact(&con);
DestoryContact(&con);
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}