实现一个通讯录,可以实现文件存储

静态通讯录https://blog.csdn.net/Damn_Yang/article/details/83583962

动态通讯录https://blog.csdn.net/Damn_Yang/article/details/83587824

使用I/O流的概念,并使用IO函数,完成通讯录的文件存储

Contact.h

#ifndef __Contact_H__
#define __Contact_H__

#pragma warning(disable:4996)

#include
#include
#include
#include
enum SLECT
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	ALERT,
	SHOW,
	SORT,
	EMPTY
};
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_ADDR 30
#define MAX_TELE 20
#define MAX 2


typedef struct People
{
	char name[MAX_NAME];
	short agel;
	char sex[MAX_SEX];
	char tele[MAX_TELE];
	char addr[MAX_ADDR];
}People;
typedef struct Contact
{
	People *data;
	unsigned int sz;
	int capacity;
}Contact;

void AddPeople(Contact *ps);
int SearchPeople(Contact *ps);
void DelePeople(Contact *ps);
void AlertPeople(Contact *ps);
void ShowPeople(Contact *ps);
void SortPeople(Contact *ps);
void EmptyPeople(Contact *ps);
void InitContact(Contact *ps);
void CheckCapacity(Contact *ps);
void SaveContcat(Contact *ps);
void LoadContcat(Contact *ps);



#endif 

2.Contact.c

#include"contact.h"
//添加联系人
void AddPeople(Contact *ps)
{
	assert(ps != NULL);
	CheckCapacity(ps);
	printf("请输入姓名:");
	scanf("%s", ps->data[ps->sz].name);
	printf("请输入性别:");
	scanf("%s", ps->data[ps->sz].sex);
	printf("请输入年龄:");
	scanf("%d", &ps->data[ps->sz].agel);
	printf("请输入电话:");
	scanf("%s", ps->data[ps->sz].tele);
	printf("请输入地址:");
	scanf("%s", ps->data[ps->sz].addr);
	ps->sz++;
	printf("添加成功\n");


}
//查找联系人
int SearchPeople(Contact *ps)
{
	char names[20];
	printf("请输入姓名:");
	scanf("%s", names);
	size_t i = 0;
	for (; i < ps->sz; i++)
	{
		if (0 == strcmp(ps->data[i].name, names))
		{
			return i;
		}
	}
	return -1;

}
int FindPeople(Contact *ps)
{
	char names[20];
	printf("请输入姓名:");
	scanf("%s", names);
	size_t i = 0;
	for (; i < ps->sz; i++)
	{
		if (0 == strcmp(ps->data[i].name, names))
		{
			return i;
		}
	}
	return -1;
}
//删除联系人
void DelePeople(Contact *ps)
{
	if (ps->sz == 0)
	{
		printf("通讯录已空\n");
		return;
	}
	else
	{
		int ret = FindPeople(ps);
		if (ret < 0)
		{
			printf("查无此人\n");
			return;
		}
		else
		{
			size_t i = ret;
			for (; i < ps->sz - 1; i++)
			{
				ps->data[i] = ps->data[i + 1];
			}
			ps->sz--;
		}
	}

}

//修改联系人信息
void AlertPeople(Contact *ps)
{
	int ret = FindPeople(ps);
	if (ret < 0)
	{
		printf("查无此人\n");
		return;
	}
	else
	{
		printf("请输入要修改的姓名:");
		scanf("%s", ps->data[ret].name);
		printf("请输入要修改的性别:");
		scanf("%s", ps->data[ret].sex);
		printf("请输入要修改的年龄:");
		scanf("%d", &ps->data[ret].agel);
		printf("请输入要修改的电话:");
		scanf("%s", ps->data[ret].tele);
		printf("请输入要修改的地址:");
		scanf("%s", ps->data[ret].addr);
		printf("修改成功\n");
	}

}
//显示所有联系人信息
void ShowPeople(Contact *ps)
{
	if (ps->sz == 0)
	{
		printf("此通讯录为空\n");
		return;
	}
	else
	{
		size_t i = 0;
		printf("%-5s\t%-5s\t%-3s\t%-12s\t%-12s\n", "名字", "性别", "年龄", "电话", "地址");
		for (; i < ps->sz; i++)
		{
			printf("%-5s\t%-5s\t%-3d\t%-12s\t%-17s\n",
				ps->data[i].name,
				ps->data[i].sex,
				ps->data[i].agel,
				ps->data[i].tele,
				ps->data[i].addr);
		}
	}

}

