多版本通讯录

Posted peiyao456

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多版本通讯录相关的知识,希望对你有一定的参考价值。

开始写通讯录已经好久了,才把通讯录的多个版本整理完,下边我来谈谈实现通讯录过程中的一些问题,以及自己的心

得,另外附加代码。

通讯录要求:

      1.增加信息   2.删除信息    3.查找信息  4.修改信息    

      5.显示信息   6.清除信息  7.按姓名排序  0.退出

我觉得,写代码之前一定要想清楚自己要实现的功能,应该怎么实现,大致应该清楚这些。

首先,静态通讯录,给出通讯录存储元素的总大小,通讯录里最多只能存储这么多信息。而通讯录的增删改查,通常

要找到通讯录的最后一个元素的位置,或者是元素的个数,所以我们需要再定义一个结构体,第一个成员是通讯录

的信息,是个结构体数组,第二个元素是当前通讯录的信息个数,这样,增加信息的时候,如果通讯录信息个数等于

通讯录的最大存储元素数,此时无法增加。所以,增加的结构体还是蛮有用的。关于防止头文件重复包含,在这种小

项目中也是需要注意的。

代码:

//Contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define NAME_MAX 12
#define SEX_MAX 4
#define TELE_MAX 12
#define ADDR_MAX 20
enum OP
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	DISPLAY,
	CLEAR,
	SORT
};
typedef struct person
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}person;
typedef struct ConInfo
{
	person data[1000];
	int size;
}ConInfo,*pConInfo;
void init_con(pConInfo pcon);
void add_person(pConInfo pcon);
void dis_con(pConInfo pcon);
void del_con(pConInfo pcon);
void search_person(pConInfo pcon);
void clear_con(pConInfo pcon);
void modify_person(pConInfo pcon);
void sort_con(pConInfo pcon);
#endif
//contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
//#ifndef __CONTACT_H__
//#define __CONTACT_H__
#include<string.h>
#include<stdio.h>
static int find(pConInfo pcon, char *name)
{
	int i = 0;
	for (i = 0;i < pcon->size;i++)
	{
		if (strcmp(pcon->data[i].name, name) == 0)
			return i;
	}
	return -1;
}

