目录
目录
1. 功能介绍
该项目用C语言实现了一个字符界面的通讯录,在终端窗口运行,功能包括对联系人信息的增添,查找(按照姓名),修改,删除,展示,排序(按照年龄)的功能;
该代码的内存为动态分配,会随着输入数据的增多而动态分配,不用担心造成内存浪费的情况;
在结束进程时,程序还能保存联系人数据到本地文件(以二进制形式保存),并在下一次打开时从本地读取到程序内存中。
2. 代码部分
2.1 main函数 - 'test.c'
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
int main()
{
AddressBook con;
int input = 0;
InitialAddressBook(&con);
do
{
system("cls");
display();
printf("Please input function choice:>");
scanf("%d", &input);
switch (input)
{
case ADD:
addInfo(&con);
break;
case DEL:
delInfo(&con);
break;
case SEARCH:
searchInfo(&con);
break;
case MODIFY:
modifyInfo(&con);
break;
case SHOW:
showInfo(&con);
break;
case SORT:
sortByAgeInfo(&con);
break;
case EXIT:
saveData(&con);
freeMemory(&con);
break;
default:
printf("Invalid input! Please choose again!");
Sleep(1500);
break;
}
} while (input);
}
2.2 结构体定义、函数声明 - 'contact.h'
#define _CRT_SECURE_NO_WARNINGS 1
#define DEFAULT_CAPACITY 3
#define INC_SIZE 2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
struct PeoInfo
{
char name[10];
int age;
char gender[6];
char number[12];
};
typedef struct PeoInfo PeoInfo;
struct AddressBook
{
PeoInfo* data;
int size;
int capacity;
};
typedef struct AddressBook AddressBook;
enum CHOOSE
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
int InitialAddressBook(AddressBook* p); //Initialize the address book
void display(); //dispaly the lobby for user to choose
void addInfo(AddressBook* p); //add information to address book
void delInfo(AddressBook* p); //delete information from address book
void searchInfo(AddressBook* p); //serch information storaged in the address book
void modifyInfo(AddressBook* p); //modify information storaged int the address book
void showInfo(AddressBook* p); //show all information storaged int the address book
void sortByAgeInfo(AddressBook* p); //sort the information storaged in the address book by age
void saveData(AddressBook* p); //save data to BIN doucument;
void freeMemory(AddressBook* p); //free the memory
2.3 函数定义 - 'contact.c'
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void display()
{
printf("=======================================\n");
printf("******BuiDer's AddressBook System******\n");
printf("=======================================\n");
printf("*******1.add 2.del*******\n");
printf("*******3.search 4.modify****\n");
printf("*******5.show 6.sort******\n");
printf("*************** 0.exit ****************\n");
printf("=======================================\n");
}
int check_capacity(AddressBook* p) //support:InitialAddressBook -- check the capacity is enough or not. If not, increase the capacity
{
if (p->size == p->capacity)
{
PeoInfo* temp = (PeoInfo*)realloc(p->data, (p->capacity + INC_SIZE) * sizeof(PeoInfo));
if (temp == NULL)
{
perror("check_capacity:realloc");
return 0;
}
else
{
p->data = temp;
p->capacity += INC_SIZE;
return 1;
}
}
}
int LoadData(AddressBook* p) //support:InitialAddressBook -- Load Data form BIN document;
{
FILE* pf = fopen("data.dat", "rb");
if (pf == NULL)
{
perror("LoadData:fopen");
return 1;
}
PeoInfo temp = { 0 };
while (fread(&temp, sizeof(PeoInfo), 1, pf))
{
check_capacity(p);
p->data[p->size] = temp;
p->size++;
}
fclose(pf);
pf = NULL;
return;
}
int InitialAddressBook(AddressBook* p)
{
p->data = (PeoInfo*)malloc(sizeof(PeoInfo) * DEFAULT_CAPACITY);
if (p->data == NULL)
{
perror("Initial:malloc");
return 1;
}
p->size = 0;
p->capacity = 0;
LoadData(p);
return;
}
void addInfo(AddressBook* p)
{
check_capacity(p);
printf("Please input a name for the person you wanna add:>\n");
scanf("%s", p->data[p->size].name);
printf("Please input the age of this person:>\n");
scanf("%d", &(p->data[p->size].age));
printf("Please input the gender of this person:>\n");
scanf("%s", p->data[p->size].gender);
printf("Please input the phone number of this person:>\n");
scanf("%s", p->data[p->size].number);
p->size++;
printf("Added Sucessfully!\n");
Sleep(500);
}
int FindByName(AddressBook* p, char name[]) //support:searchInfo & delInfo -- find information by name,is existed, return location; if not, return -1
{
for (int i = 0; i < p->size; i++)
{
if (!strcmp(p->data[i].name, name))
{
return i;
break;
}
}
return -1;
}
void searchInfo(AddressBook* p)
{
char* Tname[10];
printf("Please input the name you wanna serach:>\n");
scanf("%s", Tname);
int i = FindByName(p, Tname);
if (i != -1)
{
printf("\n\n================================================\n");
printf("%-10s\t%-4s\t%-5s\t%-12s\n", "Name", "Age", "Gender", "Phone Number");
printf("================================================\n");
printf("%-10s\t%-4d\t%-5s\t%-12s\n\n\n\n", p->data[i].name, p->data[i].age,
p->data[i].gender, p->data[i].number);
printf("Press 'Enter' to continue:>");
getchar();
getchar();
return;
}
else
{
printf("The person is not exsited in this address book.\n");
Sleep(1000);
}
printf("Not Founded :(\n");
}
void delInfo(AddressBook* p)
{
if (p->size == 0)
{
printf("The address book is empty!\n");
Sleep(500);
return;
}
char* Tname[10];
printf("Please input the name you wanna serach:>\n");
scanf("%s", Tname);
int i = FindByName(p, Tname);
if (i != -1)
{
for (int j = i; j < p->size; j++)
{
p->data[j] = p->data[j++];
}
p->size--;
}
else
{
printf("The person is not exsited in this address book.\n");
Sleep(1000);
}
}
void modifyInfo(AddressBook* p)
{
char* Tname[10];
printf("Please input the name you wanna modify:>\n");
scanf("%s", Tname);
int i = FindByName(p, Tname);
if (i != -1)
{
printf("Please input a new name:>\n");
scanf("%s", p->data[i].name);
printf("Please input a new age:>\n");
scanf("%d", &(p->data[i].age));
printf("Please input a new gender:>\n");
scanf("%s", p->data[i].gender);
printf("Please input a new phone number:>\n");
scanf("%s", p->data[i].number);
printf("Modified Sucessfully!\n");
Sleep(500);
return;
}
else
{
printf("The person is not exsited in this address book.\n");
Sleep(1000);
}
}
void showInfo(AddressBook* p)
{
printf("\n\n================================================\n");
printf("%-10s\t%-4s\t%-5s\t%-12s\n", "Name", "Age", "Gender", "Phone Number");
printf("================================================\n");
for (int i = 0; i < p->size; i++)
{
printf("%-10s\t%-4d\t%-5s\t%-12s\n\n\n\n", p->data[i].name, p->data[i].age,
p->data[i].gender, p->data[i].number);
}
printf("Press 'Enter' to continue:>");
getchar();
getchar();
}
int compareByAge(const void* a, const void* b)
{
PeoInfo* p1 = (PeoInfo*)a;
PeoInfo* p2 = (PeoInfo*)b;
return p2->age - p1->age;
}
void sortByAgeInfo(AddressBook* p)
{
qsort(p->data, p->size, sizeof(PeoInfo), compareByAge);
printf("Sorted Sucessfully!\n");
Sleep(500);
}
void saveData(AddressBook* p)
{
FILE* pf = fopen("data.dat", "wb");
if (pf == NULL)
{
perror("saveData:fopen");
return 1;
}
for (int i = 0; i < p->size; i++)
{
fwrite(p->data + i, sizeof(PeoInfo), 1, pf);
}
fclose(pf);
pf = NULL;
}
void freeMemory(AddressBook* p)
{
free(p->data);
p->data = NULL;
}