//按姓名排序通讯录
void SortPeople(Contact *ps)
{
	if (ps->sz > 1)
	{
		size_t i = 0;
		for (; i < ps->sz - 1; i++)
		{
			size_t j = 0;
			for (; j < ps->sz - 1 - i; j++)
			{
				if (strcmp(ps->data[j].name, ps->data[j + 1].name))
				{
					People temp = ps->data[j];
					ps->data[j] = ps->data[j + 1];
					ps->data[j + 1] = temp;
				}
			}
		}
	}
	printf("排序成功\n");

}

//清空通讯录
void EmptyPeople(Contact *ps)
{
	ps->sz = 0;
	printf("清空成功\n");
}
//初始化通讯录
void InitContact(Contact *ps)

{
	ps->sz = 0;
	ps->data = malloc(sizeof(People)*MAX);
	if (ps->data == NULL)
	{
		perror("use malloc");
	}
	memset(ps->data, 0, sizeof(People)*MAX);
	ps->capacity = MAX;
	LoadContcat(ps);
}


void CheckCapacity(Contact *ps)
{
	if (ps->sz == ps->capacity)
	{
		People *tmp = realloc(ps->data, (ps->capacity + 10)*sizeof(People));
		if (tmp == NULL)
		{
			perror("use malloc");
			exit(0);
		}
		else
		{
			ps->data = tmp;
			ps->capacity += 10;
			printf("增容成功\n");
		}
	}
}
//保存录入通讯录联系人的信息
void SaveContcat(Contact *ps)
{
	int i = 0;
	FILE* pfwrite = fopen("FILENAME", "w");
	if (pfwrite == NULL);
	{
		perror("use fopen to write");
		exit(0);
	}
	for (i = 0; i < ps->sz; i++)
	{
		fwrite(ps->data + i, sizeof(People), 1, pfwrite);
	}
	fclose(pfwrite);
	pfwrite = NULL;
}
//初始化的时候加载已经存在的联系人
void LoadContcat(Contact *ps)
{
	People tmp;
	FILE* pfread = fopen("FILENAME", "r");
	if (pfread == NULL)
	{
		perror("use fopen to read");
		exit(0);
	}
	while (fread(&tmp, sizeof(People), 1, pfread))
	{
		CheckCapacity(ps);
		ps->data[ps->sz] = tmp;
		ps->sz++;
	}
	fclose(pfread);
	pfread = NULL;
}

3.test.c


#include"contact.h"
void menu()
{
	printf("**********************************\n");
	printf("****  1.add          2.del    ****\n");
	printf("****  3.search       4.alert  ****\n");
	printf("****  5.show         6.sort   ****\n");
	printf("****  7.empty        0.exit   ****\n");
	printf("**********************************\n");
}

int main()
{
	int input = 0;

	Contact ps;
	InitContact(&ps);
	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			AddPeople(&ps);
			break;
		case DEL:
			DelePeople(&ps);
			break;
		case SEARCH:
			SearchPeople(&ps);
			break;
		case ALERT:
			AlertPeople(&ps);
			break;
		case SHOW:
			ShowPeople(&ps);
		case SORT:
			SortPeople(&ps);
		case EMPTY:
			EmptyPeople(&ps);
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}

 

你可能感兴趣的:(C语言项目)