通讯录
- 一、基本思路及功能介绍
- 二、功能实现
- 1.基础菜单的实现
- 2.添加联系人信息功能实现
- 3.显示联系人信息功能实现
- 4.删除联系人信息功能实现
- 5.查找联系人信息功能实现
- 6.修改联系人信息功能实现
- 7.排序联系人信息功能实现
- 8.加载和保存联系人信息功能实现
- 三、源文件展示
- 1.test.c
- 2.contact.c
- 3.contact.h
一、基本思路及功能介绍
想要用C语言实现通讯录的功能,基本上涵概了C语言入门的一些知识运用,如:指针、结构体、文件操作、动态内存管理等相关的知识;
实现一个简单的通讯录的基本功能:
1.添加联系人信息;2.删除联系人信息;3.查找联系人信息;4.修改联系人信息;5.显示联系人信息;6.排序联系人信息; 7.保存联系人信息;8.退出通讯录;
二、功能实现
1.基础菜单的实现
void menu()
{printf("\t\t\t\t************************************\n");printf("\t\t\t\t****** 通讯录管理系统 ********\n");printf("\t\t\t\t************************************\n");printf("\t\t\t\t* 1.添加联系人 *\n");printf("\t\t\t\t* 2.删除联系人 *\n");printf("\t\t\t\t* 3.查找联系人 *\n");printf("\t\t\t\t* 4.修改联系人 *\n");printf("\t\t\t\t* 5.显示联系人 *\n");printf("\t\t\t\t* 6.排序联系人 *\n");printf("\t\t\t\t* 0.退出通讯录 *\n");printf("\t\t\t\t************************************\n");printf("\t\t\t\t************************************\n");
}
enum
{EXIT,ADD,DEL,SEARCH,MODIFY,SHOW,SORT
};
int main()
{int input = 0;struct Contact con;InitContact(&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);//保存信息DistroyContact(&con);//销毁通讯录信息printf("退出通讯录\n");break;default:printf("选择错误,请重新选择\n");break;}} while (input);return 0;
}
2.添加联系人信息功能实现
//扩充容量
void CheckContact(struct Contact* pc)
{if (pc->sz == pc->capacity){struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(struct PeoInfo));if (ptr != NULL){pc->data = ptr;pc->capacity += 2;printf("扩容成功\n");}else{printf("扩容失败\n");exit(1);}}
}//初始化通讯录
void InitContact(struct Contact* pc)
{pc->sz = 0;pc->data = (struct PeoInfo*)malloc(DEFAULT_SZ * sizeof(struct PeoInfo));pc->capacity = DEFAULT_SZ;
}//添加联系人信息
void AddContact(struct Contact* pc)
{CheckContact(pc);printf("请输入联系人的姓名:>");scanf("%s", pc->data[pc->sz].name);printf("请输入联系人的年龄:>");scanf("%d", &pc->data[pc->sz].age);printf("请输入联系人的性别:>");scanf("%s", pc->data[pc->sz].sex);printf("请输入联系人的电话:>");scanf("%s", pc->data[pc->sz].tele);printf("请输入联系人的住址:>");scanf("%s", pc->data[pc->sz].addr);printf("添加成功\n");pc->sz++;
}
3.显示联系人信息功能实现
//显示联系人信息
void ShowContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,显示失败!!!\n");return;}int i = 0;printf("\t\t--------------------------------------------------------------------------\n");printf("\t\t--------------------------------------------------------------------------\n");printf("\t%15s\t%15s\t%15s\t%15s\t%15s\n\n","姓名", "年龄", "性别", "电话", "住址");for (i = 0; i < pc->sz; i++){printf("\t%15s\t%15d\t%15s\t%15s\t%15s\n",pc->data[i].name,pc->data[i].age,pc->data[i].sex,pc->data[i].tele,pc->data[i].addr);}printf("\t\t--------------------------------------------------------------------------\n");printf("\t\t--------------------------------------------------------------------------\n");
}
4.删除联系人信息功能实现
//删除联系人信息
int FindContactByName(const struct Contact* pc, char* name)
{int i = 0;for (i = 0; i < pc->sz; i++){if (strcmp(pc->data[i].name, name) == 0){return i;}}return -1;
}void DelContact(struct Contact* pc)
{if (pc->sz == 0){printf("联系人为空,操作失败\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要删除的联系人姓名:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("查找的联系人不存在!!!\n");}else{for (int i = pos; i < pc->sz - 1; i++){pc->data[i] = pc->data[i + 1];}pc->sz--;printf("删除成功\n");}
}
5.查找联系人信息功能实现
//查找联系人信息
void SearchContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,查找失败!!!\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要查找的联系人的名字:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("联系人不存在!!!\n");return;}else{printf("\t%15s\t%15s\t%15s\t%15s\t%15s\n\n","姓名", "年龄", "性别", "电话", "住址");printf("\t%15s\t%15d\t%15s\t%15s\t%15s\n",pc->data[pos].name,pc->data[pos].age,pc->data[pos].sex,pc->data[pos].tele,pc->data[pos].addr);}
}
6.修改联系人信息功能实现
//修改联系人信息
void ModifyContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,查找失败!!!\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要修改的联系人姓名:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("联系人不存在!!!\n");return;}else{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");
}
7.排序联系人信息功能实现
//排序联系人信息
void SortContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,无法排序!!!\n");return;}int i = 0;int j = 0;struct PeoInfo tmp;for (i = 0; i < pc->sz - 1; i++){int flag = 1;for (j = 0; j < pc->sz - i - 1; j++){if (strcmp(pc->data[j].name, pc->data[j + 1].name) < 0){tmp = pc->data[j];pc->data[j] = pc->data[j + 1];pc->data[j + 1] = tmp;flag = 0;}}if (flag == 1){return;}}printf("排序成功\n");
}
8.加载和保存联系人信息功能实现
//加载有效信息到通讯录中
void LoadContact(struct Contact* pc)
{FILE* pf = fopen("contact.txt", "rb");if (pf == NULL){perror("contact:fopen");return;}struct PeoInfo tmp = { 0 };while (fread(&tmp, sizeof(struct PeoInfo), 1, pf)){CheckContact(pc);pc->data[pc->sz] = tmp;pc->sz++;}fclose(pf);pf = NULL;
}//保存信息
void SaveContact(struct Contact* pc)
{//1.打开文件FILE* pf = fopen("contact.txt", "wb");if (pf == NULL){perror("SaveContact:fopen");return;}//2.写数据int i = 0;for (i = 0; i < pc->sz; i++){fwrite(&(pc->data[i]), sizeof(struct PeoInfo), 1, pf);}//3.关闭文件fclose(pf);pf = NULL;
}
三、源文件展示
1.test.c
void menu()
{printf("\t\t\t\t************************************\n");printf("\t\t\t\t****** 通讯录管理系统 ********\n");printf("\t\t\t\t************************************\n");printf("\t\t\t\t* 1.添加联系人 *\n");printf("\t\t\t\t* 2.删除联系人 *\n");printf("\t\t\t\t* 3.查找联系人 *\n");printf("\t\t\t\t* 4.修改联系人 *\n");printf("\t\t\t\t* 5.显示联系人 *\n");printf("\t\t\t\t* 6.排序联系人 *\n");printf("\t\t\t\t* 0.退出通讯录 *\n");printf("\t\t\t\t************************************\n");printf("\t\t\t\t************************************\n");
}
enum
{EXIT,ADD,DEL,SEARCH,MODIFY,SHOW,SORT
};
int main()
{int input = 0;struct Contact con;InitContact(&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);//保存信息DistroyContact(&con);//销毁通讯录信息printf("退出通讯录\n");break;default:printf("选择错误,请重新选择\n");break;}} while (input);return 0;
}
2.contact.c
#include "contact.h"//扩充容量
void CheckContact(struct Contact* pc)
{if (pc->sz == pc->capacity){struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(struct PeoInfo));if (ptr != NULL){pc->data = ptr;pc->capacity += 2;printf("扩容成功\n");}else{printf("扩容失败\n");exit(1);}}
}//加载有效信息到通讯录中
void LoadContact(struct Contact* pc)
{FILE* pf = fopen("contact.txt", "rb");if (pf == NULL){perror("contact:fopen");return;}struct PeoInfo tmp = { 0 };while (fread(&tmp, sizeof(struct PeoInfo), 1, pf)){CheckContact(pc);pc->data[pc->sz] = tmp;pc->sz++;}fclose(pf);pf = NULL;
}//初始化通讯录
void InitContact(struct Contact* pc)
{pc->sz = 0;pc->data = (struct PeoInfo*)malloc(DEFAULT_SZ * sizeof(struct PeoInfo));pc->capacity = DEFAULT_SZ;LoadContact(pc);//加载有效信息到通讯录中
}//添加联系人信息
void AddContact(struct Contact* pc)
{CheckContact(pc);printf("请输入联系人的姓名:>");scanf("%s", pc->data[pc->sz].name);printf("请输入联系人的年龄:>");scanf("%d", &pc->data[pc->sz].age);printf("请输入联系人的性别:>");scanf("%s", pc->data[pc->sz].sex);printf("请输入联系人的电话:>");scanf("%s", pc->data[pc->sz].tele);printf("请输入联系人的住址:>");scanf("%s", pc->data[pc->sz].addr);printf("添加成功\n");pc->sz++;
}//销毁通讯录
void DistroyContact(struct Contact* pc)
{free(pc->data);pc->data = NULL;pc->sz = 0;pc->capacity = 0;
}//显示联系人信息
void ShowContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,显示失败!!!\n");return;}int i = 0;printf("\t\t--------------------------------------------------------------------------\n");printf("\t\t--------------------------------------------------------------------------\n");printf("\t%15s\t%15s\t%15s\t%15s\t%15s\n\n","姓名", "年龄", "性别", "电话", "住址");for (i = 0; i < pc->sz; i++){printf("\t%15s\t%15d\t%15s\t%15s\t%15s\n",pc->data[i].name,pc->data[i].age,pc->data[i].sex,pc->data[i].tele,pc->data[i].addr);}printf("\t\t--------------------------------------------------------------------------\n");printf("\t\t--------------------------------------------------------------------------\n");
}//删除联系人信息
int FindContactByName(const struct Contact* pc, char* name)
{int i = 0;for (i = 0; i < pc->sz; i++){if (strcmp(pc->data[i].name, name) == 0){return i;}}return -1;
}void DelContact(struct Contact* pc)
{if (pc->sz == 0){printf("联系人为空,操作失败\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要删除的联系人姓名:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("查找的联系人不存在!!!\n");}else{for (int i = pos; i < pc->sz - 1; i++){pc->data[i] = pc->data[i + 1];}pc->sz--;printf("删除成功\n");}
}//查找联系人信息
void SearchContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,查找失败!!!\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要查找的联系人的名字:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("联系人不存在!!!\n");return;}else{printf("\t%15s\t%15s\t%15s\t%15s\t%15s\n\n","姓名", "年龄", "性别", "电话", "住址");printf("\t%15s\t%15d\t%15s\t%15s\t%15s\n",pc->data[pos].name,pc->data[pos].age,pc->data[pos].sex,pc->data[pos].tele,pc->data[pos].addr);}
}//修改联系人信息
void ModifyContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,查找失败!!!\n");return;}char name[NAME_MAX] = { 0 };printf("请输入要修改的联系人姓名:>");scanf("%s", name);int pos = FindContactByName(pc, name);if (pos == -1){printf("联系人不存在!!!\n");return;}else{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");
}//排序联系人信息
void SortContact(struct Contact* pc)
{if (pc->sz == 0){printf("通讯录为空,无法排序!!!\n");return;}int i = 0;int j = 0;struct PeoInfo tmp;for (i = 0; i < pc->sz - 1; i++){int flag = 1;for (j = 0; j < pc->sz - i - 1; j++){if (strcmp(pc->data[j].name, pc->data[j + 1].name) < 0){tmp = pc->data[j];pc->data[j] = pc->data[j + 1];pc->data[j + 1] = tmp;flag = 0;}}if (flag == 1){return;}}printf("排序成功\n");
}//保存信息
void SaveContact(struct Contact* pc)
{//1.打开文件FILE* pf = fopen("contact.txt", "wb");if (pf == NULL){perror("SaveContact:fopen");return;}//2.写数据int i = 0;for (i = 0; i < pc->sz; i++){fwrite(&(pc->data[i]), sizeof(struct PeoInfo), 1, pf);}//3.关闭文件fclose(pf);pf = NULL;
}
3.contact.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define NAME_MAX 20
#define SEX_MAX 20
#define TELE_MAX 20
#define ADDR_MAX 20
#define DEFAULT_SZ 3struct PeoInfo
{char name[NAME_MAX];//姓名int age; //年龄char sex[SEX_MAX]; //性别char tele[TELE_MAX];//电话char addr[ADDR_MAX];//住址
};struct Contact
{struct PeoInfo* data;//能够动态存储联系人的信息int sz; //记录当前联系人的个数int capacity; //记录当前通讯录的容量
};//初始化通讯录
void InitContact(struct Contact* pc);//添加联系人信息
void AddContact(struct Contact* pc);//销毁通讯录信息
void DistroyContact(struct Contact* pc);//显示联系人信息
void ShowContact(struct Contact* pc);//删除联系人信息
void DelContact(struct Contact* pc);//查找联系人信息
void SearchContact(struct Contact* pc);//修改联系人信息
void ModifyContact(struct Contact* pc);//排序联系人信息
void SortContact(struct Contact* pc);//加载有效信息到通讯录中
void LoadContact(struct Contact* pc);//保存信息
void SaveContact(struct Contact* pc);
#四、演示效果
没有联系人
存入联系人
查找联系人
修改联系人
显示联系人
排序联系人
退出通讯录