码迷,mamicode.com
首页 > 其他好文 > 详细

创建通讯录实现(增,删,查,改,显示,清空)功能

时间:2015-05-21 09:09:21      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:通讯录            

contact.h

#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

test.c

#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 con;
    int input = 1;
    con.count = 0;
    while(input)
    {
        print_menu();
        printf("请选择操作:\n");
        scanf("%d",&input);
        switch(input)
        {
        case 1:

            add_peo(&con);
            break;
        case 2:
            del_peo(&con);
            break;
        case 3:
            search(&con);
            break;
        case 4:
            modify(&con);
            break;
        case 5:
            show(&con);
            break;
        case 6:
            clear(&con);
            break;
        default:
            break;
        }
    }
    return 0;
}

main.c

#include<stdio.h>
#include<string.h>
#include"contact.h"

int find(char *p, Pcon pcon)
{
    int i = 0;
    for(;i < pcon->count; i++)
    {
        if(strcmp(pcon->dhb[i].name,p) == 0)
            return i;
    }
    return -1;
}

void add_peo(Pcon pcon)
{
    if(pcon->count < MAX_PEO)
    {
        printf("请输入姓名、年龄、性别、电话、住址:\n");
        scanf("%s",pcon->dhb [pcon->count].name);
        scanf("%d",&pcon->dhb [pcon->count].age);
        scanf("%s",pcon->dhb [pcon->count].sex);
        scanf("%s",pcon->dhb [pcon->count].tele);
        scanf("%s",pcon->dhb [pcon->count].addr);

    }
    pcon->count ++;
    printf("存储成功\n");

}
void del_peo(Pcon pcon)
{
    int i = 0;
    int ret = 0;
    char name[10];
    printf("请输入要删除的名字:\n");
    scanf("%s",name);
    ret = find(name,pcon);
    if(ret == -1)
    {
        printf("无此联系人\n");
    }
    else
        for(i = ret; i < pcon->count-1; i++)
        {
            pcon->dhb[i] = pcon->dhb[i + 1];
        }
        pcon->count--;
        printf("删除成功\n");
}
void search(Pcon pcon)
{
    int i = 0;
    int ret = 0;
    char name[10];
    printf("请输入要查找人的名字:\n");
    scanf("%s",name);
    ret = find(name,pcon);
    if(ret == -1)
    {
        printf("无此联系人\n");
    }
    else
    {
        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);
        printf("\n");
    }
}

void modify(Pcon pcon)
{
    int i = 0;
    int ret = 0;
    char name[10];
    printf("请输入要查找人的名字:\n");
    scanf("%s",name);
    ret = find(name,pcon);
    printf("请输入姓名、年龄、性别、电话、住址:\n");
    scanf("%s",pcon->dhb [i].name);
    scanf("%d",&pcon->dhb [i].age);
    scanf("%s",pcon->dhb [i].sex);
    scanf("%s",pcon->dhb [i].tele);
    scanf("%s",pcon->dhb [i].addr);
    printf("修改成功\n");

}

void show(Pcon pcon)
{
    int i = 0;
    for(; 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);
        printf("\n");
    }
}

void clear(Pcon pcon)
{
    pcon->count = 0;
}

创建通讯录实现(增,删,查,改,显示,清空)功能

标签:通讯录            

原文地址:http://blog.csdn.net/yangrujing/article/details/45877469

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