动态通讯录

时间:2022-12-30 17:56:41

头文件

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 1000
#define name_max 20
#define sex_max 5
#define tele_max 12
#define addr_max 30
typedef struct PeoIoFo
{
char name[name_max];
int age;
char sex[sex_max];
char tele[tele_max];
char addr[addr_max];
}Peo;
typedef struct Contact
{
Peo *date;
int size;
int capacity;
}Contact;
enum
{
exit1,
add,
del,
search,
modify,
show,
sort
};
void InitContact(Contact*ps);
void Addcontact(Contact*ps);
void ShowContact(const Contact*ps);
void DelContact(Contact*ps);
void SearchContact(const Contact*ps);
void ModContact(Contact*ps);
void SortContact(Contact*ps);
void DestoryContact(Contact*ps);

实现代码部分

#include"contact.h"
//实现寻找功能
int static searchbyname(Contact*ps, char name[name_max])
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (strcmp(ps->date[i].name, name) == 0)
return i;
}
return -1;
}
//初始化
void InitContact(Contact* ps)
{
ps->date = (Peo*)malloc(MAX*sizeof(Peo));
if (ps->date == NULL)
return;
ps->size = 0;
ps->capacity = MAX;
}
//检查动态内存够不够
int Chackcapacity(Contact* ps)
{
if (ps->size == ps->capacity)
{
Peo* ptr = (Peo*)realloc(ps->date, (ps->capacity + 2) * sizeof(Peo));
if (ptr != NULL)
{
ps->date = ptr;
ps->capacity += 2;
printf("增容成功\n");
return 1;
}
else
{
printf("增容失败\n");
return 0;
}
}
}
//添加用户
void Addcontact(Contact* ps)
{
int j=Chackcapacity(ps);
if(j==1)
{
printf("请输入名字>:");
scanf("%s", ps->date[ps->size].name);
printf("请输入年龄>:");
scanf("%d", &(ps->date[ps->size].age));
printf("请输入性别>:");
scanf("%s", ps->date[ps->size].sex);
printf("请输入电话>:");
scanf("%s", ps->date[ps->size].tele);
printf("请输入地址>:");
scanf("%s", ps->date[ps->size].addr);
ps->size++;
printf("添加成功\a");
}
}
//显示用户
void ShowContact(const Contact* ps)
{
if (ps->size == 0)
printf("通讯录为空");
else
{
int i = 0;
printf("%-20s\t%-4s\t%-5\t%-12s%-20s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < ps->size; i++)
{
printf("%-20s\t%-4d\t%-5\t%-12s%-20s\n",
ps->date[i].name,
ps->date[i].age,
ps->date[i].sex,
ps->date[i].tele,
ps->date[i].addr);
}
}
}
//删除用户
void DelContact(Contact* ps)
{
char name[name_max];
if (ps->size == 0)
printf("通讯录为空");
else
{
printf("请输入删除人>:");
scanf("%s", name);
int j= searchbyname(ps,name);
if (j ==-1)
{
printf("要删除的人不存在");
}
else
{
for (; j < ps->size; j++)
{
ps->date[j] = ps->date[j + 1];
}
ps->size--;
printf("删除成功");
}
}
}
//寻找用户
void SearchContact(const Contact* ps)
{
char name[name_max];
if (ps->size == 0)
printf("通讯录为空");
else
{
printf("请输入寻找人>:");
scanf("%s", name);
int i = searchbyname(ps,name);
if (i ==-1)
printf("寻找人不存在");
else
{
printf("%-20s\t%-4s\t%-5\t%-12s%-20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5\t%-12s%-20s\n",
ps->date[i].name,
ps->date[i].age,
ps->date[i].sex,
ps->date[i].tele,
ps->date[i].addr);
}
}
}
//修改用户
void ModContact(Contact* ps)
{
char name[name_max];
if (ps->size == 0)
printf("通讯录为空");
else
{
printf("请输入修改人>:");
scanf("%s", name);
int pos = searchbyname(ps, name);
if (pos == -1)
printf("修改人不存在");
else
{
printf("请输入名字>:");
scanf("%s", ps->date[pos].name);
printf("请输入年龄>:");
scanf("%d", &(ps->date[pos].age));
printf("请输入性别>:");
scanf("%s", ps->date[pos].sex);
printf("请输入电话>:");
scanf("%s", ps->date[pos].tele);
printf("请输入地址>:");
scanf("%s", ps->date[pos].addr);
}
}
}
//排序用户
int combyname(const void* e1, const void* e2)
{
return strcmp(((Contact*)e1)->date->name, ((Contact*)e2)->date->name);
}
void SortContact(Contact* ps)
{
qsort(ps->date, ps->size, sizeof(ps->date[0]), combyname);
ShowContact(ps);
}
//毁坏通讯录
void DestoryContact(Contact* ps)
{
free(ps->date);
ps->date = NULL;
}

主函数

#include"contact.h"
void mune()
{
printf("*********************************\n");
printf("*****1add 2del************\n");
printf("*****3search 4modify*********\n");
printf("*****5show 6sort***********\n");
printf("***********0exit*****************\n");
}
int main()
{
int input = 0;
Contact con;
InitContact(&con);
do
{
mune();
printf("请选择>:");
scanf("%d", &input);
switch (input)
{
case add:
Addcontact(&con);
break;
case del:
DelContact(&con);
break;
case search:
SearchContact(&con);
break;
case modify:
ModContact(&con);
break;
case show:
ShowContact(&con);
break;
case sort:
SortContact(&con);
break;
case exit1:
DestoryContact(&con);
break;
default:
printf("非法输入");
break;
}
}while (input);
return 0;
}