您的位置:首页 > 编程语言 > C语言/C++

C语言实现简单的通讯录

2015-03-17 11:37 387 查看
实现一个通讯录;

通讯录可以用来存储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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: