通讯录的实现
一.头文件
1.1顺序表头文件SeqList.h
#pragma once#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>#include "Contact.h"typedef peoInfo SLDataType;typedef struct SeqList
{SLDataType* arr;int size;int capacity;
}SL;//初始化
void SLInit(SL* ps);//销毁(空间释放)
void SLDestroy(SL* ps);//打印
void SLPrint(SL s);//判断空间
void SLCheckCapacity(SL* ps);//尾插
void SLPushBack(SL* ps, SLDataType x);//头插void SLPushFront(SL* ps, SLDataType x);//尾删void SLPopBack(SL* ps);//头删void SLPopFront(SL* ps);//指定位置之前插入void SLInsert(SL* ps, int pos, SLDataType x);//删除数据void SLErase(SL* ps, int pos);//顺序表的查找int SLFind(SL* ps, SLDataType x);
1.2通讯录头文件Contact.h
#pragma once#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 20typedef struct personinfo
{char name[NAME_MAX];char gender[GENDER_MAX];int age;char tel[TEL_MAX];char addr[ADDR_MAX];}peoInfo;typedef struct SeqList Contact;//初始化
void ContactInit(Contact* con);//销毁
void ContactDestroy(Contact* con);//添加数据
void ContactAdd(Contact* con);//删除数据
void ContactDel(Contact* con);//修改
void ContactModify(Contact* con);//查找
void ContactFind(Contact* con);//展示
void ContactShow(Contact* con);
二.源文件
2.1顺序表源文件SeqList.c
#include"SeqList.h"//初始化
void SLInit(SL* ps)
{ps->arr = NULL;ps->size = ps->capacity = 0;
}//销毁
void SLDestroy(SL* ps)
{if (ps->arr){free(ps->arr);}ps->arr = NULL;ps->size = ps->capacity = 0;
}//打印void SLPrint(SL s)
{for (int i = 0; i < s.size; i++){printf("%d ", s.arr[i]);}printf("\n");
}//判断空间
void SLCheckCapacity(SL* ps)
{if (ps->capacity == ps->size){int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));if (tmp == NULL){perror("realloc");exit(1);}ps->capacity = newCapacity;ps->arr = tmp;}}//尾插
void SLPushBack(SL* ps, SLDataType x)
{//assert断言assert(ps);//判断空间是否足够?SLCheckCapacity(ps);ps->arr[ps->size++] = x;}//头插void SLPushFront(SL* ps, SLDataType x)
{assert(ps);SLCheckCapacity(ps);for (int i = ps->size; i >0 ; i--){ps->arr[i] = ps->arr[i - 1];}ps->arr[0] = x;ps->size++;
}//尾删
void SLPopBack(SL* ps)
{assert(ps);assert(ps->size);ps->size--;}//头删
void SLPopFront(SL* ps)
{assert(ps);assert(ps->size);for (int i = 1;i<ps->size;i++){ps->arr[i-1] = ps->arr[i];}ps->size--;}//在指定位置前插入
void SLInsert(SL* ps, int pos, SLDataType x)
{assert(ps);assert(pos >= 0 && pos <= ps->size);SLCheckCapacity(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);assert(pos >= 0 && pos < ps->size);for (int i = pos;i<ps->size-1;i++){ps->arr[i] = ps->arr[i + 1];}ps->size--;}
2.2通讯录源文件Contact.c
#include"Contact.h"
#include"SeqList.h"void ContactInit(Contact* con)
{SLInit(con);
}void ContactDestroy(Contact* con)
{SLDestroy(con);
}void ContactAdd(Contact* con)
{peoInfo info;printf("请输入要添加的联系人姓名\n");scanf("%s", info.name);printf("请输入要添加的联系人性别\n");scanf("%s", info.gender);printf("请输入要添加的联系人年龄\n");scanf("%d", &info.age);printf("请输入要添加的联系人电话\n");scanf("%s", info.tel);printf("请输入要添加的联系人地址\n");scanf("%s", info.addr);SLPushBack(con,info);}int FindByName(Contact* con, char name[])
{for (int i = 0; i < con->size; i++){if (strcmp(con->arr[i].name, name) == 0){return i;}}return -1;
}//删除数据
void ContactDel(Contact* con)
{char name[NAME_MAX];printf("请输入要删除的联系人姓名\n");scanf("%s",name);int Find = FindByName(con, name);if (Find < 0){printf("没找到要删除的联系人\n");return;}//找到了SLErase(con,Find);printf("删除成功\n");}void ContactShow(Contact* con)
{printf("%s %s %s %s %s\n", "姓名", "性别","年龄", "电话", "住址");for (int i = 0; i < con->size; i++){printf("%s %s %d %s %s\n", con->arr[i].name, con->arr[i].gender, con->arr[i].age, con->arr[i].tel, con->arr[i].addr);}}//修改
void ContactModify(Contact* con)
{char name[NAME_MAX];printf("请输入要修改的联系人姓名\n");scanf("%s", name);int Find = FindByName(con, name);if (Find < 0){printf("要修改的联系人不存在\n");return;}//说明存在printf("请输入新的联系人姓名\n");scanf("%s", con->arr[Find].name);printf("请输入新的联系人性别\n");scanf("%s", con->arr[Find].gender);printf("请输入新的联系人年龄\n");scanf("%d", &con->arr[Find].age);printf("请输入新的联系人电话\n");scanf("%s", con->arr[Find].tel);printf("请输入新的联系人地址\n");scanf("%s", con->arr[Find].addr);printf("修改成功\n");
}//查找
void ContactFind(Contact* con)
{char name[NAME_MAX];printf("请输入要查找的联系人姓名\n");scanf("%s", name);int Find = FindByName(con, name);if (Find < 0){printf("要查找的联系人不存在\n");return;}//说明存在printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%s %s %d %s %s\n", con->arr[Find].name, con->arr[Find].gender, con->arr[Find].age, con->arr[Find].tel, con->arr[Find].addr);}
三.测试文件
#include"SeqList.h"
void menu()
{printf("***************通讯录****************\n");printf("******1.增加联系人 2.删除联系人******\n");printf("******3.修改联系人 4.查找联系人******\n");printf("******5.展示联系人 0.退出 *******\n");printf("*************************************\n");
}int main()
{int input = 0;Contact con;ContactInit(&con);do{menu();printf("请输入\n");scanf("%d", &input);switch (input){case 1:ContactAdd(&con);ContactAdd(&con);break;case 2:ContactDel(&con);break;case 3:ContactModify(&con);break;case 4:ContactFind(&con);break;case 5:ContactShow(&con);break;case 0:printf("退出通讯录\n");break;default:printf("输入错误\n");break;}printf("------------------------------------------------------\n");} while (input);ContactDestroy(&con);return 0;
}
四.运行结果
运行结果大家可自行尝试,下面只是部分截图。
五.解释
为什么会写顺序表的头文件 源文件?
通讯录实际上就是个顺序表。需要用到顺序表相关方法,对通讯录的操作,实际上就是对顺序表进行操作。顺序表中对于“增加 删除 修改 查找......”等方法都已经提供好了,通讯录可直接使用。
(1007.doc)