说明1
通讯录的实现依赖于顺序表的实现,顺序表里面的的数据类型各种各样,可以是整形,字符型,还可以是浮点型去去,但是只要把里面的数据类型用一个结构体来代替,就可以实现通讯录项目
说明2
1.实现的过程中可能会出现头文件交叉包含的问题,这是可以使用前置声明,避免的头文件的交叉包含问题,
int add(int x,int y);//函数的前置声明
int main()
{
return 0;
}
int add(int x,int y)
{
//函数的实现
}
通讯录的实现
SeqList.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
#include"string.h"
typedef Info SLtypedat;
typedef struct SeqList
{
SLtypedat* arr;
int capcity;//容量
int size;//有效个数
}SL;
void SLinit(SL* ps);//初始化顺序表
void checkSL(SL* ps);//检查是否增加容量
void SLprint(SL* ps);
void SLpushfront(SL* ps, SLtypedat x);//顺序表的头插
void SLpushback(SL* ps, SLtypedat x);//顺序包的u尾部删除
void SLpopback(SL* ps);//顺序表的尾部删除
void SLpopfront(SL* ps);//顺序表的头部删除
void SLinsert(SL* ps, int pos, SLtypedat x);//特定位置的插入
void SLerase(SL* ps, int pos);//删除指定位置的数据
void SLdestory(SL* ps);//数据的修改
SeqList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void SLinit(SL* ps)
{
ps->arr = NULL;
ps->capcity = ps->size = 0;
}
void checkSL(SL* ps)
{
if (ps->size == ps->capcity)
{
int newcapticy = (ps->capcity ? 0 : 2 *ps->capcity);
SLtypedat* temp = (SLtypedat*)realloc(ps->arr, newcapticy * sizeof(SLtypedat));
if (temp == NULL)
{
perror("temp");
exit(1);
}
ps->arr = temp;
ps->capcity = newcapticy;
}
}
void SLpushfront(SL* ps, SLtypedat x)
{
assert(ps != NULL);
checkSL(ps);
for (int i = ps->size-1; i >= 0; i--)
{
ps->arr[i + 1] = ps->arr[i];
}
ps->arr[0] = x;
ps->size++;
}
void SLprint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->arr[i]);
}
}
void SLpushback(SL* ps, SLtypedat x)
{
assert(ps);
checkSL(ps);
ps->arr[ps->size++] = x;
}
void SLpopback(SL* ps)
{
assert(ps);
ps->size--;
}
void SLpopfront(SL* ps)
{
assert(ps);
for (int i = 0; i < ps->size-2; i++)
{
ps->arr[i] = ps->arr[i+1];
}
ps->size--;
}
void SLinsert(SL* ps, int pos, SLtypedat x)
{
assert(ps);
assert((pos >= 0) && (pos < ps->size));
checkSL(ps);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1];
}
ps->arr[pos] = x;
ps->size++;
}
void SLerase(SL* ps, int pos)
{
assert(ps);
for (int i = pos; i<=ps->size-2; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
void SLdestory(SL* ps)
{
assert(ps);
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capcity = 0;
}
Contact.h
#pragma once
#define name_max 20
#define tel_max 12
#define addr_max 30
#define gender_max 2
typedef struct PersonInfo
{
char name[name_max];
int age;
char gender[gender_max];
char tel[tel_max];
char addr[addr_max];
}Info;
struct SeqList;//前置声明,避免头文件的交叉包含
typedef struct SeqList Contact;
void Contactinit(Contact* pcon);//通讯录的初始化
void Contactdestory(Contact* pcon);//通讯录的销毁
void Contactadd(Contact* pcon);//添加联系人
void Contactprint(Contact* pcon);//查看通讯录
int ConFindbyname(Contact* pcon);//查找联系人的下标
void Contactdel(Contact* pcon);//联系人的删除
void Contactfind(Contact* pcon);//查找联系人
void Contactmodefy(Contact* pcon);//联系人的修改
Contact.c
#include"SeqList.h"
void Contactinit(Contact* pcon)
{
SLinit(pcon);
}
void Contactdestory(Contact* pcon)
{
SLdestory(pcon);
}
void Contactadd(Contact* pcon)
{
Info info;
printf("请输入要添加的联系人姓名\n");
scanf("%s", info.name);
printf("请输入联系人的年龄\n");
scanf("%d", &info.age);
printf("请输入联系人的性别\n");
scanf("%s", info.gender);
printf("请输入联系人的电话号码\n");
scanf("%s", info.tel);
printf("请输入联系人的家庭住址\n");
scanf("%s", info.addr);
SLpushback(pcon,info);
}
void Contactprint(Contact* pcon)
{
printf("%s %s %s %s %s\n", "姓名", "年龄", "性别", "电话", "住址");
for (int i = 0; i < pcon->size; i++)
{
printf("%s %d %s %s %s\n",pcon->arr[i].name,
pcon->arr[i].age,
pcon->arr[i].gender,
pcon->arr[i].tel,
pcon->arr[i].addr);
}
}
int ConFindbyname(Contact* pcon)
{
char name[name_max];
printf("请输入联系人姓名\n");
scanf("%s", name);
for (int i = 0; i < pcon->size; i++)
{
if (strcmp(name, pcon->arr[i].name) == 0)
{
return i;
}
}
return -1;
}
void Contactdel(Contact* pcon)
{
int ret = ConFindbyname(pcon);
if (ret < 0)
{
printf("您要删除的联系人不存在\n");
}
else
{
SLerase(pcon, ret);
}
}
void Contactfind(Contact* pcon)
{
int ret = ConFindbyname(pcon);
if (ret < 0)
{
printf("您要查找的联系人不存在\n");
}
else
{
printf("%s %d %s %s %s\n", pcon->arr[ret].name,
pcon->arr[ret].age,
pcon->arr[ret].gender,
pcon->arr[ret].tel,
pcon->arr[ret].addr);
}
}
void Contactmodefy(Contact* pcon)
{
int ret = ConFindbyname(pcon);
if (ret < 0)
{
printf("您要修改的联系人不存在\n");
}
else
{
printf("请输入新的姓名\n");
scanf("%s", pcon->arr[ret].name);
printf("请输入新的年龄\n");
scanf("%d", &pcon->arr[ret].age);
printf("请输入新的性别\n");
scanf("%s", pcon->arr[ret].gender);
printf("请输入新的电话\n");
scanf("%s", pcon->arr[ret].tel);
printf("请输入新的家庭住址\n");
scanf("%s", pcon->arr[ret].addr);
}
}
Contacttest.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
void menu()
{
printf("***************通讯表**************\n");
printf("***** 1.添加联系人 2.删除联系人****\n");
printf("***** 3.查找联系人 4.修改联系人****\n");
printf("***** 5.查看通讯表 0.退出通讯表****\n");
printf("***********************************\n");
}
int main()
{
Contact con;
Contactinit(&con);
int select = 0;
do
{
menu();
printf("请输入你的选择\n");
scanf("%d", &select);
switch(select)
{
case 1://添加联系人
Contactadd(&con);
break;
case 2://删除联系人
Contactdel(&con);
break;
case 3://查找联系人
Contactfind(&con);
break;
case 4://修改联系人
Contactmodefy(&con);
break;
case 5://查看通讯表
Contactprint(&con);
break;
case 0://退出通讯表
printf("退出通讯表中.....\n");
break;
default:
break;
}
} while (select!=0);
Contactdestory(&con);
return 0;
}