contact.h
#define MAX 1000

#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30

#include
#include

enum Option
{
    EXIT,//0
    ADD,
    DEL,
    SEARCH,
    MODIFY,
    SHOW,
    SORT
};
struct PeoInfo
{
    char name[MAX_NAME];
    int age;
    char sex[MAX_SEX];
    char tele[MAX_TELE];
    char addr[MAX_ADDR];
}; 
//通讯录类型
struct Contact
{
    struct PeoInfo data[MAX];//存放一个信息
    int size;//记录当前已有的元素个数
};
//声明函数
//初始化通讯录的函数
void InitContact(struct Contact* ps);

//增加一个信息的函数
void AddContact(struct Contact* ps);

//打印通讯录中的信息
void ShowContact(const struct Contact* ps);

//删除指定联系人
void DelContact(struct Contact* ps);

//查找指定联系人
void SearchContact(const struct Contact* ps);

//修改指定联系人
void ModifyContact(struct Contact* ps);

//排序通讯录内容
void SortContact(struct Contact* ps);

contact.c
#include "contact.h"

void InitContact(struct Contact* ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->size = 0;//设置通讯录最初只有0个元素
}

void AddContact(struct Contact* ps)
{
if (ps->size == MAX)
{
printf("通讯录已满,无法增加\n");
}
else
{
printf("请输入名字:>");
scanf("%s", ps->data[ps->size].name);
printf("请输入年龄:>");
scanf("%d", &(ps->data[ps->size].age));
printf("请输入性别:>");
scanf("%s", ps->data[ps->size].sex);
printf("请输入电话:>");
scanf("%s", ps->data[ps->size].tele);
printf("请输入地址:>");
scanf("%s", ps->data[ps->size].addr);

    ps->size++;
    printf("添加成功\n");
}

}

void ShowContact(const struct Contact* ps)
{
if (ps->size == 0)
{
printf("通讯录为空白\n");
}
else
{
int i = 0;
printf("%-8s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < ps->size; i++)
{
printf("%-8s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[i].name,
ps->data[i].age,
ps->data[i].sex,
ps->data[i].tele,
ps->data[i].addr);
}
}
}

static int FindByName(const struct Contact* ps,char name[MAX_NAME])
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (0 == strcmp(ps->data[i].name, name))
{
return i;
}
}
return -1;//找不到返回 -1
}

void DelContact(struct Contact* ps)
{
char name[MAX_NAME];
printf("请输入要删除的人的名字:>");
scanf("%s", name);
//1.查找删除的人的位置
//找到了返回名字所在元素的下标
//找不到返回 -1
int pos = FindByName(ps, name);
//2.删除
if (pos == -1)
{
printf("要删除的人不存在\n");
}
else
{
//删除数据
int j = 0;
for (j = pos; j < ps->size-1; j++)
{
ps->data[j] = ps->data[j + 1];
}
ps->size--;
printf("删除成功\n");
}
}

void SearchContact(const struct Contact* ps)
{
char name[MAX_NAME];
printf("请输入要查找人的名字:>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("查无此人\n");
}
else
{
printf("%-8s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-8s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[pos].name,
ps->data[pos].age,
ps->data[pos].sex,
ps->data[pos].tele,
ps->data[pos].addr);
}
}

void ModifyContact(struct Contact* ps)
{
char name[MAX_NAME];
printf("请输入要修改的联系人的姓名:>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("要修改联系人不存在");
}
else
{
printf("请输入名字:>");
scanf("%s", ps->data[pos].name);
printf("请输入年龄:>");
scanf("%d", &(ps->data[pos].age));
printf("请输入性别:>");
scanf("%s", ps->data[pos].sex);
printf("请输入电话:>");
scanf("%s", ps->data[pos].tele);
printf("请输入地址:>");
scanf("%s", ps->data[pos].addr);

    printf("修改完成\n");
}

}

void SortContact(struct Contact* ps)
{
if (ps->size == 0)
{
printf("通讯录中无联系人,排序错误,请先添加联系人\n");
}
else
{
int i, j;
for (i = 0; i < ps->size; i++)
{
for (j = 0; j < ps->size - i - 1; j++)
{
if (strcmp(ps->data[j].name, ps->data[j + 1].name) > 0)
{
printf("1");
struct PeoInfo str = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = str;
}
}
}
printf("按照姓氏已排序完成\n");
}
}

#include "contact.h"

void menu()
{
    printf("****************************\n");
    printf("****  1.add     2.del    ***\n");
    printf("****  3.serch   4.modify  **\n");
    printf("****  5.show    6.sort   ***\n");
    printf("****  0.exit    ************\n");
    printf("****************************\n");
}
int main()
{
    int input = 0;
    //创建通讯录
    struct Contact con;//con就是通讯录,里边包含:1000个元素的数和size
    //初始化通讯录 
    InitContact(&con);
    //
    do
    {
        menu();
        printf("请选择:>");
        scanf_s("%d", &input);
        switch (input)
        {
        case ADD:
            AddContact(&con);
            break;
        case DEL:
            DelContact(&con);
            break;
        case SEARCH:
            SearchContact(&con);
            break;
        case MODIFY:
            ModifyContact(&con);
        case SHOW:
            ShowContact(&con);
            break;
        case SORT:
            SortContact(&con);
            break;
        case EXIT:
            printf("退出通讯录\n");
            break;
        default:
            printf("选择错误\n");
            break;
        }
    } while (input);
    return 0;
}