void init_con(pConInfo pcon)
{
	pcon->size = 0;
}
void add_person(pConInfo pcon)
{
	if (pcon->size == 1000)
	{
		printf("通讯录已经满了\n");
		return;
	}
	int num = 0;//插入人数
	printf("请输入插入的人数:>");
	scanf("%d",&num);
	while (num--)
	{
		printf("请输入联系人姓名:>");
		scanf("%s",pcon->data [pcon->size].name);
		printf("请输入联系人年龄:>");
		scanf("%d", &(pcon->data[pcon->size].age));
		printf("请输入联系人性别:>");
		scanf("%s", pcon->data[pcon->size].sex);
		printf("请输入联系人电话:>");
		scanf("%s", pcon->data[pcon->size].tele);
		printf("请输入联系人地址:>");
		scanf("%s", pcon->data[pcon->size].addr);
		pcon->size++;
		printf("插入成功\n");
	}
}
void dis_con(pConInfo pcon)
{
	int i = 0;
	printf("通讯录信息如下:\n");
	printf("%12s%4s%4s%12s%20s\n","name","age","sex","tele","addr");
	for (i = 0;i < pcon->size;i++)
	{
		printf("%12s%4d%4s%12s%20s",
			pcon->data[i].name,
			pcon->data[i].age,
			pcon->data[i].sex, pcon->data[i].tele,
			pcon->data[i].addr);
		printf("\n");
	}
}
void del_con(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;//查找的位置
	int index = 0;
	if (pcon->size == 0)
	{
		printf("通讯录已经是空的\n");
	}
	printf("请输入删除联系人的姓名:>");
	scanf("%s",name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	for (index = pos;index < pcon->size;index++)
	{
		memcpy(&(pcon->data [index]), &(pcon->data[index+1]),sizeof(person));
	}
	pcon->size--;
	printf("删除成功\n");
}
void search_person(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;
	printf("请输入要查找联系人的姓名:>");
	scanf("%s",name);
	pos = find(pcon,name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	printf("%12s%4d%4s%12s%20s",
		pcon->data[pos].name,
		pcon->data[pos].age,
		pcon->data[pos].sex, pcon->data[pos].tele,
		pcon->data[pos].addr);
	printf("\n");
}
void clear_con(pConInfo pcon)
{
	pcon->size = 0;
	printf("清空成功!\n");
}
void modify_person(pConInfo pcon)
{
	if (pcon->size == 0)
	{
		printf("通讯录为空");
	}
	char name[NAME_MAX];
	int pos = 0;
	printf("请输入要修改的联系人的姓名\n");
	scanf("%s",name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要修改的联系人不存在\n");
		return;
	}
	printf("请输入修改后的信息\n");
	scanf("%s%d%s%s%s",
		pcon->data[pos].name,
		&(pcon->data[pos].age),
		pcon->data[pos].sex,
		pcon->data[pos].tele,
		pcon->data[pos].addr);
	printf("修改成功\n");
}
void sort_con(pConInfo pcon)
{
	int i = 0;
	int j = 0;
	int flag = 0;
	person tmp ;
	for (i = 0;i < pcon->size - 1;i++)
	{
		flag = 1;
		for (j = 0;j < pcon->size - 1 - i;j++)
		{
			if (strcmp(pcon->data[j].name, pcon->data[j + 1].name)> 0)
			{
				
				tmp = pcon->data[j];
				pcon->data[j] = pcon->data[j + 1];
				pcon->data[j + 1] = tmp;
				flag = 0;
				
			}
			
			if (flag == 1)
				break;
		}
	}
printf("排序成功\n");
dis_con(pcon);
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Contact.h"

void menu()
{
	printf("*****1.add       2.del   ****\n");
	printf("*****3.search    4.modify****\n");
	printf("*****5.display   6.clear ****\n");
	printf("*****7.sort      0.exit  ****\n");
}
int main()
{
	ConInfo  pson = {0};
	init_con(&pson);
	int input = 1;
	while (input)
	{
		menu();
		printf("请选择:>");
		scanf("%d",&input);
		switch (input)
		{
		case ADD:
			add_person(&pson);
			break;
		case DEL:
			del_con(&pson);
			break;
		case SEARCH:
			search_person(&pson);
			break;
		case MODIFY:
			modify_person(&pson);
			break;
		case CLEAR:
			clear_con(&pson);
			break;
		case DISPLAY:
			dis_con(&pson);
			break;
		case SORT:
			sort_con(&pson);
			break;
		case EXIT:
			break;
		default:
			printf("输入错误");
			break;
		}
	}
	system("pause");
	return  0;
}

由于静态通讯录存在一定的缺陷,比如,开辟了很大一块空间,但是某人的朋友少,就会造成空间浪费。如果开辟的

太小,如果某人的朋友多,岂不是存不下吗??所以,动态通讯录专门用于解决这个问题。动态开辟,要多少开辟多

少,下边展示代码。

//contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
//#ifndef __CONTACT_H__
//#define __CONTACT_H__
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
static int find(pConInfo pcon, char *name)
{
	int i = 0;
	for (i = 0;i < pcon->size;i++)
	{
		if (strcmp(pcon->data[i].name, name) == 0)
			return i;
	}
	return -1;
}
void init_con(pConInfo pcon)
{
	pcon->size = 0;
	pcon->capacity = 2;
	pcon->data = (person *)malloc(2*sizeof(person));
	if (pcon->data == NULL)
	{
		printf("out of memory");
		return;
	}
}
void expand_capacity(pConInfo pcon)
{
	person *p = NULL;
	pcon ->data = (person *)realloc(pcon->data,(pcon->capacity+ INC_CAP)*sizeof(person));
	if (pcon->data == NULL)
	{
		printf("out of memory");
		exit(EXIT_FAILURE);
	}
	else
	{
		p = pcon->data;
	}
	pcon->capacity = pcon->capacity + INC_CAP;
}
void add_person(pConInfo pcon)
{
	if (pcon->size == pcon->capacity)
	{
		expand_capacity(pcon);
	}
	/*if (pcon->size == 0)
	{
		init_con(pcon);
	}*/
	
		printf("请输入联系人姓名:>");
		scanf("%s", pcon->data[pcon->size].name);
		printf("请输入联系人年龄:>");
		scanf("%d", &(pcon->data[pcon->size].age));
		printf("请输入联系人性别:>");
		scanf("%s", pcon->data[pcon->size].sex);
		printf("请输入联系人电话:>");
		scanf("%s", pcon->data[pcon->size].tele);
		printf("请输入联系人地址:>");
		scanf("%s", pcon->data[pcon->size].addr);
		pcon->size++;
		printf("插入成功\n");

}
void dis_con(pConInfo pcon)
{
	int i = 0;
	printf("通讯录信息如下:\n");
	printf("%12s%4s%4s%12s%20s\n", "name", "age", "sex", "tele", "addr");
	for (i = 0;i < pcon->size;i++)
	{
		printf("%12s%4d%4s%12s%20s",
			pcon->data[i].name,
			pcon->data[i].age,
			pcon->data[i].sex, pcon->data[i].tele,
			pcon->data[i].addr);
		printf("\n");
	}
}
void del_con(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;//查找的位置
	int index = 0;
	if (pcon->size == 0)
	{
		printf("通讯录已经是空的\n");
	}
	printf("请输入删除联系人的姓名:>");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
    else
	    {
	        for (index = pos;index < pcon->size;index++)
				{
					memcpy(&(pcon->data[index]), &(pcon->data[index + 1]), sizeof(person));
				}
			pcon->size--;
			printf("删除成功\n");
		}
}
void search_person(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;
	printf("请输入要查找联系人的姓名:>");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	else
	printf("%12s%4d%4s%12s%20s",
		pcon->data[pos].name,
		pcon->data[pos].age,
		pcon->data[pos].sex,
		pcon->data[pos].tele,
		pcon->data[pos].addr);
	printf("\n");
}
void clear_con(pConInfo pcon)
{
	pcon->size = 0;
	printf("清空成功!\n");
}
void modify_person(pConInfo pcon)
{
	if (pcon->size == 0)
	{
		printf("通讯录为空");
	}
	char name[NAME_MAX];
	int pos = 0;
	printf("请输入要修改的联系人的姓名\n");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要修改的联系人不存在\n");
		return;
	}
	else
	{
		printf("请输入修改后的信息\n");
		scanf("%s%d%s%s%s",
			pcon->data[pos].name,
			&(pcon->data[pos].age),
			pcon->data[pos].sex,
			pcon->data[pos].tele,
			pcon->data[pos].addr);
		printf("修改成功\n");
	}
}
void free_con(pConInfo pcon)
{
	free(pcon->data);
	pcon->data = NULL;
}
void sort_con(pConInfo pcon)
{
	int i = 0;
	int j = 0;
	int flag = 0;
	person tmp;
	for (i = 0;i < pcon->size - 1;i++)
	{
		flag = 1;
		for (j = 0;j < pcon->size - 1 - i;j++)
		{
			if (strcmp(pcon->data[j].name, pcon->data[j + 1].name)> 0)
			{
				
				tmp = pcon->data[j];
				pcon->data[j] = pcon->data[j + 1];
				pcon->data[j + 1] = tmp;
				flag = 0;

			}
			if (flag == 1)
				break;
		}
	}
	printf("排序成功\n");
	dis_con(pcon);
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"Contact.h"

void menu()
{
	printf("*****1.add       2.del   ****\n");
	printf("*****3.search    4.modify****\n");
	printf("*****5.display   6.clear ****\n");
	printf("*****7.sort      0.exit  ****\n");
}
int main()
{
	ConInfo  pson = { 0 };
	init_con(&pson);
	int input = 1;
	while (input)
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			add_person(&pson);
			break;
		case DEL:
			del_con(&pson);
			break;
		case SEARCH:
			search_person(&pson);
			break;
		case MODIFY:
			modify_person(&pson);
			break;
		case CLEAR:
			clear_con(&pson);
			break;
		case DISPLAY:
			dis_con(&pson);
			break;
		case SORT:
			sort_con(&pson);
			break;
		case EXIT:
			free_con(&pson);
			break;
		default:
			printf("输入错误");
			break;
		}
	}
	system("pause");
	return  0;
}
//contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define NAME_MAX 12
#define SEX_MAX 4
#define TELE_MAX 12
#define ADDR_MAX 20
#define INC_CAP  2
enum OP
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	DISPLAY,
	CLEAR,
	SORT
};
typedef struct person
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}person;
typedef struct ConInfo
{
	person *data;
	int size;
	int capacity;//当前电话本的容量
}ConInfo, *pConInfo;
void init_con(pConInfo pcon);
void add_person(pConInfo pcon);
void dis_con(pConInfo pcon);
void del_con(pConInfo pcon);
void search_person(pConInfo pcon);
void clear_con(pConInfo pcon);
void modify_person(pConInfo pcon);
void sort_con(pConInfo pcon);
void free_con(pConInfo pcon);
#endif

