// ********************************** 通讯录.h 文件 *********************************
#pragma once
#define MAX_name 20
#define MAX_sex 5
#define MAX_tele 15
#define MAX_addr 30
#define DEFAULT_SZ 3
struct PeoInfo
{
char name[MAX_name];
int age;
char sex[MAX_sex];
char tele[MAX_tele];
char addr[MAX_addr];
};
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
struct Contact // 通讯录的类型
{
struct PeoInfo* data;
int size; // 记录当前已经有的元素个数
int capacity;// 当前data里的最大容量
};
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);
void DestroyContact(struct Contact* ps);
// ************************** 通讯录.cpp **************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include"通讯录.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<search.h>
void InitContact(struct Contact* ps)
{
//memset(ps->data, 0, sizeof(ps->data));
//ps->size = 0; //初始化通讯录 全部为 0
ps->data = (struct PeoInfo*)malloc(3 * sizeof(struct PeoInfo));
if (ps->data == NULL)
{
return;
}
ps->size = 0;
ps->capacity = DEFAULT_SZ;
}
void CheckCapacity(struct Contact* ps)
{
if (ps->size == ps->capacity)
{
//增容
struct PeoInfo* ptr=(struct PeoInfo*)realloc(ps->data, (ps->capacity + 2) * sizeof(struct PeoInfo));
if (ptr != NULL)
{
ps->data = ptr;
ps->capacity += 2;
printf("增容成功!\n");
}
else
printf("增容失败!\n");
}
}
void AddContact(struct Contact* ps)
{
//检测当前空间是否满了:如果满了,扩容;如果不满,退出,进行下一步
CheckCapacity(ps);
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");
}
// ******** 使用qsort 对通讯录排序******
int cmp_by_name(const void* e1, const void* e2)
{
//return strcmp(((struct Contact*)e1)->data.name, ((struct Contact*)e2)->data.name);
return strcmp(((struct Contact*)e1)->data->name, (((struct Contact*)e2)->data->name));
}
void SortContact(struct Contact* ps)
{
qsort(ps->data, ps->size, sizeof(ps->data[0]), cmp_by_name);
}
void DestroyContact(struct Contact* ps)
{
free(ps->data);
ps->data = NULL;
}
void ShowContact(const struct Contact* ps)
{
if (ps->size == 0)
{
printf("通讯录为空!\n");
}
else
{
int i = 0;
printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < ps->size; i++)
{
printf("%20s\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)
{
int i = 0;
for (i = 0; i < ps->size ; i++)
{
if (strcmp(ps->data[i].name, name) == 0)
return i;
}
return -1; //找不到
}
void DelContact(struct Contact* ps)
{
char name[MAX_name] = { 0 };
printf("请输入要删除人的名字:");
scanf("%s", name);
int pos=Findbyname(ps,name);
if (pos == -1)
{
printf("要删除的人不存在!\n");
}
else
{
int i = 0;
for (i = pos; i < ps->size-1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->size--;
printf("删除成功!\n");
}
}
void SearchContact(const struct Contact* ps)
{
char name[MAX_name] = { 0 };
printf("请输入要查找人的名字:");
scanf("%s", name);
int pos = Findbyname(ps, name);
if (pos == -1)
{
printf("要查找人的信息不存在!");
}
else
{
printf("%20s\t%4s\t%5s\t%12s\t%20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%20s\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] = { 0 };
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");
}
}
//**************************** test.cpp **********************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include"通讯录.h"
void menu()
{
printf("***************************************************\n");
printf("******* 1.ADD 2.DEL ************\n");
printf("******* 3.SEARCH 4.MODIFY ***********\n");
printf("******* 5.SHOW 6.SORT ************\n");
printf("*************** 0.EXIT ************************\n");
printf("***************************************************\n");
}
int main()
{
int input = 0;
struct Contact con; //con 就是通讯录,其中包含了data 指针、size和capacity
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 SHOW:
ShowContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
//销毁通讯录------释放动态内存开辟的内存
DestroyContact(&con);
printf("退出通讯录!");
break;
default:
printf("输入错误,请重新输入:");
break;
}
} while (input);
return 0;
}
本人小白,刚学习C语言,近期学习了动态内存分配,看着视频写了这个动态内存版本的通讯录。现在这个通讯录其他功能都可以实现,就是这个排序功能现在实现不了了。之前struct Contact con中创建的是 struct PeoInfo data[1000] ,使用这个结构体数组的话,排序功能可以跑起来,但是现在改成struct PeoInfo* data 后,排序功能不能正常使用。求大佬,帮我解答下。