如何用C语言完成一个简单的通讯录项目?
First
我们首先得了解一个通讯录有哪些功能?如何用代码实现这些功能?
1.通讯录的目录。
2.联系人的添加。
3.练习人的删除。
4.通讯录的修改。
5.保存通讯录到文件。
6.读取通讯录信息。
7.联系人的查找。
通讯录的大小和基本信息
typedef struct AddressInfo//通讯录的基本信息
{
char _name[24];
char _tel[24];
int _age;
enum SEX _sex;
}AddressInfo;
#define MAX_SIZE 100
typedef struct AddressBook
{
AddressInfo* _AIArray;
size_t _size; // 有效数据个数
size_t _capacity; // 容量
}AddressBook;
通讯录的目录:
int main()
{
AddressBook addressbook;
AddressBookInit(&addressbook);
AddressInfo info1 = { "张三", "110", 18, Man };
AddressBookAdd(&addressbook, &info1);
AddressInfo info2 = { "小王", "111", 19, Man };
AddressBookAdd(&addressbook, &info2);
AddressInfo info3 = { "小李", "111", 19, Man };
AddressBookAdd(&addressbook, &info3);
//AddressBookLoad(&addressbook, "addressfile.bin");
AddressBookSave(&addressbook, "addressfile.txt");
printf("==============================\n");
int option = 0;
do{
printf("1.添加通讯录信息,2.查找通讯录信息\n");
printf("3.删除通讯录信息,4.修改通讯录信息\n");
printf("5.保存通讯录到文件,6.读取通讯录信息\n");
printf("7.打印通讯录信息,-1.退出\n");
scanf("%d", &option);
if (option == 1)
{
printf("请依次输入姓名,电话,年龄,性别:\n");
AddressInfo info;
scanf("%s",info._name);
scanf("%s", info._tel);
scanf("%d", &info._age);
char sex[10];
scanf("%s", &sex);
if (strcmp(sex, "男"))
info._sex == 0;
else
info. _sex == 1;
AddressBookAdd(&addressbook, &info);
}
else if (option == 2)
{
char name[20];
printf("请输入查找联系人的姓名:\n");
scanf("%s", name);
AddressInfo *ret = AddressBookFind(&addressbook, name);
if (ret == NULL)
{
printf("查无此人\n");
}
else
{
printf("姓名:%s", ret->_name);
printf("电话:%s", ret->_tel);
printf("年龄:%d", ret->_age);
printf("性别:%d", ret->_sex);
}
}
else if (option == 3)
{
char name[20];
printf("请输入要删除的联系人的姓名:\n");
scanf("%s", name);
AddressBookDel(&addressbook, name);
}
else if (option == 4)
{
char name[20];
printf("请输入修改联系人的信息:\n");
scanf("%s", name);
AddressInfo *ret = AddressBookFind(&addressbook, name);
if (ret == NULL)
{
printf("查无此人");
}
else
{
printf("请输入修改联系人的电话:\n");
scanf("%s", ret->_tel);
}
}
else if (option == 5)
{
AddressBookSave(&addressbook, "addressfile.bin");
}
else if (option == 6)
{
addressbook._size = 0;
AddressBookLoad(&addressbook, "addressfile.bin");
}
else if (option == 7)
{
AddressBookPrint(&addressbook);
}
} while (option >= 0);
printf("==============================");
return 0;
}
各个功能的实现:
void AddressBookInit(AddressBook* pbook)
{
assert(pbook);
pbook->_size = 0;
pbook->_capacity = 2;
pbook->_AIArray = (AddressInfo*)malloc(sizeof(AddressInfo)* 2);
assert(pbook->_AIArray);
}
void AddressBookDestoty(AddressBook *pbook)//空间的释放
{
assert(pbook);
free(pbook);
pbook->_AIArray = NULL;
pbook->_size = pbook->_capacity = 0;
}
void AddressBookAdd(AddressBook* pbook, AddressInfo* pinfo)
{
assert(pbook);
if (pbook->_size == pbook->_capacity)
{
// 空间满了,进行扩容
pbook->_AIArray = realloc(pbook->_AIArray, pbook->_capacity * 2 * sizeof(AddressInfo));//扩容二倍
pbook->_capacity *= 2;
}
strcpy(pbook->_AIArray[pbook->_size]._name, pinfo->_name);
strcpy(pbook->_AIArray[pbook->_size]._tel, pinfo->_tel);
pbook->_AIArray[pbook->_size]._age = pinfo->_age;
pbook->_AIArray[pbook->_size]._sex = pinfo->_sex;
pbook->_size++;
}
void AddressBookSave(AddressBook* pbook, const char* filename)
{
assert(pbook);
FILE* fp = fopen(filename, "w");//文件的写入
for (size_t i = 0; i < pbook->_size; ++i)
{
fputs(pbook->_AIArray[i]._name, fp);
fputc('\n', fp);
char telstr[20];
itoa(pbook->_AIArray[i]._tel, telstr,10);
fputs(pbook->_AIArray[i]._tel, fp);
fputc('\n', fp);
}
AddressInfo end;
end._age = -1;
fwrite(&end, sizeof(AddressInfo), 1, fp);
}
void AddressBookLoad(AddressBook* pbook, const char* filename)
{
assert(pbook);
FILE* fp = fopen(filename, "r");//文件的读入
AddressInfo info;
while (1)
{
info._name[0] = '\0';
info._tel[0] = '\0';
info._age = 0;
fscanf("%s%s%d",info._name,info._tel,info._age);
if (info._age == -1)
{
break;
}
else
{
AddressBookAdd(pbook, &info);
}
}
}
void AddressBookDel(AddressBook* pbook, const char* name)
{
for (size_t i = 0; i < pbook->_size; ++i)
{
if (strcmp(pbook->_AIArray[i]._name, name) == 0)
{
while (i < pbook->_size - 1)
{
strcpy(pbook->_AIArray[i]._name, pbook->_AIArray[i + 1]._name);//把后一个信息拷贝到目标对象中去,进行覆盖。
strcpy(pbook->_AIArray[i]._tel, pbook->_AIArray[i + 1]._tel);
pbook->_AIArray[i]._age = pbook->_AIArray[i + 1]._age;
pbook->_AIArray[i]._sex = pbook->_AIArray[i + 1]._sex;
++i;
}
pbook->_size--;
break;
}
}
}
AddressInfo* AddressBookFind(AddressBook* pbook, const char* name)
{
assert(pbook);
for (size_t i = 0; i < pbook->_size; ++i)
{
if (strcmp(pbook->_AIArray[i]._name , name)==0)//使用库函数进行比较
{
return &pbook->_AIArray[i];
}
}
return NULL;
}
void AddressBookPrint(AddressBook* pbook)
{
assert(pbook);
for (size_t i = 0; i < pbook->_size; ++i)
{
//printf("======================================\n");
printf("name:%s\n", pbook->_AIArray[i]._name);
printf("tel:%s\n", pbook->_AIArray[i]._tel);
printf("age:%d\n", pbook->_AIArray[i]._age);
printf("sex:%d\n", pbook->_AIArray[i]._sex);
printf("======================================\n");
printf("\n");
}
}
实现效果: