C语言 通讯录

实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息

6. 清空所有联系人

定义头文件

<span style="font-size:18px;">#ifndef __CONTACT_H__
#define __CONTACT_H__

#define NAME_MAX 20
#define SEX_MAX  5
#define TELE_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 tele[TELE_MAX];
    char addr[ADDR_MAX];
}*pPeople;

typedef struct contact
{
    int count;
    struct people DHB[PEO_MAX];

}*pContact;

void add_peo(pContact pcon);
void del_peo(pContact pcon);
void search_peo(pContact pcon);
void modify_peo(pContact pcon);
void show_peo(pContact pcon);
void clear_peo(pContact pcon);
#endif
</span>
函数实现部分

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

int find(pContact pcon,char* name)
{
    int i = 0;
    while(i<pcon->count)
    {
        if(strcmp(name,pcon->DHB[i].name) == 0)
            return i;
            i++;
    }
     return -1;
}
void add_peo(pContact pcon)         //添加联系人
{
    if(pcon->count == PEO_MAX)
    {
        printf("电话本容量已达上限\n");
        return;
    }
    printf("name:>");
    scanf("%s",(pcon->DHB[pcon->count]).name);
    printf("sex:>");
    scanf("%s",(pcon->DHB[pcon->count]).sex);
    printf("age:>");
    scanf("%d",&((pcon->DHB[pcon->count]).age));
    printf("tele:>");
    scanf("%s",(pcon->DHB[pcon->count]).tele);
    printf("addr:>");
    scanf("%s",(pcon->DHB[pcon->count]).addr);
    pcon->count++;
}
void del_peo(pContact pcon)         //删除联系人
{
    char tmpName[NAME_MAX];
    int ret=0;
    printf("输入需删除的联系人姓名:>");
    scanf("%s",tmpName);
    if(-1 != (ret=find(pcon,tmpName)))
    {
        int j=ret;
        for(;j<pcon->count;j++)
        {
            pcon->DHB[j] = pcon->DHB[j+1];
        }
        pcon->count -= 1;
    }
    else
        printf("该联系人不存在\n");
}
void search_peo(pContact pcon)      //查找联系人
{
    char tmpName[NAME_MAX];
    int ret = 0;
    printf("请输入要查找的联系人");
    scanf("%s",tmpName);
    ret = find(pcon,tmpName);
    if(ret != -1)
    {
        printf("%s\t%s\t%d\t%s\t%s\n",
               pcon->DHB[ret].name,
               pcon->DHB[ret].sex,
               pcon->DHB[ret].age,
               pcon->DHB[ret].tele,
               pcon->DHB[ret].addr);
    }
}
void modify_peo(pContact pcon)      //修改联系人
{
    char tmpName[NAME_MAX];
    int ret = 0;
    printf("请输入要修改的联系人");
    scanf("%s",tmpName);
    ret = find(pcon,tmpName);
    if(-1 != ret)
    {
        printf("name:>");
        scanf("%s",(pcon->DHB[ret]).name);
        printf("sex:>");
        scanf("%s",(pcon->DHB[ret]).sex);
        printf("age:>");
        scanf("%d",&((pcon->DHB[ret]).age));
        printf("tele:>");
        scanf("%s",(pcon->DHB[ret]).tele);
        printf("addr:>");
        scanf("%s",(pcon->DHB[ret]).addr);
    }
}
void show_peo(pContact pcon)        //显示全部联系人
{
    int i = 0;
    for(i=0;i<pcon->count;i++)
    {
        printf("%s\t%s\t%d\t%s\t%s\n",
               pcon->DHB[i].name,
               pcon->DHB[i].sex,
               pcon->DHB[i].age,
               pcon->DHB[i].tele,
               pcon->DHB[i].addr);
    }
}
void clear_peo(pContact pcon)       //清空全部联系人
{
    pcon->count = 0;
}</span>

测试函数

<span style="font-size:18px;">#include "contact.h"
void show_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()
{
    struct contact my_contact;
    my_contact.count = 0;
    int input = 1;
    while(input)
    {
        show_menu();
        printf("please input:>");
        scanf("%d",&input);
        switch(input)
        {
            case 1:
                add_peo(&my_contact);
                system("cls");      //清屏
                break;
            case 2:
                del_peo(&my_contact);
                system("cls");
                break;
            case 3:
                search_peo(&my_contact);
                break;
            case 4:
                modify_peo(&my_contact);
                system("cls");
                break;
            case 5:
                show_peo(&my_contact);
                break;
            case 6:
                clear_peo(&my_contact);
                system("cls");
                break;
            default:
                break;
        }
    }
    return 0;
}
</span>

技术分享

技术分享

技术分享

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