通讯录中包含的内容:我们用结构体实现
typedef struct person{
char name[32];
char sex[8];
int age;
int number[16];
char adress[64];
}person_t;
typedef struct contact{//通讯录大小,保存了多少联系人以及联系人的信息(柔性数组)
int cap;
int size;
person_t persons[0];
}contact_t;
通讯录首先要有一个菜单:
void Menu(){
printf("# 1.Add 2.Delet #\n");
printf("# 3.Find 4.Change #\n");
printf("# 5.Show 6.Clear #\n");
printf("# 7.Sort 0.Exit #\n");
printf("请输入你的选择......");
}
通讯录的初始化:
void initContact(contact_t **ctp){
*ctp = (contact_t*)malloc(sizeof(contact_t)+sizeof(person_t)*DFL_NUM);
if (NULL == *ctp){
perror("malloc");
exit(1);
}
(*ctp)->cap = DFL_NUM;
(*ctp)->size = 0;
printf("default Contact init....done\n");
}
通讯录需要的操作:我们用一个个个的函数实现
由于添加联系人我们需要
1. 判断空间是否满了
2.空间满了的话开辟新的空间
3.判断要添加的联系人是否已经存在
4.输入联系人的各种信息等等
所以我们设计了相应的函数
判断空间是否已经满了(满了为真,不满为假)
//判断空间是否满了
int isFull(const contact_t *ct){
return ct->cap == ct->size;
}
如果空间满了进行空间的扩容
int Inc(contact_t **ctp){//空间的扩容
contact_t *ct = (contact_t*)realloc(*ctp, sizeof(contact_t)+sizeof(person_t)*((*ctp)->cap + INC_NUM));
if (ctp == NULL){//判断空间是否扩容成功
perror("realloc");
return 0;
}
ct->cap = (*ctp)->cap + INC_NUM;
*ctp = ct;
printf("扩容成功,cap:%d,size:%d\n", ct->cap, ct->size);
return 1;
}
判断联系人是否哦存在
int FindPerson(const contact_t *ct, const char*name){
const person_t*p = ct->persons;
for (int i = 0; i < ct->size; i++){
if (strcmp(p[i].name, name) == 0){
return i;
}
}
return -1;
}
1.添加联系人:
void AddPerson(contact_t **ctp){
person_t *p = (*ctp)->persons;
if (!isFull(*ctp) || Inc(ctp)){
contact_t *ct = *ctp;
person_t *p = ct->persons + ct->size;
printf("请输入名字: \n");
scanf("%s", p->name);
if (FindPerson(*ctp, p->name) >= 0){
printf("[%s]已经存在了\n", p->name);
return;
}
printf("请输入这个人的性别\n");
scanf("%s", p->sex);
printf("请输入这个人的年龄\n");
scanf("%d", &(p->age));
printf("请输入这个人的电话\n");
scanf("%s", p->number);
printf("请输入你的地址\n");
scanf("%s", p->adress);
(ct->size)++;
}
}
2.删除联系人
void DelPerson(contact_t *ct){
char name[32] = { 0 };
printf("请输入要删除人的姓名:\n");
scanf("%s", name);
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
ct->persons[i] = ct->persons[ct->size];
(ct->size)--;
printf("删除成功!\n");
return;
}
}
printf("要删除的人不存在!\n");
}
3.查找联系人
void SearchPerson(const contact_t *ct){
char name[32] = { 0 };
printf("请输入查找人的姓名:\n");
scanf("%s", name);
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
printf("| %10s | %4s | %3d | %8s | %8s |\n", ct->persons[i].name, ct->persons[i].sex, \
ct->persons[i].age, ct->persons[i].number, ct->persons[i].adress);
return;
}
}
printf("I can't find he!\n");
}
4.更改联系人的某项信息
void ModPerson(contact_t *ct){
char name[32] = { 0 };
printf("请输入要更改人的姓名");
scanf("%s", name);
person_t *p = ct->persons;
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
int n = 0;
printf("请选择要修改的信息:1.姓名 2.性别 3.年龄 4.电话号码 5.地址 \n");
scanf("%d", &n);
switch (n){
case 1:
printf("请输入姓名:\n");
scanf("%s", p->name);
break;
case 2:
printf("请输入性别:\n");
scanf("%s", p->sex);
break;
case 3:
printf("请输入年龄:\n");
scanf("%d", &(p->age));
break;
case 4:
printf("请输入电话号码:\n");
scanf("%s", p->number);
break;
case 5:
printf("请输入地址:\n");
scanf("%s", p->adress);
break;
default:
printf("你输入的有问题!\n");
}
}
return;
}
printf("I can't find he!\n");
}
5.显示整个通讯录
void PrintContact(contact_t *ct){
for (int i = 0; i < ct->size; i++){
printf("|%-10s|%-4s|%-3d|%8s|%8s|\n", ct->persons[i].name, \
ct->persons[i].sex, ct->persons[i].age, \
ct->persons[i].number, ct->persons[i].adress);
}
}
6.清空整个通讯录
void ClearContact(contact_t*ct){//直接将size指向0
printf("Clear...done\n");
ct->size = 0;
}
7.按名字对通讯录进行排序
int ComperName(const void *xp, const void *yp){
const person_t *p = (const person_t*)xp;
const person_t *q = (const person_t*)yp;
return strcmp(p->name, q->name);
}
void SortContact(contact_t *ct){//使用qsort函数进行排序
qsort(ct->persons, ct->size, sizeof(person_t), ComperName);
}
全部代码如下:
#include
#include
#include
#include
#pragma warning(disable:4996)
#define DFL_NUM 1
#define INC_NUM 1
static void Menu(){
printf("# 1.Add 2.Delet #\n");
printf("# 3.Find 4.Change #\n");
printf("# 5.Show 6.Clear #\n");
printf("# 7.Sort 0.Exit #\n");
printf("请输入你的选择......");
}
typedef struct person{
char name[32];
char sex[8];
int age;
int number[16];
char adress[64];
}person_t;
typedef struct contact{
int cap;
int size;
person_t persons[0];
}contact_t;
extern void Menu();
extern void initContact(contact_t **ctp);
extern void AddPerson(contact_t **ctp);
extern void DelPerson(contact_t *ct);
extern void SearchPerson(const contact_t *ct);
extern void ModPerson(contact_t *ct);
extern void PrintContact(contact_t *ct);
extern void ClearContact(contact_t*ct);
extern void SortContact(contact_t *ct);
static int isFull(const contact_t *ct){
return ct->cap == ct->size;
}
static int Inc(contact_t **ctp){
contact_t *ct = (contact_t*)realloc(*ctp, sizeof(contact_t)+sizeof(person_t)*((*ctp)->cap + INC_NUM));
if (ctp == NULL){
perror("realloc");
return 0;
}
ct->cap =(*ctp)->cap + INC_NUM;
*ctp = ct;
printf("扩容成功,cap:%d,size:%d\n", ct->cap, ct->size);
return 1;
}
static int FindPerson(const contact_t *ct, const char*name){
const person_t*p = ct->persons;
for (int i = 0; i < ct->size; i++){
if (strcmp(p[i].name, name) == 0){
return i;
}
}
return -1;
}
static int ComperName(const void *xp, const void *yp){
const person_t *p = (const person_t*)xp;
const person_t *q = (const person_t*)yp;
return strcmp(p->name, q->name);
}
void SortContact(contact_t *ct){
qsort(ct->persons, ct->size, sizeof(person_t), ComperName);
}
void ModPerson(contact_t *ct){
char name[32] = { 0 };
printf("请输入要更改人的姓名");
scanf("%s", name);
person_t *p = ct->persons;
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
int n = 0;
printf("请选择要修改的信息:1.姓名 2.性别 3.年龄 4.电话号码 5.地址 \n");
scanf("%d", &n);
switch (n){
case 1:
printf("请输入姓名:\n");
scanf("%s", p->name);
break;
case 2:
printf("请输入性别:\n");
scanf("%s", p->sex);
break;
case 3:
printf("请输入年龄:\n");
scanf("%d", &(p->age));
break;
case 4:
printf("请输入电话号码:\n");
scanf("%s", p->number);
break;
case 5:
printf("请输入地址:\n");
scanf("%s", p->adress);
break;
default:
printf("你输入的有问题!\n");
}
}
return;
}
printf("I can't find he!\n");
}
void DelPerson(contact_t *ct){
char name[32] = { 0 };
printf("请输入要删除人的姓名:\n");
scanf("%s", name);
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
ct->persons[i] = ct->persons[ct->size];
(ct->size)--;
printf("删除成功!\n");
return;
}
}
printf("要删除的人不存在!\n");
}
void SearchPerson(const contact_t *ct){
char name[32] = {0};
printf("请输入查找人的姓名:\n");
scanf("%s", name);
for (int i = 0; i < ct->size; i++){
if (strcmp(ct->persons[i].name, name) == 0){
printf("| %10s | %4s | %3d | %8s | %8s |\n", ct->persons[i].name, ct->persons[i].sex, \
ct->persons[i].age, ct->persons[i].number, ct->persons[i].adress);
return;
}
}
printf("I can't find he!\n");
}
void ClearContact(contact_t*ct){
printf("Clear...done\n");
ct->size = 0;
}
void PrintContact(contact_t *ct){
for (int i = 0; i < ct->size; i++){
printf("|%-10s|%-4s|%-3d|%8s|%8s|\n", ct->persons[i].name, \
ct->persons[i].sex, ct->persons[i].age, \
ct->persons[i].number, ct->persons[i].adress);
}
}
void initContact(contact_t **ctp){
*ctp = (contact_t*)malloc(sizeof(contact_t)+sizeof(person_t)*DFL_NUM);
if (NULL == *ctp){
perror("malloc");
exit(1);
}
(*ctp)->cap = DFL_NUM;
(*ctp)->size = 0;
printf("default Contact init....done\n");
}
void AddPerson(contact_t **ctp){
person_t *p = (*ctp)->persons;
if (!isFull(*ctp)|| Inc(ctp)){
contact_t *ct = *ctp;
person_t *p = ct->persons+ct->size;
printf("请输入名字: \n");
scanf("%s", p->name);
if (FindPerson(*ctp, p->name) >= 0){
printf("[%s]已经存在了\n",p->name);
return;
}
printf("请输入这个人的性别\n");
scanf("%s", p->sex);
printf("请输入这个人的年龄\n");
scanf("%d", &(p->age));
printf("请输入这个人的电话\n");
scanf("%s", p->number);
printf("请输入你的地址\n");
scanf("%s", p->adress);
(ct->size)++;
}
}
int main()
{
contact_t *ct=NULL ;
initContact(&ct);
int n = 0;
int quit = 0;
while (!quit){
Menu();
scanf("%d", &n);
switch (n)
{
case 1:
AddPerson(&ct);
break;
case 2:
DelPerson(ct);
break;
case 3:
SearchPerson(ct);
break;
case 4:
ModPerson(ct);//bug!!
break;
case 5:
PrintContact(ct);
break;
case 6:
ClearContact(ct);
break;
case 7:
SortContact(ct);
break;
case 0:
quit = 1;
break;
default:
printf("Error , Please Try Again!");
}
}
printf("Bye bye !");
system("pause");
return 0;
}
如果哪里有值得更正和完善的地方欢迎各位看官在评论区指正,我看到一定会尽己所能做到完善。如果觉得还可以,请赏作者一个免费的赞,谢谢你的阅读与指导。