C语言实现通讯录的示例代码
一、Contact.h文件
包含函数的声明
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 20
#define MAX_ADDR 30
#define MAX_LEG 5
//结构体的定义,用于储存通讯录数据
struct Contact
{
char name[MAX_NAME];
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
int age;
};
struct PeoInfo
{
struct Contact* data;
int length;
int capacity;
};
//定义游戏菜单函数
void menu();
//定义初始结构体函数
void InitContact(struct PeoInfo* abs);
//添加通讯录消息函数的声明
void AddConInfo(struct PeoInfo* abs);
//查询信息是否存在函数
int IsExist(struct PeoInfo* abs, char* name);
//删除指定信息函数的定义
void DelConInfo(struct PeoInfo* abs);
//修改指定信息函数的定义
void ModefiInfo(struct PeoInfo* abs);
//查询指定信息函数的定义
void SearchInfo(struct PeoInfo* abs);
//展示通讯录信息函数的定义
void ShowInfo(struct PeoInfo* abs);
//清空通讯录列表函数的定义
void ClearInfo(struct PeoInfo* abs);
//排序通讯录信息函数的定义
void SortInfo(struct PeoInfo* abs);
//扩容函数的定义
int IncMemmery(struct PeoInfo* abs);
//释放内存函数的定义
void FreeInfo(struct PeoInfo* abs);
//保存通讯录函数的定义
void saveInfo(struct PeoInfo* abs);
//加载通讯录函数的定义
void loadInfo(struct PeoInfo* abs);
二、Contact.c文件
包含函数的实现
#include "contact.h";
//游戏菜单函数的实现
void menu()
{
printf("***********************************\n");
printf("*******1.Add 2.Del********\n");
printf("*******3.Modefi 4.Search*****\n");
printf("*******5.show 6.Clear******\n");
printf("*******7.sort 0.exit*******\n");
printf("***********************************\n");
}
//扩容函数的实现
int IncMemmery(struct PeoInfo* abs)
{
struct Contact* ptr = (struct Contact*)realloc(abs->data, (abs->capacity + 3) * sizeof(struct Contact));
if (ptr == NULL)
{
perror("GetMemmery():");
return -1;
}
else
{
abs->data = ptr;
abs->capacity += 3;
return 1;
}
}
//初始化通讯录函数的实现
void loadInfo(struct PeoInfo* abs)
{
FILE* pf;
pf = fopen("Contact.txt", "rb");
if (pf == NULL)
{
perror("InitContact():");
return;
}
struct Contact tmp = { 0 };
while (fread(&tmp, sizeof(struct Contact), 1, pf))
{
if (abs->length == abs->capacity)
{
IncMemmery(abs);
}
*(abs->data + abs->length) = tmp;
abs->length++;
}
fclose(pf);
pf == NULL;
}
void InitContact(struct PeoInfo* abs)
{
assert(abs);
abs->length = 0;
abs->data = (struct Contact*)malloc(MAX_LEG * sizeof(struct Contact));
abs->capacity = MAX_LEG;
loadInfo(abs);
}
//查询信息是否存在函数的实现
int IsExist(struct PeoInfo* abs, char* name)
{
for (int i = 0; i < abs->length; i++)
{
if (strcmp(abs->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
//添加通讯录消息函数的实现
void AddConInfo(struct PeoInfo* abs)
{
assert(abs);
if (abs->capacity == abs->length)
{
int ret = IncMemmery(abs);
if (ret == 1)
{
printf("扩容成功!\n");
}
else
{
printf("扩容失败,内存不足!\n");
return;
}
}
printf("请输入添加的姓名:>");
scanf("%s", abs->data[abs->length].name);
printf("请输入添加的姓别:>");
scanf("%s", abs->data[abs->length].sex);
printf("请输入添加的联系方式:>");
scanf("%s", abs->data[abs->length].tele);
printf("请输入添加的住址:>");
scanf("%s", abs->data[abs->length].addr);
printf("请输入添加的年龄:>");
scanf("%d", &(abs->data[abs->length].age));
abs->length++;
printf("已成功添加联系人!\n");
}
//删除指定信息函数的实现
void DelConInfo(struct PeoInfo* abs)
{
assert(abs);
char name[MAX_NAME];
printf("请输入要删除通讯录的姓名:>");
scanf("%s", name);
int ret = IsExist(abs, name);
if (ret == -1)
{
printf("不存在此联系人!\n");
}
else
{
for (int i = ret; i < abs->length; i++)
{
abs->data[i] = abs->data[i + 1];
}
abs->length--;
}
}
//修改制定信息函数的实现
void ModefiInfo(struct PeoInfo* abs)
{
char name[MAX_NAME];
printf("请输入要修改通讯录信息的姓名:>");
scanf("%s", name);
int ret = IsExist(abs, name);
if (ret == -1)
{
printf("要修改指定联系人不存在!\n");
}
else
{
printf("请输入要修改的姓名:>");
scanf("%s", abs->data[ret].name);
printf("请输入要修改的性别:>");
scanf("%s", abs->data[ret].sex);
printf("请输入要修改的联系方式:>");
scanf("%s", abs->data[ret].tele);
printf("请输入要修改的住址:>");
scanf("%s", abs->data[ret].addr);
printf("请输入要修改的年龄:>");
scanf("%d", &(abs->data[ret].age));
printf("修改成功!\n");
}
}
//查询指定信息函数的实现
void SearchInfo(struct PeoInfo* abs)
{
assert(abs);
char name[MAX_NAME];
printf("请输入要查询通讯录的姓名:>");
scanf("%s", name);
int ret = IsExist(abs, name);
if (ret == -1)
{
printf("要查找的信息不存在!");
}
else
{
printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10s\n","姓名","性别","联系方式","住址","年龄");
printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10d\n",
abs->data[ret].name,
abs->data[ret].sex,
abs->data[ret].tele,
abs->data[ret].addr,
abs->data[ret].age);
printf("查询成功!\n");
}
}
//展示通讯录信息函数的实现
void ShowInfo(struct PeoInfo* abs)
{
assert(abs);
printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10s\n", "姓名", "性别", "联系方式", "住址", "年龄");
for (int i = 0; i < abs->length; i++)
{
printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10d\n",
abs->data[i].name,
abs->data[i].sex,
abs->data[i].tele,
abs->data[i].addr,
abs->data[i].age);
}
}
//清空通讯录列表函数的实现
void ClearInfo(struct PeoInfo* abs)
{
assert(abs);
struct Contact* ptr = (struct Contact*)realloc(abs->data, MAX_LEG * (sizeof(struct Contact)));
if (ptr == NULL)
{
perror("ClearInfo():");
}
else
{
abs->length = 0;
abs->data = ptr;
memset(abs->data, 0, MAX_LEG * sizeof(struct Contact));
abs->capacity = MAX_LEG;
printf("清空通讯录成功!\n");
}
}
//排序通讯录函数的实现
int CmpByAge(const void* e1, const void* e2)
{
return ((struct Contact*)e1)->age - ((struct Contact*)e2)->age;
}
int CmpByName(const void* e1, const void* e2)
{
return strcmp(((struct Contact*)e1)->name, ((struct Contact*)e2)->name);
}
void SortInfo(struct PeoInfo* abs)
{
getchar();
char ch;
printf("请输入排序的方式:>N(姓名)、A(年龄),N or A:>");
ch = getchar();
if (ch == 'A')
{
qsort(abs->data, abs->length, sizeof(struct Contact), CmpByAge);
printf("已按照年龄排序成功!\n");
}
else if(ch == 'N')
{
qsort(abs->data, abs->length, sizeof(struct Contact), CmpByName);
printf("已按照姓名排序成功!\n");
}
else
{
printf("输入错误!\n");
}
}
//释放内存函数的实现
void FreeInfo(struct PeoInfo* abs)
{
free(abs->data);
abs->data = NULL;
}
//保存通讯录函数的实现
void saveInfo(struct PeoInfo* abs)
{
FILE* pf;
pf = fopen("Contact.txt", "wb");
if (pf == NULL)
{
perror("saveInfo():");
return;
}
for (int i = 0; i < abs->length; i++)
{
fwrite(abs->data + i, sizeof(struct Contact), 1, pf);
}
fclose(pf);
pf = NULL;
}
三、test.c文件
包含主函数即代码思想
#include "contact.h";
int main()
{
int input = 0;
struct PeoInfo con;
//初始化结构体
InitContact(&con);
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
AddConInfo(&con);
break;
case 2:
DelConInfo(&con);
break;
case 3:
ModefiInfo(&con);
break;
case 4:
SearchInfo(&con);
break;
case 5:
ShowInfo(&con);
break;
case 6:
ClearInfo(&con);
break;
case 7:
SortInfo(&con);
break;
case 0:
saveInfo(&con);
FreeInfo(&con);
printf("退出通讯录!");
break;
default:
printf("选择错误!\n");
}
} while (input);
return 0;
}
到此这篇关于C语言实现通讯录的示例代码的文章就介绍到这了,更多相关C语言通讯录内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章