c语言实现通讯录(静态)

额 通讯录其实功能很简单 有增加,删除,修改,查找等等 要实现这些功能,就要把这些函数封装成一个个模块,然后在拼凑在一起就行了

第一步 先写主函数:

#include"contact.h"
void menu()
{
    printf("*************通讯录**********************\n");
    printf("*********1  增加成员*********************\n");
    printf("*********2  删除成员*********************\n");
    printf("*********3  查找成员*********************\n");
    printf("*********4  修改成员*********************\n");
    printf("*********5  显示成员*********************\n");
    printf("*********6  清空成员*********************\n");
    printf("*********7  排序*********************\n");
    printf("*********8  退出*********************\n");
}
int main(){
    int n;
    Book people;
    Initbook(&people);
    while (1){
        menu();
        printf("please input a num you want:");
        scanf_s("%d", &n);
        switch (n)
        {
        case 1:
            Add_person(&people);
            break;
        case 2:
            Del_person(&people);
            break;
        case 3:
            Find_person(&people);
            break;
        case 4:
            Change_person(&people);
            break;
        case 5:
            show_person(&people);
            break;
        case 6:
            Initbook(&people);
            break;
        case 7:
            BubbleSort_person(&people);
        case 8:
            exit(0);
        default:
            printf("enter error!");
            break;
        }
    }
    system("pause");
    return 0;
}

完成后如图
这里写图片描述

第二部 把要使用到的头文件放在一个头文件函数中,命名为contact.h:

#ifndef CONTACT_H
#define CONTACT_H

#endif

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

#define NameMax 20  
#define SexMax 3  
#define TelMax 11  
#define AddressMax 20  
#define PeopleMax 1000  

typedef char Persontype;

typedef struct PERSON
{
    Persontype Name[NameMax];//  姓名  
    Persontype Sex[SexMax];  // 性别  
    int Age;          // 年龄  
    Persontype Tel[TelMax]; //联系方式  
    Persontype Address[AddressMax]; //地址  

}Person;

typedef struct BOOK
{
    Person Data[PeopleMax];//Address_Book结构体最大存储容量  
    int count;    //当前已存储人数  
}Book;

void Initbook(Book* people); //初始化  

void show_person(Book* people);//打印  

void Add_person(Book *people);// 增加成员  

void Del_person(Book* people);//删除成员  

int Find_person(Book* people);//查找成员  

void Change_person(Book* people);//修改成员  

void BubbleSort_person(Book* people);//冒泡排序  

第三步 函数实现各个功能:

#define _CRT_SECURE_NO_WARNINGS 1

#include"contact.h"


//初始化  
void Initbook(Book* people)
{
    people->count = 0;
}

//打印  
void show_person(Book* people)
{
    if (people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    if (people->count == 0)
    {
        printf("空通讯录!\n");
    }
    int i = 0;
    for (; i <= people->count - 1; ++i)
    {
        printf("Name:%s\n", people->Data[i].Name);
        printf("Sex:%s\n", people->Data[i].Sex);
        printf("Age:%d\n", people->Data[i].Age);
        printf("Tel:%s\n", people->Data[i].Tel);
        printf("Add:%s\n", people->Data[i].Address);
    }
    printf("\n");
}


//增加成员  
void Add_person(Book *people)
{
    assert(people);
    int i = 0;
    if (people->count > PeopleMax)
    {
        printf("通讯录已满!\n");
        return;
    }
    printf("请输入姓名:\n");
    scanf("%s", (people->Data[people->count]).Name);
    printf("请输入性别:\n");
    scanf("%s", (people->Data[people->count]).Sex);
    printf("请输入年龄:\n");
    scanf("%d", &(people->Data[people->count]).Age);
    printf("请输入联系方式:\n");
    scanf("%s", (people->Data[people->count]).Tel);
    //for(;i<people->count;++i)  
    //{  
    //    if(strcmp(people->Data[i].Tel, people->Data[people->count].Tel) == 0)  
    //    {  
    //        printf("输入有误或联系人已存在!\n");  
    //    }  
    //    return;  
    //}  
    printf("请输入住址:\n");
    scanf("%s", (people->Data[people->count]).Address);
    printf("增加成功!\n");
    people->count++;
    printf("count = %d\n", people->count);

}
//删除成员  
void Del_person(Book* people)
{
    if (people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    int ret = Find_person(people);
    if (ret != -1)
    {
        int i = ret;
        for (; i<people->count - 1; ++i)
        {
            people->Data[i] = people->Data[i + 1];
        }
        people->count--;
        printf("count = %d\n", people->count);
    }
    else
    {
        printf("没有该成员!\n");
    }
}

//查找成员  
int Find_person(Book* people)
{
    assert(people);
    char name[20];
    scanf("%s", name);
    int i = 0;
    for (; i<people->count; ++i)
    {
        if (strcmp(people->Data[i].Name, name) == 0)
        {
            printf("Name:%s\tSex:%s\tAge:%d\tTel:%s\tAdd\n", people->Data[i].Name, people->Data[i].Sex, people->Data[i].Age, people->Data[i].Tel, people->Data[i].Address);
            return i;
            printf("下标为 %d\n", i);
        }
    }
    printf("没有找到该联系人!\n");
    return -1;
}

//修改成员  
void Change_person(Book* people)
{
    assert(people);
    int ret = Find_person(people);
    if (ret != -1)
    {
        printf("Name->:\n");
        scanf("%s", people->Data[ret].Name);
        printf("Sex->:\n");
        scanf("%s", people->Data[ret].Sex);
        printf("Age->:\n");
        scanf("%d", &(people->Data[ret]).Age);
        printf("Tel->:\n");
        scanf("%s", people->Data[ret].Tel);
        printf("Add->:\n");
        scanf("%s", people->Data[ret].Address);
    }
    else
    {
        printf("没有该成员!\n");
        return;
    }
}

void BubbleSort_person(Book* people)
{
    if (people == NULL)
    {
        printf("空通讯录!\n");
        return;
    }
    int i = 0;
    for (; i<people->count - 1; ++i)
    {
        int j = 0;
        for (; j<people->count - 1 - i; ++j)
        {
            if (strcmp(people->Data[j].Name, people->Data[j + 1].Name) > 0)
            {
                char tmp[NameMax];
                strcpy(tmp, people->Data[j].Name);
                strcpy(people->Data[j].Name, people->Data[j + 1].Name);
                strcpy(people->Data[j + 1].Name, tmp);
            }
        }
    }
    show_person(people);
}

好了 这样就完成了简易的通讯录了。简单实现一把:

添加成员:
这里写图片描述

查找成员:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/xy294636185/article/details/80369826