用c语言多文件编写1000人的通讯录

实现一个通讯录:

通讯录可以用来存储1000个人的信息,每个人的信息包括:

姓名,性别,年龄,电话,住址

提供方法:

1:                 添加联系人信息

2:                 删除指定联系人信息

3:                 查找指定联系人信息

4:                 修改指定联系人信息

5:                 显示所有联系人信息

6:                 清空所有联系人


#ifndef __CONTACT_H_
#define __CONTACT_H_
#define NAME_MAX 20
#define SEX_MAX 10
#define TEL_MAX 11
#define ADDR_MAX 20
#define PEO_MAX 1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct People
{
	char name[NAME_MAX];
	char sex[SEX_MAX];
	int age;
	char tel[TEL_MAX];
	char addr[ADDR_MAX];
}*peo;
typedef struct Contact
{
	int count;
	struct People people[PEO_MAX];
}*pCon;
void add_peo(pCon pcon);
void del_peo(pCon pcon);
void find_peo(pCon pcon);
void modify_peo(pCon pcon);
void show_peo(pCon pcon);
void clear_peo(pCon pcon);
#endif
#include "contact.h"
int search(pCon pcon,char *name)
{
	int i = 0;
	for(; i < pcon->count; i++)
	{
		if(strcmp(name,pcon->people[i].name) == 0)
			return i;
	}
	return -1;
}
void add_peo(pCon pcon)							//添加联系人
{
	if(pcon->count == PEO_MAX)
	{
		printf("The contact has fullen.");
		return ;
	}
	printf("please input name:  ");
	scanf("%s",(pcon->people[pcon->count]).name);
	printf("please input sex :  ");
	scanf("%s",(pcon->people[pcon->count]).sex);
	printf("please input age:  ");
	scanf("%d",&((pcon->people[pcon->count]).age));
	printf("please input tel:  ");
	scanf("%s",(pcon->people[pcon->count]).tel);
	printf("please input address:  ");
	scanf("%s",(pcon->people[pcon->count]).addr);
	pcon->count++;
}
void del_peo(pCon pcon)							//删除联系人
{
	int i;
	char name[NAME_MAX];
	printf("please input the people you want to delete:");
	scanf("%s",name);
	i = search(pcon,name);
	if( i == -1)
		printf("The people doesn't exsit.\n");
	else if(i == 1000)
		pcon->count--;
	else
	{
		for(; i < pcon->count; i++)
			pcon->people[i] = pcon->people[i+1];
		pcon->count--;
	}
}
void find_peo(pCon pcon)						//查找联系人
{
	int i;
	char name[NAME_MAX];
	printf("please input the people you want to find:");
	scanf("%s",name);
	i = search(pcon,name);
	if( i == -1)
		printf("The people doesn't exsit.\n");
	else
		printf("%s\t%s\t%d\t%s\t%s\n",
			pcon->people[i].name,
			pcon->people[i].sex,
			pcon->people[i].age,
			pcon->people[i].tel,
			pcon->people[i].addr);
}
void modify_peo(pCon pcon)						//修改联系人
{
	int i;
	char name[NAME_MAX];
	printf("please input the people you want to modify:");
	scanf("%s",name);
	i = search(pcon,name);
	if( i == -1)
		printf("The people doesn't exsit.\n");
	else
		printf("please input name:  ");
	scanf("%s",(pcon->people[i]).name);
	printf("please input sex :  ");
	scanf("%s",(pcon->people[i]).sex);
	printf("please input age:  ");
	scanf("%d",&((pcon->people[i]).age));
	printf("please input tel:  ");
	scanf("%s",(pcon->people[i]).tel);
	printf("please input address:  ");
	scanf("%s",(pcon->people[i]).addr);
}
void show_peo(pCon pcon)						//显示联系人
{	
	int i = 0;
	for(; i < pcon->count; i++)
	{
		printf("%s\t%s\t%d\t%s\t%s\n",
			pcon->people[i].name,
			pcon->people[i].sex,
			pcon->people[i].age,
			pcon->people[i].tel,
			pcon->people[i].addr);
	}
}
void clear_peo(pCon pcon)						//清空联系人
{
	pcon->count = 0;
}



#include "contact.h"
void show_menu()							//显示界面
{
	printf("************************\n");				
	printf("*  1:add     2:del     *\n");
	printf("*  3:find    4:modify  *\n");
	printf("*  5:show    6:clear   *\n");
	printf("************************\n");
}
int main()
{
	struct Contact my_contact;					//定义联系人类
	int input = 1;
	my_contact.count = 0;						//联系人数量初始化
	while(input)							//每次进行循环
	{
		show_menu();
		printf("please input:");
		scanf("%d",&input);
		switch(input)
		{
			case 1:
				add_peo(&my_contact);
				break;
			case 2:
				del_peo(&my_contact);
				break;
			case 3:
				find_peo(&my_contact);
				break;
			case 4:
				modify_peo(&my_contact);
				break;
			case 5:
				show_peo(&my_contact);
				break;
			case 6:
				clear_peo(&my_contact);
				break;
			default :
				break;
		}
	}
	return 0;
}

技术分享

技术分享

技术分享

技术分享
技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。