C语言实现动态内存通讯录 (文件版本)

C语言实现动态内存通讯录 (文件版本

主要功能

  • 实现通讯录主要功能,添加联系人,删除联系人,查询联系人,修改联系人信息,信息通过链表储存每次关闭程序时会将数据储存到文件中,下一次启动程序会读入文件中的数据。

头文件及函数声明


#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<errno.h>
#define MAX_NAME 10
#define MAX_TELE 30
#define MAX_SEX 5
#define MAX_ADDRESS 30
struct type* Add(struct type* P);
struct type* Initialization();
void Query(struct type* P);
void Show(struct type* P);
struct type* Del(struct type* P);
struct type* Modify(const struct type* P);
void Save(struct type* P);
struct type* Read(struct type* P);
void Menu();
void Menu2();
enum 
{
	exit,//退出
	add,//添加
	del,//删除
	query,//查询
	modify,//修改
	show,//遍历输出
	save,//保存到系统文件
};
struct contact 
{
	char name[MAX_NAME];//储存联系人名字
	char telnumber[MAX_TELE];//储存联系人电话号码
	char sex[MAX_SEX];//储存联系人性别
	char address[MAX_ADDRESS];//储存联系人住址
	struct contact* next;//储存下一个节点
};
struct type
{
	int size;//记录通讯录大小
	struct contact* fist;//记录头地址
	struct contact* pcontact;//临时变量
	struct contact* end;//记录尾指针
};   

函数的实现

#include"contact.h"
void Menu()
{
	printf("************************************\n");
	printf("*****1.添加联系人**2.删除联系*******\n");
	printf("*****3.查找联系人**4.修改联系人信息*\n");
	printf("*****5.查看联系人**6.保存***********\n");
	printf("***********0.退出*******************\n");
	printf("************************************\n");
}
void Menu2()
{
	printf("--------------------\n");
	printf("---1.姓名--2.电话---\n");
	printf("---3.性别--4.住址---\n");
	printf("--------------------\n");
}
//通讯录初始化
struct type* Initialization()
{
	struct contact* p = (struct contact*)malloc(sizeof(struct contact));
	struct type* sp = (struct type*)malloc(sizeof(struct type));
	sp->fist = p;
	sp->pcontact = p;
	p->next = NULL;
	sp->end = p->next;
	sp->size = 1;
	printf("初始化成功...\n");
	return sp;
}
//添加联系人
struct type* Add(struct type* p)
{
	struct contact* fp = (struct contact*)malloc(sizeof(struct contact));
	if (fp == NULL)
	{
		printf("添加空间失败...\n");
		return 0;
	}
	else
	{
		printf("添加空间成功...\n");
		printf("请输入联系人姓名:>");
		scanf("%s", fp->name);
		printf("请输入联系人电话:>");
		scanf("%s", fp->telnumber);
		printf("请输入联系人性别:>");
		scanf("%s", fp->sex);
		printf("请输入联系人住址:>");
		scanf("%s", fp->address);
		fp->next = NULL;
		p->end = fp->next;
		p->pcontact->next = fp;
		p->pcontact = fp;
		p->size++;

	}
	return p;
}
//遍历输出联系人信息
void Show(struct type* p)
{
	int i = 0;
	struct contact* fp = p->fist->next;
	for (i = 0; i < (p->size - 1); i++)
	{
		printf("姓名:%-10s 电话号:%-15s 性别:%-5s 住址:%-20s\n", fp->name, fp->telnumber, fp->sex, fp->address);
		if (fp->next == NULL)
		{
			return ;
		}
		else 
		{
			fp = fp->next;
		}
	}
}
//查询联系人信息
void Query(struct type* p)
{
	struct contact* fp = p->fist->next;
	int i = 0;
	int j = 0;
	char name[MAX_NAME] = { 0 };
	printf("请输入要查询的联系人名字:>");
	scanf("%s", name);
	printf("查询中...\n");
	for (i = 0; i < (p->size-1); i++)
	{
		j = strcmp(fp->name, name);
		if (j == 0)
		{
			printf("姓名:%-10s 电话号:%-15s 性别:%-5s 住址:%-20s\n", fp->name, fp->telnumber, fp->sex, fp->address);
			return ;
		}
		if (i == p->size - 2)
		{
			printf("通讯录中没有此联系人....\n");
		}
		else
		{
			fp = fp->next;
		}
	}
}
//删除联系人
struct type* Del(struct type* p)
{
	struct contact* fp = p->fist->next;
	struct contact* sp = p->fist;
	int i = 0;
	int j = 0;
	int a = 0;
	char name[MAX_NAME] = { 0 };
	printf("请输入要删除的联系人名字:>");
	scanf("%s", name);
	printf("删除中...\n");
	for (i = 1; i < p->size; i++)
	{
		j = strcmp(fp->name, name);
		if (j == 0)
		{
			sp->next = fp->next;
			p->size--;
			free(fp);
			fp = NULL;
			sp = NULL;
			printf("删除成功...\n");
			break;
		}
		else
		{
			fp = fp->next;
			sp = sp->next;
		}
	}
	return p;
}
//修改联系人信息
struct type* Modify(struct type* p)
{
	struct contact* fp = p->fist->next;
	int i = 0;
	int j = 0;
	int input = 0;
	char name[MAX_NAME] = { 0 };
	printf("请输入要修改信息的联系人名字:>");
	scanf("%s", name);
	printf("查询中...\n");
	for (i = 0; i < (p->size-1); i++)
	{
		j = strcmp(fp->name, name);
		if (j == 0)
		{
			printf("请选择要修改的内容...\n");
			Menu2();
			scanf("%d", &input);
			switch (input)
			{
			case 1:
				printf("姓名修改为:>");
				scanf("%s", fp->name);
				printf("修改成功...\n");
			case 2:
				printf("联系人电话修改为:>");
				scanf("%s", fp->telnumber);
				break;
			case 3:
				printf("性别修改为:>");
				scanf("%s", fp->sex);
				break;
			case 4:
				printf("联系人住址修改为:>");
				scanf("%s", fp->address);
			default:
				break;
			}
			return p;
		}
		else
		{
			fp = fp->next;
		}
	}
	return p;
}
//保存联系人信息 
void Save(struct type* p)
{
	int i = 0;
	struct contact* sp = p->fist->next;
	FILE* fp = fopen("contact.txt", "wb");
	if (fp == NULL)
	{
		return 0;
	}
	for (i = 0; i < p->size - 1; i++)
	{
		fwrite(sp, sizeof(struct contact), 1, fp);
		sp = sp->next;
	}
	fclose(fp);
	printf("储存成功...\n");

}
//从文件中读取数据
struct type* Read(struct type* p)
{
	
	int i = 1;
	FILE* fp = fopen("contact.txt", "rb");
	if (fp == NULL)
	{
		return p;
	}
	while (i)
	{
		struct contact* sp = (struct contact*)malloc(sizeof(struct contact));
		i = fread(sp, sizeof(struct contact), 1, fp);
		if (i != 0)
		{
			sp->next = NULL;
		    p->end = sp->next;
		    p->pcontact->next = sp;
		    p->pcontact = sp;
       		p->size++;
		}
		
	}
	fclose(fp);
	printf("读取成功...\n");
	return p;
}	

程序主体

#include"contact.h"
int main()
{
	int input = 0;
	struct type* p;
	p = Initialization();
	p = Read(p);
	do
	{
		Menu();
		printf("输入:>");
		scanf("%d", &input);
		switch (input)
		{
		case(add)://添加联系人信息
			p=Add(p);
			printf("添加联系人信息成功...\n");
			break;
		case(del)://删除联系人信息
			p=Del(p);
			break;
		case(query)://查询联系人信息
			Query(p);
			break;
		case(exit)://退出软件
			Save(p);
			free(p);
			printf("感谢使用...");
			break;
		case(show)://遍历输出联系人信息
			Show(p);
			break;
		case(save):
			Save(p);//将数据储存在文件夹中
			break;
		case(modify):
			p = Modify(p);//修改联系人信息
			break;
		default://输入错误判定
			printf("选择错误请重新选择\n");
			break;
		}
	} while (input);
	return 0;
}

GitHub地址

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值