//通讯录实现
//1.新增联系人
//2.删除联系人
//3.修改联系人
//4.查找联系人
//5.打印全部联系人
//6.排序联系人
//7.清空联系人
//8.保存联系人到文件
//9.加载联系人
//0.退出
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct PersonInfo {
char name[1024];
int age;
char sex[1024];
char phone[1024];
char place[1024];
}PersonInfo;
typedef struct AddressBook {
PersonInfo* persons;
int size;
int capacity;
}AddressBook;
AddressBook g_address_book;
int menu() {
printf("********************************************\n");
printf("* 1.新增联系人 \n");
printf("* 2.删除联系人 \n");
printf("* 3.查找联系人 \n");
printf("* 4.修改联系人 \n");
printf("* 5.打印全部联系人 \n");
printf("* 6.排序联系人 \n");
printf("* 7.清空联系人 \n");
printf("* 8.保存联系人到文件 \n");
printf("* 0.退出 \n");
printf("********************************************\n");
printf("请输入你的选择\n");
int choice = 0;
scanf("%d", &choice);
return choice;
}
void Init() {
g_address_book.size = 0;
g_address_book.persons = (PersonInfo*)malloc(g_address_book.capacity * sizeof(PersonInfo));
g_address_book.capacity = 100;
//free(g_address_book.persons);
//for (int i = 0; i < g_address_book.capacity; i++) {
// g_address_book.persons[i].name[0] = '\0';
// g_address_book.persons[i].age = 0;
// g_address_book.persons[i].sex[0] = '\0';
// g_address_book.persons[i].phone[0] = '\0';
// g_address_book.persons[i].place[0] = '\0';
//}
}
void Empty() {
//
}
void AddPersonInfo() {
printf("新增联系人\n");
if (g_address_book.size >= g_address_book.capacity) {
printf("当前空间不足,进行扩容!\n");
g_address_book.capacity += 100;
/*g_address_book.persons = (PersonInfo*)realloc(g_address_book.persons,
g_address_book.capacity*sizeof(PersonInfo));*/
PersonInfo* p = (PersonInfo*)malloc(
g_address_book.capacity * sizeof(PersonInfo)
);
for (int i = 0; i < g_address_book.size; i++) {
p[i] = g_address_book.persons[i];
}
free(g_address_book.persons);
g_address_book.persons = p;
}
PersonInfo* person_info = &g_address_book.persons[g_address_book.size];
printf("请输入联系人姓名:");
scanf("%s", person_info->name);
printf("请输入联系人年龄:");
scanf("%d", &(person_info->age));
printf("请输入联系人性别:");
scanf("%s", person_info->sex);
printf("请输入联系人电话:");
scanf("%s", person_info->phone);
printf("请输入联系人地址:");
scanf("%s", person_info->place);
++g_address_book.size;
printf("新增联系人成功!\n");
}
void DelPersonInfo() {
printf("删除联系人\n");
if (g_address_book.size <= 0) {
printf("删除失败!通讯录为空!\n");
return;
}
printf("请输入要删除的序号");
int id = 0;
scanf("%d", &id);
if (id < 0 || id >= g_address_book.size) {
printf("删除联系人失败!输入的序号有误");
return;
}
g_address_book.persons[id]
= g_address_book.persons[g_address_book.size - 1];
--g_address_book.size;
printf("删除联系人成功\n");
}
void FindPersonInfo() {
printf("查找联系人!\n");
if (g_address_book.size <= 0) {
printf("通讯录为空!查找失败!\n");
}
printf("请输入姓名:");
char name[1024] = { 0 };
scanf("%s", name);
for (int i = 0; i < g_address_book.size; i++) {
PersonInfo* info = &g_address_book.persons[i];
if (strcmp(info->name, name) == 0) {
printf("[%d] %s\t%d\t%s\t%s\t%s\n", i, info->name, info->age, info->sex, info->phone, info->place);
}
}
printf("查找联系人成功!\n");
}
void UpdatePersonInfo() {
printf("更新联系人!\n");
if (g_address_book.size <= 0) {
printf("通讯录为空!修改失败\n");
}
printf("请输入要修改的序号:\n");
int id = 0;
scanf("%d", &id);
if (id<0 || id>g_address_book.size) {
printf("输入的序号有误,修改失败!\n");
return;
}
PersonInfo* info = &g_address_book.persons[id];
printf("请输入新的姓名:(%s)\n", info->name);
char name[1024] = { 0 };
scanf("%s", name);
if (strcmp(name, "*") != 0) {
strcpy(info->name, name);
}
printf("请输入新的年龄:(%d)\n", info->age);
int age = 0;
scanf("%d", &age);
if (age != 0) {
info->age = age;
}
printf("请输入新的性别:(%s)\n", info->sex);
char sex[1024] = { 0 };
scanf("%s", sex);
if (strcmp(sex, "*") != 0) {
strcpy(info->sex, sex);
}
char phone[1024] = { 0 };
printf("请输入新的电话:(%s)\n", info->phone);
scanf("%s", phone);
if (strcmp(phone, "*") != 0) {
strcpy(info->phone, phone);
}
printf("请输入新的地址:(%s)\n", info->place);
char place[1024] = { 0 };
scanf("%s", place);
if (strcmp(place, "*") != 0) {
strcpy(info->place, place);
}
printf("更新联系人成功!\n");
}
void PrintAllPersonInfo() {
printf("打印全部联系人\n");
for (int i = 0; i < g_address_book.size; i++) {
PersonInfo* info = &g_address_book.persons[i];
printf("[%d] %s\t%d\t%s\t%s\t%s\n", i, info->name, info->age, info->sex, info->phone, info->place);
}
printf("共打印了%d条数据!\n", g_address_book.size);
printf("打印全部联系人成功!\n");
}
void SortPersonInfo() {
printf("对联系人进行姓名排序\n");
if (g_address_book.size <= 0) {
printf("通讯录为空!排序失败\n");
}
int bound = 0;
for (; bound < g_address_book.size - 1; bound++) {
for (int i = 0; i < g_address_book.size - bound - 1; i++) {
if ((strcmp(g_address_book.persons[i].name, g_address_book.persons[i + 1].name) > 0)) {
PersonInfo tmp = g_address_book.persons[i];
g_address_book.persons[i] = g_address_book.persons[i + 1];
g_address_book.persons[i + 1] = tmp;
}
}
}
printf("排序成功\n");
for (int i = 0; i < g_address_book.size; i++) {
PersonInfo* info = &g_address_book.persons[i];
printf("[%d] %s\t%d\t%s\t%s\t%s\n", i, info->name, info->age, info->sex, info->phone, info->place);
}
}
void ClearAllPersonInfo() {
printf("清空全部数据!\n");
printf("您真的要清空全部数据吗?Y/N\n");
char choice[1024] = { 0 };
scanf("%s", choice);
if (strcmp(choice, "Y") == 0) {
g_address_book.size = 0;
printf("清空全部数据成功!\n");
}
else {
printf("清空操作取消!\n");
}
}
void FilePersonInfo() {
int i = 0;
FILE *pf = fopen("D:/tongxunlu.txt", "w");
if (pf == NULL) {
perror("error");
exit(EXIT_SUCCESS);
}
for (i = 0; i < g_address_book.size; i++)
{
fwrite((void*)&g_address_book.persons[i], sizeof(struct AddressBook), 1, pf);
}
fclose(pf);
printf("保存成功!\n");
}
typedef void(*Func)();
int main() {
Func arr[] = {
Empty,
AddPersonInfo,
DelPersonInfo,
FindPersonInfo,
UpdatePersonInfo,
PrintAllPersonInfo,
SortPersonInfo,
ClearAllPersonInfo,
FilePersonInfo
};
Init();
while (1) {
int choice = menu();
if (choice < 0
|| choice >= sizeof(arr) / sizeof(arr[0])) {
printf("您的输入有误,请重新输入!\n");
continue;
}
if (choice == 0) {
printf("goodbye\n");
break;
}
arr[choice]();
}
return 0;
system("pause");
}
C语言实现通讯录(动态版本)
最新推荐文章于 2019-11-19 21:30:54 发布