【C语言】C语言实现静态通讯录

   实现一个通讯录; 
通讯录可以用来存储1000个人的信息,每个人的信息包括: 
姓名、性别、年龄、电话、住址 

提供方法: 
1. 添加联系人信息 
2. 删除指定联系人信息 
3. 查找指定联系人信息 
4. 修改指定联系人信息 
5. 显示所有联系人信息 
6. 清空所有联系人 

7. 以名字排序所有联系人 

以上是实现通讯录的基本要求,代码如下,基本的注释已经在代码后面给出:


Contact.h

#ifndef __CONTACT_H__ 
#define __CONTACT_H__ 

 #include 
#include
#include
#include
#include

#define MAX 1000 
#define NAME_MAX 20 
#define SEX_MAX 5 
#define ADDR_MAX 30 
#define TELE_MAX 12 

typedef struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char addr[ADDR_MAX];
	char tele[TELE_MAX];
}PeoInfo;


typedef struct Contact
{
	PeoInfo data[MAX];
	int sz;
}Contact, *pContact;

void InitContact(pContact pcon);
void AddContact(pContact pcon);
void ShowContact(const pContact pcon);
void DelContact(pContact pcon);
void SearchContact(pContact pcon);
void ModifyContact(pContact pcon);//修改
void SortContact(pContact pcon);
void ClearContact(pContact pcon);
void ExitContact(pContact pcon);
#endif


Contact.c

#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
void InitContact(pContact pcon)
{
	pcon->sz = 0;
	memset(pcon->data, 0, sizeof(pcon->data));
}
//添加
void AddContact(pContact pcon)
{
	assert(pcon);
	if (pcon->sz == MAX)
	{
		printf("通讯录满了!!!\n");
			return ;
	}
	printf("请输入名字:");
	scanf("%s", pcon->data[pcon->sz].name);
	printf("请输入年龄:");
	scanf("%d", &(pcon->data[pcon->sz].age));//年龄 取地址
	printf("请输入性别:");
	scanf("%s", (pcon->data[pcon->sz].sex));
	printf("请输入地址:");
	scanf("%s", (pcon->data[pcon->sz].addr));
	printf("请输入电话:");
	scanf("%s", (pcon->data[pcon->sz].tele));
	pcon->sz++;
	printf("添加成功 !\n");
}

//显示
void ShowContact(const pContact pcon)
{
	int i = 0;
	printf("%15s\t%5s\t%5s\t%15s\t%15s\n", "姓名", "年龄","性别", "地址", "电话");

	for (i = 0; i < pcon->sz; i++)
	{
		printf("%15s\t%5d\t%5s\t%15s\t%15s\n", 
			pcon->data[i].name,
			pcon->data[i].age,
			pcon->data[i].sex,
			pcon->data[i].addr,
			pcon->data[i].tele);
	}
}
static int FindEntry(pContact pcon, char name[])
{
	int i = 0;
	for (i = 0; i < pcon->sz; i++)
	{
		if (strcmp(pcon->data[i].name, name) == 0)
			return i;
	}
	return -1;
}

//删除
void DelContact(pContact pcon)
{
	int pos = 0;
	char name[NAME_MAX] = { 0 };
	printf("请输入要删除人的名字:\n");
	scanf("%s", name);
	pos = FindEntry(pcon, name);
	if (pos == -1)
	{
		printf("要删除的人不存在 !\n");
		return;
	}
	else
	{
		//删除
		int j = 0;
		for (j = pos; j < pcon->sz; j++)
		{
			pcon->data[j] = pcon->data[j + 1];
		}
		pcon->sz--;
			printf("删除成功 !\n");
	}
}


//查找
void SearchContact(pContact pcon)
{
	int pos = 0;
	char name[NAME_MAX] = { 0 };
	printf("请输入要查找人的名字:\n");
	scanf("%s", name);
	pos = FindEntry(pcon, name);
	if (pos == -1)
	{
		printf("要查找的人不存在 !\n");
		return;
	}
	else
	{
			printf("%15s\t%5d\t%5s\t%15s\t%15s\n",
				pcon->data[pos].name,
				pcon->data[pos].age,
				pcon->data[pos].sex,
				pcon->data[pos].addr,
				pcon->data[pos].tele);
		}
	}


