码迷,mamicode.com
首页 > 编程语言 > 详细

【C语言】实现一个简单的通讯录

时间:2015-05-20 14:49:37      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:

<span style="font-size:18px;">#include<stdio.h>
#include"contact.h"

void print_menu()
{
	printf("*******************************\n");
	printf("***1>add       2>del     ******\n");
	printf("***3>search    4>modify  ******\n");
	printf("***5>show      6>clear   ******\n");
	printf("*******************************\n");
}
int main()
{
	PCON pco;
	int input=1;
	pco.count=0;
	while(input)
	{
		print_menu();
		printf("请选择:");
		scanf("%d",&input);
		switch(input)
		{
		case 1:
			add_peo(&pco);
			break;
		case 2:
			del_peo(&pco);
			break;
		case 3:
			search(&pco);
			break;
		case 4:
			modify(&pco);
			break;
		case 5:
			show(&pco);
			break;
		case 6:
			clear(&pco);
			break;
		default :
			break;
		}
	}
	return 0;
}
</span>
<span style="font-size:18px;">#ifndef __CONTACT_H
#define __CONTACT_H_

#define NAME_LEN 20
#define SEX_LEN 5
#define TELE_LEN 12
#define ADDR_LEN 20
#define MAX_PEO 1000

typedef struct Peo
{
	char name[NAME_LEN];
	int age;
	char sex[SEX_LEN];
	char tele[TELE_LEN];
	char addr[ADDR_LEN];

}PEO,*PPEO;
typedef struct Contact
{
	PEO dhb[MAX_PEO];//存储人的信息
	int count;//记录有效人的个数
}PCON,*Pcon;

void add_peo(Pcon pcon);
void del_peo(Pcon pcon);
void search(Pcon pcon);
void modify(Pcon pcon);
void show(Pcon pcon);
void clear(Pcon pcon);

#endif
</span>

<span style="font-size:18px;">#include<string.h>
#include"contact.h"

int find(Pcon pcon)
{
	int i=0;
	char name[NAME_LEN];
	printf("请输入姓名:");
	scanf("%s",name);
	for(i=0;i<=pcon->count;i++)
	{
		if(strcmp(name,pcon->dhb[i].name)==0)
			return i;
	} 
	return -1;
}


void add_peo(Pcon pcon)
{
	if(pcon->count>MAX_PEO)
		printf("电话本已满");
	else
	{
		printf("姓名:");
		scanf("%s",pcon->dhb[pcon->count].name);
		printf("年龄:");
		scanf("%d",&(pcon->dhb[pcon->count].age));
		printf("性别:");
		scanf("%s",pcon->dhb[pcon->count].sex);
		printf("电话:");
		scanf("%s",pcon->dhb[pcon->count].tele);
		printf("住址:");
		scanf("%s",pcon->dhb[pcon->count].addr);
	}
	pcon->count++;
	printf("添加成功!")
}
void del_peo(Pcon pcon)
{
	int i=0;
	int ret=find(pcon);
	if(ret!=-1)
	{
		for(i=ret;i<pcon->count-1;i++)
		{
			pcon->dhb[i]=pcon->dhb[i+1];
		}
		pcon->count--;
		
	}
	else
		printf("未找到要删除的人");
	printf("删除成功!");
}
void search(Pcon pcon)
{
	int ret=find(pcon);
	if(ret!=-1)
	{
		printf("%10s\t",pcon->dhb[ret].name);
		printf("%10d\t",pcon->dhb[ret].age);
		printf("%10s\t",pcon->dhb[ret].sex);
		printf("%10s\t",pcon->dhb[ret].tele);
		printf("%10s\t",pcon->dhb[ret].addr);
	}
	else
		printf("未找到此人");
}
void modify(Pcon pcon)
{
	int ret;
	ret=find(pcon);
	if(ret!=-1)
	{
		printf("姓名:");
		scanf("%s",pcon->dhb[ret].name);
		printf("年龄:");
		scanf("%d",&(pcon->dhb[ret].age));
		printf("性别:");
		scanf("%s",pcon->dhb[ret].sex);
		printf("电话:");
		scanf("%s",pcon->dhb[ret].tele);
		printf("住址:");
		scanf("%s",pcon->dhb[ret].addr);
	}
	else
		printf("未找到要修改的人");
}
void show(Pcon pcon)
{
	int i=0;
	for(i=0;i<pcon->count;i++)
	{
		printf("%10s\t",pcon->dhb[i].name);
		printf("%10d\t",pcon->dhb[i].age);
		printf("%10s\t",pcon->dhb[i].sex);
		printf("%10s\t",pcon->dhb[i].tele);
		printf("%10s\t",pcon->dhb[i].addr);
	}
}
void clear(Pcon pcon)
{
	pcon->count=0;
}</span>


【C语言】实现一个简单的通讯录

标签:

原文地址:http://blog.csdn.net/sunshine552/article/details/45870255

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!