通讯录

test.c


#include "contact.h"



void menu()
{
printf("********************************\n");
printf("******* 1.add 2.del *****\n");
printf("******* 3.search 4.modify *****\n");
printf("******* 5.sort 6.print *****\n");
printf("******* 0.exit *****\n");
printf("********************************\n");
}


enum option
{ //枚举类型成员最好大写
EXIT,
add,
del,
search,
modify,
sort,
print

};

int main()
{
int input = 0;
//创建通讯录
Contact con;
//初始化通讯录
//给data申请一块连续的空间在堆上
InitContact(&con);
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case add:
AddContact(&con);
break;
case del:
DelContact(&con);
break;
case search:
SearchContact(&con);
break;
case modify:
ModifyContact(&con);
break;
case sort:
//自己完善
break;
case print:
PrintContact(&con); //即使不修改数据 传地址也比较好
break;
case EXIT:
DestoryContact(&con);
printf("退出通讯录\n");
break;
default:
printf("输入有误,请重新输入;>");
break;
}


} while (input);


return 0;
}


contact.h


#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
//头文件
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

//定义
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30

#define DEFAULT_SZ 3
#define INC_SZ 2

#define MAX 1000

//类型的定义
typedef struct PeoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];

char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo; //把struct PeoInfo 简写为 PeoInfo

typedef struct Contact
{
//创建通讯录 动态版本
PeoInfo* data; //申请动态空间
int sz;
int capacity;//最大容量
}Contact;

//初始化通讯录
void InitContact(Contact* pc);


//增加联系人
void AddContact(Contact* pc);


//打印通讯录信息
void PrintContact(const Contact* pc);


//删除通讯录的信息
void DelContact(Contact* pc);

//查找通讯录
void SearchContact(Contact* pc);


//FindByName定义
int FindByName(Contact* pc, char name[]);


//修改通讯录
void ModifyContact(Contact* pc);


//销毁通讯录
void DestoryContact(Contact *pc);


contact.c


#include "contact.h"
static int FindByName(Contact* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}

}
return -1; //找不到
}


//静态版本初始化
void InitContact(Contact* pc)
{
pc->data = (PeoInfo*)malloc(DEFAULT_SZ*sizeof(PeoInfo));
if (pc->data == NULL)
{
perror("InitContact");
return;
}
pc->sz = 0;//初始化之后默认是0
pc->capacity = DEFAULT_SZ;
}

void AddContact(Contact* pc)
{
//增加容量
if (pc->sz == pc->capacity)
{
PeoInfo* ptr = realloc(pc->data,(pc->capacity+INC_SZ)*sizeof(PeoInfo));
if (ptr != NULL)
{
pc->data = ptr;
pc->capacity += INC_SZ;
printf("增容成功\n");
}
else
{
perror("AddContact");
printf("增加联系人失败");
return;
}
}
// 增加一个人的信息
printf("输入名字;>");
scanf("%s", pc->data[pc->sz].name);
printf("输入年龄:>");
scanf("%d", &pc->data[pc->sz].age);
printf("输入性别:>");
scanf("%s", pc->data[pc->sz].sex);
printf("输入电话:>");
scanf("%s", pc->data[pc->sz].tele);
printf("输入地址:>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("增加成功\n");
}

void PrintContact(const Contact* pc)
{
int i = 0;
//打印标题
printf("%20s\t %5s\t %5s\t %12s\t %20s\n","name","age","sex","tele","addr");
for (i = 0; i < pc->sz; i++)
{
printf("%20s\t %5d\t %5s\t %12s\t %20s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr );

}

}
void DelContact(Contact* pc)
{
char name[MAX_NAME] = { 0 };

if (pc->sz == 0)
{
printf("通讯录为空,无需删除\n");
return;
}
printf("请输入要删除人的名字:>");
scanf("%s",name);
//查找
int pos = FindByName(pc, name);
//yes/no
if (pos == -1)
{
printf("要删除的人不存在\n");
}
else
{ //删除
int i = 0;
for (i = pos; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功\n");
}


}


void SearchContact(Contact* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要查找人的名字:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
}
else
{
int i = 0;
//打印标题
printf("%20s\t %5s\t %5s\t %12s\t %20s\n", "name", "age", "sex", "tele", "addr");


printf("%20s\t %5d\t %5s\t %12s\t %20s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);


}


}


void ModifyContact(Contact* pc)
{
char name[MAX_NAME] = { 0 };


printf("请输入要修改人的名字:>");
scanf("%s", name);
//查找
int pos = FindByName(pc, name);
//yes/no
if (pos == -1)
{
printf("要修改的人不存在\n");
}
else
{ //修改
// 增加一个人的信息
printf("输入名字;>");
scanf("%s", pc->data[pos].name);
printf("输入年龄:>");
scanf("%d", &pc->data[pos].age);
printf("输入性别:>");
scanf("%s", pc->data[pos].sex);
printf("输入电话:>");
scanf("%s", pc->data[pos].tele);
printf("输入地址;>");
scanf("%s", pc->data[pos].addr);
printf("修改成功\n");
}

}


void DestoryContact(Contact* pc)
{



free(pc->data);
pc->data = NULL;
pc->sz = 0;
pc->capacity = 0;
}