//修改
void ModifyContact(pContact pcon)
{
	int i = 0;
	char flag = 'Y';
	int mod = -1;
	char na[20] = { 0 };
	do
	{
		printf("请输入需要修改的人:");
		scanf("%s", na);
		for (i = 0; i < pcon->sz; i++)
		{
			if (strcmp(pcon->data[i].name, na) == 0)
			{
				mod = i;
			}
		}
		if (mod >= 0)
		{
			printf("请输入修改后的名字:");
			scanf("%s", &(pcon->data[mod].name));
			printf("请输入修改后的年龄:");
			scanf("%d", &(pcon->data[mod].age));
			printf("请输入修改后的性别:");
			scanf("%s", &(pcon->data[mod].sex));
			printf("请输入修改后的地址:");
			scanf("%s", &(pcon->data[mod].addr));
			printf("请输入修改后的电话号码:");
			scanf("%s", &(pcon->data[mod].tele));
		printf("是否继续录入?(继续输入请输入Y,任意键退出):\n");
		getchar();
		scanf("%c", &flag);
		if (toupper(flag) != 'Y')
			break;
	}
	else
	{
		printf("没有你要修改的人!");
		printf("是否继续修改联系人?(继续则输入Y,按任意键退出):");
		getchar();
		scanf("%c", &flag);
		if (toupper(flag) != 'Y')
			break;
	}
} while (toupper(flag) == 'Y');
}


//排序
void  SortContact(pContact pcon)
		{
			int i = 0, j = 0;
			for (i = 0; i < pcon->sz - 1; i++)
			{
				for (j = 0; j < pcon->sz - 1 - i; j++)
				{
					if (strcmp(pcon->data[j].name, pcon->data[j + 1].name) > 0)
					{
						PeoInfo  peo = pcon->data[j];
						pcon->data[j] = pcon->data[j + 1];
						pcon->data[j + 1] = peo;
					}
				}
			}
			printf("%15s\t%5s\t%5s\t%15s\t%15s\n", "姓名", "年龄", "性别", "地址", "电话");
			for (i = 0; i < pcon->sz; i++)
			{
				printf("%4s\t%2d\t%2s\t%12s\t%12s\n",
					pcon->data[i].name,
					pcon->data[i].age,
					pcon->data[i].sex,
					pcon->data[i].addr,
					pcon->data[i].tele);
			}
		}


		//清空
		void ClearContact(pContact pcon)
		{
			pcon->sz = 0;
		}

test.c

#include"contact.h"
//#include
enum  {
	EXIT,
    ADD,
    DEL,
    SEARCH,
    MODIFY,
    SHOW,
    SORT,
    CLEAR,
	DESTROY
 };

	void meau()
	{
		printf("        *********************************************\n");
		printf("        *********************************************\n");
		printf("        ###############My address book###############\n");
		printf("        * 1-add          2-del         3-search     *\n");
		printf("        * 4-modify       5-show        6-Sort       *\n");
		printf("        * 0-Exit                       7-clear      *\n");
		printf("        *********************************************\n");
		printf("        *********************************************\n");
	}

	void test()
	{
		int input = 0;
		Contact my_con;
		InitContact(&my_con);
		do
		{
			meau();
				printf("请选择:");
			scanf_s("%d", &input);
			switch (input)
			{
			case ADD://添加
				AddContact(&my_con);
				break;
			case SHOW://显示
				ShowContact(&my_con);
				break;
			case DEL://删除
				DelContact(&my_con);
				break;
			case SEARCH://查找
				SearchContact(&my_con);
				break;
			case MODIFY://修改
				ModifyContact(&my_con);
					break;
			case SORT://排序
			SortContact(&my_con);
				break;
			case CLEAR://清除
				ClearContact(&my_con);
				break;
                 //case DESTROY:
				//DestroyContact(&my_con);
				//break;
            case EXIT://退出
				exit(0);
				break;
			}
		}while (input);
	}
	int main()
	{
		test();
		return 0;
	}

     

     以上就是静态通讯录的实现,在该通讯录中,结构体和指针占有很重要的一部分。由于个人的能力有限,有不足的地方欢迎大家批评指正,新手上路,大神勿喷!

你可能感兴趣的:(C语言,c语言,函数,编程,原创,通讯录)