C语言实现通讯录的方法(包括静态版本和动态版本

编辑: admin 分类: c#语言 发布时间: 2021-12-12 来源:互联网
目录
  • 1.静态通讯录的实现
    • 实现的方法:
  • 2.动态通讯录的实现
    • 实现的方法:
  • 3.总结

    1.静态通讯录的实现

    实现的方法:

    我们采用的方法就是工程形势,实现将功能和定义以及测试分成三个文件,其中定义放在.h文件,实现和测试放在.c文件当中。

    (1)contact.h文件的基本实现:

    #pragma once//防止头文件重复定义
    
    #define NAME_MAX 20
    #define SEX_MAX 5
    #define TELE_MAX 12
    #define ADDR_MAX 30
    #define MAX 1000  //静态通讯录,最大为1000
    
    #include<stdio.h>
    #include<string.h>
    
    struct PeoInfo
    {
    	char name[NAME_MAX];
    	int age;
    	char sex[SEX_MAX];
    	char tele[TELE_MAX];
    	char addr[ADDR_MAX];
    };
    
    //静态的版本
    struct Contact
    {
    	struct PeoInfo data[MAX];//容量为1000的通讯录
    	int sz; //已经使用了多少个通讯录
    };
    
    //初始化通讯录
    void InitContact(struct Contact* pc);
    
    //通讯录的增加
    void PushContact(struct Contact* pc);
    
    
    //通讯录的删除:
    void PopContact(struct Contact* pc);
    
    //通讯录的打印
    void ShowContact(const struct Contact* pc);
    
    //通过名字来查找,找到返回下标,找不到返回-1。
    int FindContactByName(const struct Contact* pc, const char* name);
    
    //通讯的查找并打印
    void SearchContact(const struct Contact* pc);
    
    //修改联系人的信息
    void ModContact(struct Contact* pc);
    
    //以名字排序
    void SortByNameContact(struct Contact* pc);
    
    //清空所有联系人
    void DestroyContact(struct Contact* pc);

    (2)contact.c文件的基本实现

    #include"contact.h"
    
    //通讯录的初始化
    void InitContact(struct Contact* pc)
    {
    	pc->sz = 0;
    	memset(pc->data, 0, sizeof(struct PeoInfo) * MAX);
    }
    
    //通讯录增加
    void PushContact(struct Contact* pc)
    {
    	if (pc->sz >= MAX)
    	{
    		printf("This contact is full!\n");
    	}
    	printf("please input name:>");
    	scanf("%s", &pc->data[pc->sz].name);
    	printf("please input age:>");
    	scanf("%d", &pc->data[pc->sz].age);
    	printf("please input sex:>");
    	scanf("%s", pc->data[pc->sz].sex);
    	printf("please input tele:>");
    	scanf("%s", pc->data[pc->sz].tele);
    	printf("please input addr:>");
    	scanf("%s", pc->data[pc->sz].addr);
    	printf("Add successful\n");
    	pc->sz++;
    }
    
    //通过名字来查找,找到返回下标,找不到返回-1。
    int FindContactByName(const struct Contact* pc, const char* ThisName)
    {
    	int i = 0;
    	for (i = 0; i < pc->sz; i++)
    	{
    		if (strcmp(ThisName, pc->data[i].name) == 0)
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    
    //通讯录的打印
    void ShowContact(const struct Contact* pc)
    {
    	printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age","sex","tele","addr");
    	int i = 0;
    	for (i = 0; i < pc->sz; i++)
    	{
    		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
    	}
    }
    
    //通讯录的删除:
    void PopContact(struct Contact* pc)
    {
    	if (pc->sz == 0)
    	{
    		printf("Address book is empty. Can Not be deleted\n");
    		return;
    	}
    	char name[NAME_MAX] = { 0 };
    	printf("Please enter the name of the person you want to delete:>");
    	scanf("%s", name);
    	//1.查找
    	int pos = FindContactByName(pc, name);
    	if ( pos == -1)
    	{
    		printf("There's no one by that name!\n");
    	}
    	else
    	{
    		//2.删除
    		int j = 0;
    		for (j = pos; j < pc->sz - 1; j++)
    		{
    			pc->data[j] = pc->data[j + 1];
    		}
    		printf("Delection successful\n");
    	}
    	pc->sz--;
    	
    }
    
    //通讯的查找
    void SearchContact(const struct Contact* pc)
    {
    	char name[NAME_MAX] = { 0 };
    	printf("please input name of you want to search:>");
    	scanf("%s", name);
    	int pos = FindContactByName(pc, name);
    	if (pos == -1)
    	{
    		printf("No this one!\n");
    	}
    	else
    	{
    		printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
    		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", 
    		pc->data[pos].name, 
    		pc->data[pos].age, 
    		pc->data[pos].sex, 
    		pc->data[pos].tele, 
    		pc->data[pos].addr);
    	}
    }
    
    //修改联系人的信息
    void ModContact(struct Contact* pc)
    {
    	char name[NAME_MAX] = { 0 };
    	printf(" Please enter the name of the person you want to modify : > ");
    	scanf("%s", name);
    	int pos = FindContactByName(pc, name);
    	if (pos == -1)
    	{
    		printf("No this one\n");
    	}
    	else
    	{
    		printf("please input new name:>");
    		scanf("%s", &pc->data[pos].name);
    		printf("please input new age:>");
    		scanf("%d", &pc->data[pos].age);
    		printf("please input new sex:>");
    		scanf("%s", pc->data[pos].sex);
    		printf("please input new tele:>");
    		scanf("%s", pc->data[pos].tele);
    		printf("please input new addr:>");
    		scanf("%s", pc->data[pos].addr);
    		printf("Modify successful\n");
    	}
    }
    //以名字排序
    void SortByNameContact(struct Contact* pc)
    {
    	if (pc->sz == 0 || pc->sz == 1)
    	{
    		printf("Too few contacts to sort\n");
    	}
    	int i, j;
    	for (i = 0; i < pc->sz ; i++)
    	{
    		int flag = 0;
    		for (j = 0; j < pc->sz - 1 - i; j++)
    		{
    			if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
    			{
    				struct PeoInfo tmp = pc->data[j];
    				pc->data[j] = pc->data[j + 1];
    				pc->data[j + 1] = tmp;
    				flag = 1;
    			}
    		}
    		if (flag == 0)
    		{
    			break;
    		}
    	}
    	printf("Sort successful\n");
    }
    
    
    //清空所有联系人
    void DestroyContact(struct Contact* pc)
    {
    	pc->sz = 0;
    	printf("Destroy successful\n");
    }

    (3)test.c文件的实现

    #define _CRT_SECURE_NO_WARNINGS 1
    #include"contact.h"
    void menu()
    {
    	printf("************************************\n");
    	printf("******   1.add      2.del    *******\n");
    	printf("******   3.search   4.modify *******\n");
    	printf("******   5.show     6.sort   *******\n");
    	printf("******   7.destroy  0.exit   *******\n");
    	printf("************************************\n");
    }
    
    enum Option//使用枚举,增加代码的可读性
    {
    	EXIT,
    	ADD,
    	DEL,
    	SEARCH,
    	MODIFY,
    	SHOW,
    	SORT,
    	DESTROY,
    };
    
    int main()
    {
    	int input;
    	struct Contact con;
    	//初始化通讯录
    	InitContact(&con);
    	do
    	{
    		menu();
    		printf("please select:>\n");
    		scanf("%d", &input);
    		switch (input)
    		{
    		case EXIT:
    			printf("exit succseeful!\n");
    			break;
    		case ADD:
    			PushContact(&con);
    			break;
    		case DEL:
    			PopContact(&con);
    			break;
    		case SEARCH:
    			SearchContact(&con);
    			break;
    		case MODIFY:
    			ModContact(&con);
    			break;
    		case SHOW:
    			ShowContact(&con);
    			break;
    		case SORT:
    			SortByNameContact(&con);
    			break;
    		case DESTROY:
    			DestroyContact(&con);
    			break;
    		default:
    			printf("Select error, please select again!\n");
    		}
    	} while (input);
    	return 0;
    }

    2.动态通讯录的实现

    实现的方法:

    和静态实现细节都差不多,只是静态使用的数组,所以固定了通讯录的大小,不能超出通讯录大小的限制,且没有使用完还会造成空间的浪费。所以使用动态内存分配来实现动态的通讯录。会节省空间并且是通讯录的大小变得灵活。

    (1)contact.h文件的基本实现:

    #pragma once//防止头文件重复定义
    
    #define NAME_MAX 20
    #define SEX_MAX 5
    #define TELE_MAX 12
    #define ADDR_MAX 30
    #define DEFINE_SZ  3
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    struct 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 PushContact(struct Contact* pc);
    
    //通讯录的删除:
    void PopContact(struct Contact* pc);
    
    //通讯录的打印
    void ShowContact(const struct Contact* pc);
    
    //通过名字来查找,找到返回下标,找不到返回-1。
    int FindContactByName(const struct Contact* pc, const char* name);
    
    //通讯的查找并打印
    void SearchContact(const struct Contact* pc);
    
    //修改联系人的信息
    void ModContact(struct Contact* pc);
    
    //以名字排序
    void SortByNameContact(struct Contact* pc);
    
    //清空所有联系人
    void DestroyContact(struct Contact* pc);

    (2)contact.c文件的基本实现

    #include"contact.h"
    
    //通讯录的初始化
    
    
    void InitContact(struct Contact* pc)
    {
    	pc->sz = 0;
    	pc->data = (struct PeoInfo*)malloc(sizeof(struct PeoInfo) * DEFINE_SZ);
    	if (pc->data == NULL)
    	{
    		printf("malloc fail\n");
    		return;
    	}
    	pc->capacity = DEFINE_SZ;
    }
    
    //通讯录增加
    void PushContact(struct Contact* pc)
    {
    	if (pc->sz == pc->capacity)
    	{
    		//增容:
    		struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, sizeof(struct PeoInfo) * (pc->capacity + 2));
    		if (ptr == NULL)
    		{
    			printf("realloc fail\n");
    			return;
    		}
    		else
    		{
    			pc->data = ptr;
    			pc->capacity += 2;
    			printf("Compatibilization successful\n");
    		}
    		
    	}
    	//录入新增成员信息:
    	printf("please input name:>");
    	scanf("%s", &pc->data[pc->sz].name);
    	printf("please input age:>");
    	scanf("%d", &pc->data[pc->sz].age);
    	printf("please input sex:>");
    	scanf("%s", pc->data[pc->sz].sex);
    	printf("please input tele:>");
    	scanf("%s", pc->data[pc->sz].tele);
    	printf("please input addr:>");
    	scanf("%s", pc->data[pc->sz].addr);
    	printf("Add successful\n");
    	pc->sz++;	
    }
    
    //通过名字来查找,找到返回下标,找不到返回-1。
    int FindContactByName(const struct Contact* pc, const char* ThisName)
    {
    	int i = 0;
    	for (i = 0; i < pc->sz; i++)
    	{
    		if (strcmp(ThisName, pc->data[i].name) == 0)
    		{
    			return i;
    		}
    	}
    	return -1;
    }
    
    //通讯录的打印
    void ShowContact(const struct Contact* pc)
    {
    	printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
    	int i = 0;
    	for (i = 0; i < pc->sz; i++)
    	{
    		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
    	}
    }
    
    //通讯录的删除:
    void PopContact(struct Contact* pc)
    {
    	if (pc->sz == 0)
    	{
    		printf("Address book is empty. Can Not be deleted\n");
    		return;
    	}
    	char name[NAME_MAX] = { 0 };
    	printf("Please enter the name of the person you want to delete:>");
    	scanf("%s", name);
    	//1.查找
    	int pos = FindContactByName(pc, name);
    	if (pos == -1)
    	{
    		printf("There's no one by that name!\n");
    	}
    	else
    	{
    		//2.删除
    		int j = 0;
    		for (j = pos; j < pc->sz - 1; j++)
    		{
    			pc->data[j] = pc->data[j + 1];
    		}
    		printf("Delection successful\n");
    	}
    	pc->sz--;
    
    }
    
    //通讯的查找
    void SearchContact(const struct Contact* pc)
    {
    	char name[NAME_MAX] = { 0 };
    	printf("please input name of you want to search:>");
    	scanf("%s", name);
    	int pos = FindContactByName(pc, name);
    	if (pos == -1)
    	{
    		printf("No this one!\n");
    	}
    	else
    	{
    		printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
    		printf("%15s\t%5d\t%8s\t%15s\t%20s\n",
    			pc->data[pos].name,
    			pc->data[pos].age,
    			pc->data[pos].sex,
    			pc->data[pos].tele,
    			pc->data[pos].addr);
    	}
    }
    
    //修改联系人的信息
    void ModContact(struct Contact* pc)
    {
    	char name[NAME_MAX] = { 0 };
    	printf(" Please enter the name of the person you want to modify : > ");
    	scanf("%s", name);
    	int pos = FindContactByName(pc, name);
    	if (pos == -1)
    	{
    		printf("No this one\n");
    	}
    	else
    	{
    		printf("please input new name:>");
    		scanf("%s", &pc->data[pos].name);
    		printf("please input new age:>");
    		scanf("%d", &pc->data[pos].age);
    		printf("please input new sex:>");
    		scanf("%s", pc->data[pos].sex);
    		printf("please input new tele:>");
    		scanf("%s", pc->data[pos].tele);
    		printf("please input new addr:>");
    		scanf("%s", pc->data[pos].addr);
    		printf("Modify successful\n");
    	}
    }
    
    //以名字排序
    void SortByNameContact(struct Contact* pc)
    {
    	if (pc->sz == 0 || pc->sz == 1)
    	{
    		printf("Too few contacts to sort\n");
    	}
    	int i, j;
    	for (i = 0; i < pc->sz; i++)
    	{
    		int flag = 0;
    		for (j = 0; j < pc->sz - 1 - i; j++)
    		{
    			if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
    			{
    				struct PeoInfo tmp = pc->data[j];
    				pc->data[j] = pc->data[j + 1];
    				pc->data[j + 1] = tmp;
    				flag = 1;
    			}
    		}
    		if (flag == 0)
    		{
    			break;
    		}
    	}
    	printf("Sort successful\n");
    }
    
    //清空所有联系人
    void DestroyContact(struct Contact* pc)
    {
    	pc->sz = 0;
    	pc->capacity = 0;
    	free(pc->data);
    	pc->data = NULL;
    	printf("Destroy successful\n");
    }

    (3)test.c文件的实现

    #include"contact.h"
    
    void menu()
    {
    	printf("************************************\n");
    	printf("******   1.add      2.del    *******\n");
    	printf("******   3.search   4.modify *******\n");
    	printf("******   5.show     6.sort   *******\n");
    	printf("******   7.destroy  0.exit   *******\n");
    	printf("************************************\n");
    }
    
    enum Option
    {
    	EXIT,
    	ADD,
    	DEL,
    	SEARCH,
    	MODIFY,
    	SHOW,
    	SORT,
    	DESTROY,
    };
    
    int main()
    {
    	int input;
    	struct Contact con;
    	//初始化通讯录
    	InitContact(&con);
    	do
    	{
    		menu();
    		printf("please select:>\n");
    		scanf("%d", &input);
    		switch (input)
    		{
    		case EXIT:
    			DestroyContact(&con);
    			printf("exit succseeful!\n");
    			break;
    		case ADD:
    			PushContact(&con);
    			break;
    		case DEL:
    			PopContact(&con);
    			break;
    		case SEARCH:
    			SearchContact(&con);
    			break;
    		case MODIFY:
    			ModContact(&con);
    			break;
    		case SHOW:
    			ShowContact(&con);
    			break;
    		case SORT:
    			SortByNameContact(&con);
    			break;
    		case DESTROY:
    			DestroyContact(&con);
    			break;
    		default:
    			printf("Select error, please select again!\n");
    		}
    	} while (input);
    	return 0;
    }

    3.总结

    其实通讯录的实现就是数据结构的一种体现,我们需要学的东西还有很多,请大家一起跟我努力吧!!
    再就是有问题请大家及时指正!!!谢谢大家。

    到此这篇关于C语言实现通讯录的方法(包括静态版本和动态版本)的文章就介绍到这了,更多相关C语言实现通讯录内容请搜索海外IDC网以前的文章或继续浏览下面的相关文章希望大家以后多多支持海外IDC网!

    【原URL http://www.yidunidc.com/tw.html复制请保留原URL】