#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
//#define 叫做宏定义命令,它也是C语言预处理命令的一种。所谓宏定义,就是用一个标识符来表示一个字符
//串,如果在后面的代码中出现了该标识符,那么就全部替换成指定的字符串。使修改更方便
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define DEFAULT_SZ 3
enum Option //枚举常量增加代码的可读性
{
Exti,
ADD,
DEL,
SEARCH,
REMAKE,
MODIFY,
SORT,
PRINT
};
//通讯录人的基本信息
typedef struct Peoinfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tele[TELE_MAX];
char addr[ADDR_MAX];
}Peoinfo;
//通讯录的基本信息
typedef struct Contact
{
Peoinfo* people;//人
int sz;//记录人数
int capacity;//人数扩容
}Contact;
void menu()
{
printf("------------------------------\n");
printf("------------------------------\n");
printf("-------1.ADD 2.DEL------\n");
printf("-------3.SEARCH 4.REMAKE---\n");
printf("-------5.MODIFY 6.SORT-----\n");
printf("-------7.PRINT 0.Exti-----\n");
printf("------------------------------\n");
printf("------------------------------\n");
}
//初始化通讯录
void inContact(Contact* pc)
{
assert(pc);
pc->sz = 0;
pc->capacity = DEFAULT_SZ;
pc->people = (Peoinfo*)malloc(pc->capacity * sizeof(Peoinfo));
if (pc->people == NULL)
{
perror("initContact::malloc");
return;
}
memset(pc->people, 0, pc->capacity * sizeof(Peoinfo));
}
//是否需要扩容
void checkCapacity(Contact* pc)
{
//动态版本
if (pc->sz == pc->capacity)
{
Peoinfo* tmp = (Peoinfo*)realloc(pc->people, (pc->capacity + 2) * sizeof(Peoinfo));
if (tmp != NULL)
{
pc->people = tmp;
}
pc->capacity += 2;
printf("增容成功!\n");
}
}
//添加联系人
void addContact(Contact* pc)
{
assert(pc);
//静态版本
/*if (pc->sz == MAX)
{
printf("通讯录人已满\n");
return;
}*/
checkCapacity(pc);
printf("请输入新联系人名字:");
scanf("%s", pc->people[pc->sz].name);
printf("请输入新联系人性别:");
scanf("%s", pc->people[pc->sz].sex);
printf("请输入新联系人年龄:");
scanf("%d", &(pc->people[pc->sz].age));
printf("请输入新联系人电话:");
scanf("%s", pc->people[pc->sz].tele);
printf("请输入新联系人地址:");
scanf("%s", pc->people[pc->sz].addr);
pc->sz++;
printf("加入成功!\n");
}
//查找联系人
int FindByName(const Contact* pc, char name[])
{
assert(pc);
for (int i = 0; i < pc->sz; i++)
{
if (0 == strcmp(pc->people[i].name, name))
{
return i;
}
}
return -1;
}
//删除联系人
void delContact(Contact* pc)
{
assert(pc);
if (pc->sz == 0)
{
printf("通讯录人已空,无法执行\n");
return;
}
char name[NAME_MAX] = { 0 };
printf("请输入姓名:");
scanf("%s", name);
int pow = FindByName(pc, name);
if (-1 == FindByName(pc, name))
{
printf("未找到该联系人\n");
return;
}
for (int i = pow; i < pc->sz - 1; i++)
{
pc->people[i] = pc->people[i + 1];
}
pc->sz--;
printf("删除成功!\n");
}
//查找联系人并打印信息
void searchContact(const Contact* pc)
{
assert(pc);
if (pc->sz == 0)
{
printf("通讯录人已空,无法执行\n");
return;
}
char name[NAME_MAX] = { 0 };
printf("请输入姓名:");
scanf("%s", name);
int pow = FindByName(pc, name);
if (-1 == FindByName(pc, name))
{
printf("未找到该联系人\n");
return;
}
printf("该联系人为:");
printf("%-20s %-5s %-5d %-12s %-30s\n",
pc->people[pow].name,
pc->people[pow].sex,
pc->people[pow].age,
pc->people[pow].tele,
pc->people[pow].addr);
}
//修改联系人
void remakeContact(Contact* pc)
{
assert(pc);
if (pc->sz == 0)
{
printf("通讯录人已空,无法执行\n");
return;
}
char name[NAME_MAX] = { 0 };
printf("请输入姓名:");
scanf("%s", name);
int pow = FindByName(pc, name);
if (-1 == FindByName(pc, name))
{
printf("未找到该联系人\n");
return;
}
printf("请输入新联系人名字:");
scanf("%s", pc->people[pow].name);
printf("请输入新联系人性别:");
scanf("%s", pc->people[pow].sex);
printf("请输入新联系人年龄:");
scanf("%d", &(pc->people[pow].age));
printf("请输入新联系人电话:");
scanf("%s", pc->people[pow].tele);
printf("请输入新联系人地址:");
scanf("%s", pc->people[pow].addr);
printf("修改成功!\n");
}
//以名字排序所有联系人
void modifyContact(Contact* pc)
{
assert(pc);
Peoinfo tmp;
for (int i = 0; i < pc->sz - 1; i++)
{
for (int j = 0; j < pc->sz - 1 - i; j++)
{
if (strcmp(pc->people[j].name, pc->people[j + 1].name) > 0)
{
tmp = pc->people[j];
pc->people[j] = pc->people[j + 1];
pc->people[j + 1] = tmp;
}
}
}
printf("调整成功!\n");
}
//清空所有联系人
void sortContact(Contact* pc)
{
assert(pc);
pc->sz = 0;
memset(pc->people, 0, sizeof(pc->people));
printf("清空成功!\n");
}
//打印联系人
void printContact(Contact* pc)
{
assert(pc);
if (pc->sz == 0)
{
printf("暂无联系人,快去添加好友吧!\n");
return;
}
printf("%-20s %-5s %-5s %-12s %-30s\n",
"姓名",
"性别",
"年龄",
"号码",
"地址");
for (int i = 0; i < pc->sz; i++)
{
printf("%-20s %-5s %-5d %-12s %-30s\n",
pc->people[i].name,
pc->people[i].sex,
pc->people[i].age,
pc->people[i].tele,
pc->people[i].addr);
}
printf("共%d位联系人\n", pc->sz);
}
void test()
{
int input = 0;
Contact con;
inContact(&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 REMAKE:
remakeContact(&con);
break;
case MODIFY:
modifyContact(&con);
break;
case SORT:
sortContact(&con);
break;
case PRINT:
printContact(&con);
break;
case Exti:
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
通讯录(动态版)
最新推荐文章于 2024-10-08 21:10:10 发布