之前我有一篇博客写的是静态顺序表实现通讯录,但是静态顺序表有一个很大的缺陷就是,如果存满了,就不能在继续存储了。
为了改进之前的版本,本次将实现动态顺序表的通讯录,可以动态开辟空间,不用担心空间存储满的问题。环境和之前的一样,是:VS2008;
此次为了规范,建立的是.c文件
#include
#include
#include
#include
#pragma warning(disable:4996)
typedef struct People{
char _name[20];
char _sex[2];
int _age;
char _number[20];
char _address[100];
}People;
typedef struct PeopleBook
{
People* _a;
size_t _size; // 有效数据个数
size_t _capacity; // 容量
}PeopleBook;
void Init(PeopleBook* PB);//初始化
void CheckCapacity(PeopleBook* PB);//检查容量是否足够,不够就增容
void AddPeople(PeopleBook* PB);//添加联系人
void DeletePeople(PeopleBook* PB);//删除联系人
void FindPeople(PeopleBook* PB);//查找联系人并返回联系人信息
void ChangePeople(PeopleBook* PB);//修改联系人信息
void PrintPeopleBook(PeopleBook* PB);//打印所有联系人信息
void DestroyPeople(PeopleBook* PB);//销毁所有联系人信息
void Swap(People* a,People* b);//交换两个联系人信息,
void SortPeople(PeopleBook* PB);//排序所有联系人
void GUI();//显示界面
void Init(PeopleBook* PB){
PB->_a = NULL;
PB->_capacity = 0;
PB->_size = 0;
}
void CheckCapacity(PeopleBook* PB){
People* pTemp;
size_t newSize;
if(PB->_capacity == PB->_size){
newSize = (PB->_capacity)*2+3;
pTemp = (People*)realloc(PB->_a,sizeof(People)*newSize);
if (pTemp)
{
PB->_a = pTemp;
PB->_capacity = newSize;
}
else{
perror("realloc is failed!\n");
}
}
}
void AddPeople(PeopleBook* PB){
assert(PB);
CheckCapacity(PB);
printf("Please input people's name:\n");
scanf("%s",PB->_a[PB->_size]._name);
printf("Please input people's sex:\n");
scanf("%s",PB->_a[PB->_size]._sex);
printf("Please input people's age:\n");
scanf("%d",&(PB->_a[PB->_size]._age));
printf("Please input people's number:\n");
scanf("%s",PB->_a[PB->_size]._number);
printf("Please input people's address:\n");
scanf("%s",PB->_a[PB->_size]._address);
printf("Add people success!\n");
++PB->_size;
}
void DeletePeople(PeopleBook* PB){
char tempname[20];
size_t temp = 0;
size_t i =0;
assert(PB);
printf("Please input the people you want to delete:\n");
scanf("%s",tempname);
for (;i_size;i++)
{
if (strcmp(tempname,PB->_a[i]._name) == 0)
{
printf("The %d People is:\n",i+1);
printf("name:%s\n",PB->_a[i]._name);
printf("sex:%s\n",PB->_a[i]._sex);
printf("age:%d\n",PB->_a[i]._age);
printf("number:%s\n",PB->_a[i]._number);
printf("address:%s\n",PB->_a[i]._address);
}
}
printf("Please input whitch people you want to delete:\n");
scanf("%d",&temp);
i = temp-1;
for (;i_size-1;i++)
{
strcpy(PB->_a[i]._name,PB->_a[i+1]._name);
strcpy(PB->_a[i]._sex,PB->_a[i+1]._sex);
PB->_a[i]._age,PB->_a[i+1]._age;
strcpy(PB->_a[i]._number,PB->_a[i+1]._number);
strcpy(PB->_a[i]._address,PB->_a[i+1]._address);
}
--PB->_size;
printf("Delete success!\n");
}
void FindPeople(PeopleBook* PB){
char tempname[20];
size_t i = 0;
int flag = 0;
assert(PB);
printf("Please input people's name who you are finding:\n");
scanf("%s",tempname);
printf("The result is:\n");
for (;i_size;i++)
{
if(strcmp(tempname,PB->_a[i]._name)==0){
//printf("The %d People is:\n",i+1);
printf("name:%s\n",PB->_a[i]._name);
printf("sex:%s\n",PB->_a[i]._sex);
printf("age:%d\n",PB->_a[i]._age);
printf("number:%s\n",PB->_a[i]._number);
printf("address:%s\n",PB->_a[i]._address);
flag = 1;
}
}
if(flag == 0){
printf("Empty!\n");
}
}
void ChangePeople(PeopleBook* PB){
size_t i = 0 ;
char tempname[20];
int temp = 0;
assert(PB);
printf("Please input whitch people you want to change?\n");
scanf("%s",tempname);
for (;i_size;i++)
{
if(strcmp(tempname,PB->_a[i]._name)==0){
printf("The %d People is:\n",i+1);
printf("name:%s\n",PB->_a[i]._name);
printf("sex:%s\n",PB->_a[i]._sex);
printf("age:%d\n",PB->_a[i]._age);
printf("number:%s\n",PB->_a[i]._number);
printf("address:%s\n",PB->_a[i]._address);
}
}
printf("Please input the number:\n");
scanf("%d",&temp);
printf("Please input people's new name:\n");
scanf("%s",PB->_a[temp-1]._name);
printf("Please input people's new sex:\n");
scanf("%s",PB->_a[temp-1]._sex);
printf("Please input people's new age:\n");
scanf("%d",&(PB->_a[temp-1]._age));
printf("Please input people's new number:\n");
scanf("%s",PB->_a[temp-1]._number);
printf("Please input people's new address:\n");
scanf("%s",PB->_a[temp-1]._address);
printf("Change successful!\n");
}
void PrintPeopleBook(PeopleBook* PB){
size_t i = 0;
assert(PB);
if(PB->_size == 0){
printf("People is empty!\n");
return;
}
else{
for (;i_size;i++)
{
printf("The %d People is:\n",i+1);
printf("name:%s\n",PB->_a[i]._name);
printf("sex:%s\n",PB->_a[i]._sex);
printf("age:%d\n",PB->_a[i]._age);
printf("number:%s\n",PB->_a[i]._number);
printf("address:%s\n",PB->_a[i]._address);
}
}
}
void DestroyPeople(PeopleBook* PB){
assert(PB);
free(PB->_a);
PB->_size = 0;
printf("Destroy people is successed!\n");
};
void Swap(People* a,People* b){
People temp;
temp = *a;
*a = *b;
*b = temp;
}
void SortPeople(PeopleBook* PB){
size_t begin;
size_t finish = PB->_size;
int flag = 0;
assert(PB);
while (finish>1)
{
begin = 1;
while (begin_a[begin]._name,PB->_a[begin-1]._name)<0){
Swap(&(PB->_a[begin]),&(PB->_a[begin-1]));
flag = 1;
}
begin++;
}
if(flag == 0){
return;
}
finish--;
}
}
void GUI(){
printf("***************************************************\n");
printf("* 1、添加联系人 2、删除联系人 *\n");
printf("* 3、查找联系人 4、修改联系人 *\n");
printf("* 5、显示联系人 6、清空联系人 *\n");
printf("* 7、排序联系人 0、退出 *\n");
printf("***************************************************\n");
}
void test(){
int select = 0;
PeopleBook ConnactBook;
GUI();
Init(&ConnactBook);
while (select!= EOF)
{
printf("please input your select:\n");
scanf("%d",&select);
switch(select){
case 1:
AddPeople(&ConnactBook);
GUI();
break;
case 2:
DeletePeople(&ConnactBook);
GUI();
break;
case 3:
FindPeople(&ConnactBook);
GUI();
break;
case 4:
ChangePeople(&ConnactBook);
GUI();
break;
case 5:
PrintPeopleBook(&ConnactBook);
GUI();
break;
case 6:
DestroyPeople(&ConnactBook);
GUI();
break;
case 7:
SortPeople(&ConnactBook);
GUI();
break;
case 0:
printf("Thanks For Use~! Success To Exit!\n");
return ;
break;
default:
printf("Error input,please input again!\n");
}
}
}
int main(){
test();
return 0;
}
限于编者水平,此代码有很多不足之处,欢迎各位来批评指正。