C语言实现简单的通讯录

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


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

6. 清空所有联系人


/****************************************
*  File Name  : contacts.h
*  Creat Data : 2015.3.17
*  Author     : WK
*****************************************/
#ifndef __CONTACTS_H__
#define __CONTACTS_H__

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 1000
#define NAME_LENGTH 20
#define SEX_LENGTH 5
#define TELE_LENGTH 20
#define ADDR_LENGTH 30

struct PeopleInfo
{
	char name[NAME_LENGTH];
	char sex[SEX_LENGTH];
	int age;
	char tele[TELE_LENGTH];
	char addr[ADDR_LENGTH];
};

struct Contacts
{
	struct PeopleInfo person[MAX];
	int user_count;
};

typedef struct Contacts *pContacts;

int add_mess(pContacts pcon);
int dele_mess(pContacts pcon);
int clear_mess(pContacts pcon);
int find_mess(pContacts pcon);
int modify_mess(pContacts pcon);
void show_mess(pContacts pcon);

#endif


/****************************************
*  File Name  : contact.c
*  Creat Data : 2015.3.17
*  Author     : WK
*****************************************/
#include "contacts.h"

int find_entry(pContacts pcon)//找到该人信息所存储的数组下标
{
	int i = 0 ;
	char name[NAME_LENGTH];
	printf("Please input name:");
	scanf("%s",name);
	for(i = 0;i < pcon->user_count;i++)
	{
		if(strcmp(pcon->person[i].name ,name) == 0)
		{
			return i;
		}
	}
	return -1;
}
int add_mess(pContacts pcon)
{
	if(pcon->user_count == MAX)
	{
		printf("Telephone book is full!\n");
		return -1;
	}
	printf("Please input name:");
	scanf("%s",pcon->person[pcon->user_count].name);
	printf("Please input sex:");
	scanf("%s",pcon->person[pcon->user_count].sex);
	printf("Please input age:");
	scanf("%d",&(pcon->person[pcon->user_count].age));
	printf("Please input tele:");
	scanf("%s",pcon->person[pcon->user_count].tele);
	printf("Please input addr:");
	scanf("%s",pcon->person[pcon->user_count].addr);
	pcon->user_count++;//人数加一
	return 1;
}
int dele_mess(pContacts pcon)
{
	int i = 0;
	int ret = find_entry(pcon);
	if(ret != -1)
	{
		for(i = ret;i < pcon->user_count-1;i++)//注意此处要照顾到i+1,当i=user_count-1-1时,i+1=user_count,否则就越界了
		{
			pcon->person[i] = pcon->person[i+1];
		}
		pcon->user_count--;//人数减一
		return 1;
	}
	else
	{
		printf("not exist!\n");
		return -1;
	}
}
int clear_mess(pContacts pcon)
{
	pcon->user_count = 0;
	return 1;
}
int find_mess(pContacts pcon)
{
	int ret = find_entry(pcon);
	if(ret != -1)
	{
		printf("name:%s\n",pcon->person[ret].name);
		printf("sex:%s\n",pcon->person[ret].sex);
		printf("age:%d\n",pcon->person[ret].age);
		printf("tele:%s\n",pcon->person[ret].tele);
		printf("addr:%s\n",pcon->person[ret].addr);
		return 1;
	}
	else
	{
		printf("not exist!\n");
		return -1;
	}
}
int modify_mess(pContacts pcon)
{
	int ret = find_entry(pcon);
	if(ret != -1)
	{
		printf("Please input name:");
		scanf("%s",pcon->person[ret].name);
		printf("Please input sex:");
		scanf("%s",pcon->person[ret].sex);
		printf("Please input age:");
		scanf("%s",pcon->person[ret].age);
		printf("Please input tele:");
		scanf("%s",pcon->person[ret].tele);
		printf("Please input addr:");
		scanf("%s",pcon->person[ret].addr);
		return 1;
	}
	else
	{
		printf("not exist!\n");
		return -1;
	}		
}
void show_mess(pContacts pcon)
{
	int i = 0;
	printf("\tname\tsex\t\tage\t\ttele\t\t\taddr\n");
	for(i = 0;i < pcon->user_count;i++)
	{
		printf("%10s\t",pcon->person[i].name);
		printf("%5s\t", pcon->person[i].sex);
		printf("%10d\t",pcon->person[i].age);
		printf("%15s\t",pcon->person[i].tele);
		printf("%20s\t",pcon->person[i].addr);
	}
	printf("\n");
}


/****************************************
*  File Name  : main.c
*  Creat Data : 2015.3.17
*  Author     : WK
*****************************************/
#include "contacts.h"

int main()
{	
	int num=0;
	struct Contacts person;
	person.user_count = 0;
  
  printf("                     welcome using\n");
  printf("\n");
  printf("         The telephone_Book Management System    \n");
  printf("\n");
  printf("     *******************Menu********************\n");
  printf("     *       1  add person's message           *\n");
  printf("     **      2  delete person's message        *\n");
  printf("     ***     3  find person's message          *\n");
  printf("     ****    4  modify person's message        *\n");
  printf("     *****   5  show all of person's message   *\n");
  printf("     ******  6  clear all of person's message  *\n");
  printf("     !!!!!!! 0  quit                           *\n");
  printf("     *******************************************\n");
  while(1)
  {
  printf("please input a number which is you want to operate: ");
  scanf("%d",&num);
  switch(num)
  {
  case 1:    add_mess( &person);
       break;
     case 2:   dele_mess( &person);
      break;
  case 3:   find_mess( &person);
      break;
  case 4:   modify_mess( &person);
      break;
  case 5:   show_mess(&person);
      break;
  case 6:   clear_mess( &person);
     break;
  case 0:   printf("Dear! Thanks for using\n");
     break;
   
  default:  printf("input error");
        break;
  }
  }	
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值