增加信息的时候发现容量不够,就扩容,哦,对了,结构体里必须再加一个成员,就是通讯录的容量。

现在还能清晰的记得,在之前做c++课程设计(我的课题是宾馆客房管理系统),老师给的要求就是,不要给我演示

的时候就是按顺序(就是先增加信息,再删除啊显示啊之类的),我当时就是没做好,现在,我来让我的通讯录实现

这个功能----把信息保存在文件中,每次运行时,只要把文件中的信息载入内存,就不用每次都是先录入信息。

下边展示代码:

//contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define NAME_MAX 12
#define SEX_MAX 4
#define TELE_MAX 12
#define ADDR_MAX 20
#define INC_CAP  2
enum OP
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	DISPLAY,
	CLEAR,
	SORT
};
typedef struct person
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
}person;
typedef struct ConInfo
{
	person *data;
	int size;
	int capacity;//当前电话本的容量
}ConInfo, *pConInfo;
void init_con(pConInfo pcon);
void add_person(pConInfo pcon);
void dis_con(pConInfo pcon);
void del_con(pConInfo pcon);
void search_person(pConInfo pcon);
void clear_con(pConInfo pcon);
void modify_person(pConInfo pcon);
void sort_con(pConInfo pcon);
void free_con(pConInfo pcon);
void save_con(pConInfo pcon);
void load_con(pConInfo pcon);
#endif
//contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
//#ifndef __CONTACT_H__
//#define __CONTACT_H__
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
static int find(pConInfo pcon, char *name)
{
	int i = 0;
	for (i = 0;i < pcon->size;i++)
	{
		if (strcmp(pcon->data[i].name, name) == 0)
			return i;
	}
	return -1;
}
void init_con(pConInfo pcon)
{
	pcon->size = 0;
	pcon->capacity = 2;
	pcon->data = (person *)malloc(2 * sizeof(person));
	if (pcon->data == NULL)
	{
		printf("out of memory");
		return;
	}
	load_con(pcon);
}
void check_capacity(pConInfo pcon)
{
	person *p = NULL;
	pcon->data = (person *)realloc(pcon->data, (pcon->capacity + INC_CAP)*sizeof(person));
	if (pcon->data == NULL)
	{
		printf("out of memory");
		exit(EXIT_FAILURE);
	}
	else
	{
		p = pcon->data;
	}
	pcon->capacity = pcon->capacity + INC_CAP;
}
void add_person(pConInfo pcon)
{
	/*if (pcon->size == pcon->capacity)
	{
		expand_capacity(pcon);
	}*/
	/*if (pcon->size == 0)
	{
	init_con(pcon);
	}*/
	check_capacity(pcon);
	printf("请输入联系人姓名:>");
	scanf("%s", pcon->data[pcon->size].name);
	printf("请输入联系人年龄:>");
	scanf("%d", &(pcon->data[pcon->size].age));
	printf("请输入联系人性别:>");
	scanf("%s", pcon->data[pcon->size].sex);
	printf("请输入联系人电话:>");
	scanf("%s", pcon->data[pcon->size].tele);
	printf("请输入联系人地址:>");
	scanf("%s", pcon->data[pcon->size].addr);
	pcon->size++;
	printf("插入成功\n");

}
void dis_con(pConInfo pcon)
{
	int i = 0;
	printf("通讯录信息如下:\n");
	printf("%12s%4s%4s%12s%20s\n", "name", "age", "sex", "tele", "addr");
	for (i = 0;i < pcon->size;i++)
	{
		printf("%12s%4d%4s%12s%20s",
			pcon->data[i].name,
			pcon->data[i].age,
			pcon->data[i].sex, pcon->data[i].tele,
			pcon->data[i].addr);
		printf("\n");
	}
}
void del_con(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;//查找的位置
	int index = 0;
	if (pcon->size == 0)
	{
		printf("通讯录已经是空的\n");
	}
	printf("请输入删除联系人的姓名:>");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	else
	{
		for (index = pos;index < pcon->size;index++)
		{
			memcpy(&(pcon->data[index]), &(pcon->data[index + 1]), sizeof(person));
		}
		pcon->size--;
		printf("删除成功\n");
	}
}
void search_person(pConInfo pcon)
{
	char name[NAME_MAX];
	int pos = 0;//存储查找到的元素的位置
	printf("请输入要查找联系人的姓名:>");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要查找的联系人不存在\n");
	}
	else
		printf("%12s%4d%4s%12s%20s",
			pcon->data[pos].name,
			pcon->data[pos].age,
			pcon->data[pos].sex,
			pcon->data[pos].tele,
			pcon->data[pos].addr);
	printf("\n");
}
void clear_con(pConInfo pcon)
{
	pcon->size = 0;
	printf("清空成功!\n");
}
void modify_person(pConInfo pcon)
{
	if (pcon->size == 0)
	{
		printf("通讯录为空");
	}
	char name[NAME_MAX];
	int pos = 0;
	printf("请输入要修改的联系人的姓名\n");
	scanf("%s", name);
	pos = find(pcon, name);
	if (pos == -1)
	{
		printf("要修改的联系人不存在\n");
		return;
	}
	else
	{
		printf("请输入修改后的信息\n");
		scanf("%s%d%s%s%s",
			pcon->data[pos].name,
			&(pcon->data[pos].age),
			pcon->data[pos].sex,
			pcon->data[pos].tele,
			pcon->data[pos].addr);
		printf("修改成功\n");
	}
}
void free_con(pConInfo pcon)
{
	free(pcon->data);
	pcon->data = NULL;
}
void sort_con(pConInfo pcon)
{
	int i = 0;
	int j = 0;
	int flag = 0;
	person tmp;
	for (i = 0;i < pcon->size - 1;i++)
	{
		flag = 1;
		for (j = 0;j < pcon->size - 1 - i;j++)
		{
			if (strcmp(pcon->data[j].name, pcon->data[j + 1].name)> 0)
			{

				tmp = pcon->data[j];
				pcon->data[j] = pcon->data[j + 1];
				pcon->data[j + 1] = tmp;
				flag = 0;

			}
			if (flag == 1)
				break;
		}
	}
	printf("排序成功\n");
	dis_con(pcon);
}
void save_con(pConInfo pcon)
{
	FILE *pwrite = fopen("contact.dat","w");
	int i = 0;
	if (pwrite == NULL)
	{
		perror("open the file for write");
		exit(EXIT_FAILURE);
	}
	else
	{
		for (i = 0;i < pcon->size;i++)
		{
		    fwrite(&(pcon->data[i]),sizeof(person),1,pwrite);
		}
	}
	fclose(pwrite);
}
void load_con(pConInfo pcon)
{
	FILE *pread = fopen("contact.dat","r");
	int i = 0;
	person tmp = { 0 };
	if (pread == NULL)
	{
		perror("open the file for read");
		exit(EXIT_FAILURE);
	}
	else
	{
		while(fread(&tmp, sizeof(person), 1, pread))
		{
			check_capacity(pcon);
			pcon->data[i] = tmp;
			pcon->size++;
			i++;
		}
	}
	fclose(pread);
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"contact.h"

void menu()
{
	printf("*****1.add       2.del   ****\n");
	printf("*****3.search    4.modify****\n");
	printf("*****5.display   6.clear ****\n");
	printf("*****7.sort      0.exit  ****\n");
}
int main()
{
	ConInfo  pson = { 0 };
	
	init_con(&pson);
	int input = 1;
	while (input)
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			add_person(&pson);
			break;
		case DEL:
			del_con(&pson);
			break;
		case SEARCH:
			search_person(&pson);
			break;
		case MODIFY:
			modify_person(&pson);
			break;
		case CLEAR:
			clear_con(&pson);
			break;
		case DISPLAY:
			dis_con(&pson);
			break;
		case SORT:
			sort_con(&pson);
			break;
		case EXIT:
			save_con(&pson);
			free_con(&pson);
			break;
		default:
			printf("输入错误");
			break;
		}
	}
	system("pause");
	return  0;
}

在做文件版本时,我就碰到信息导入不到内存的原因。是因为导入信息时判定条件不对。所以,不管做任何事,大事

也好,小事也罢,踏踏实实做好。

这次也才懂得,并不是所有的函数实现都必须在头文件中声明,只有是函数接口才在头文件中声明。

其实,我们也可以把信息保存在数据库文件,这个功能,之后再实现~~


一位学长在自己的博客中提到,学习,会遇见不同层次的人;分享,确实能获得更多~~对我希望自己从分享中学到更

多~~

以上是关于多版本通讯录的主要内容,如果未能解决你的问题,请参考以下文章

论如何设计一款端对端加密通讯软件

JeecgUniapp移动框架 2.0版本发布,一份代码多终端适配

JeecgUniapp移动框架 2.0版本发布,一份代码多终端适配

异常和TCP通讯

Android - 片段中的联系人选择器

html 优雅的博客时事通讯片段