C语言通讯录动态内存的版本

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct ContactsInfo {
	char name[1024];
	char phonenum[1024];
}ContactsInfo;

typedef struct AddressBook {
	ContactsInfo* contacts;
	int size;
	int capacity;
}AddressBook;

//全局变量
AddressBook g_address_book;

void Init() {
	g_address_book.size = 0;
	g_address_book.capacity = 100;
	g_address_book.contacts = (ContactsInfo*)malloc(
		g_address_book.capacity * sizeof(ContactsInfo));
	for (int i = 0; i < g_address_book.capacity; ++i) {
		g_address_book.contacts[i].name[0] = '\0';
		g_address_book.contacts[i].phonenum[0] = '\0';
	}
}

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("0.退出\n");
	printf("=====================\n");
	printf("请输入您的选择:");
	int choice = 0;
	scanf("%d", &choice);
	return choice;
}

void Empty() {
	//方便转移表使用
}

void AddContactsInfo() {
	printf("新增联系人\n");
	if (g_address_book.size >= g_address_book.capacity) {
		printf("当前空间不足,进行扩容!\n");
		g_address_book.capacity += 100;
		ContactsInfo* p = (ContactsInfo*)malloc(g_address_book.capacity * sizeof(ContactsInfo));
		for (int i = 0; i < g_address_book.size; ++i) {
			p[i] = g_address_book.contacts[i];
		}
		free(g_address_book.contacts);
		g_address_book.contacts = p;
	}
	ContactsInfo* contact_info = 
		&g_address_book.contacts[g_address_book.size];
	printf("请输入联系人姓名:");
	scanf("%s", contact_info->name);
	printf("请输入联系人电话:");
	scanf("%s", contact_info->phonenum);
	++g_address_book.size;
	printf("新增联系人成功!\n");
}

void DelContactsInfo() {
	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("删除失败,输入序号有误!\n");
		return;
	}
	g_address_book.contacts[id] =
		g_address_book.contacts[g_address_book.size - 1];
	--g_address_book.size;
	printf("删除联系人成功!\n");
}

void FindContactsInfo() {
	printf("查找联系人\n");
	if (g_address_book.size <= 0) {
		printf("查找失败,通讯录为空!\n");
		return;
	}
	printf("请输入要查找的姓名:");
	char name[1024] = { 0 };
	scanf("%s", &name);
	for (int i = 0; i < g_address_book.size; ++i) {
		ContactsInfo* info = &g_address_book.contacts[i];
		if (strcmp(info->name, name) == 0) {
			printf("[%d] %s\t%s\n", i, info->name, info->phonenum);
		}
	}
	printf("查找联系人成功!\n");
}

void UpdateContactsInfo() {
	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("修改失败,输入的序号有误!\n");
		return;
	}
	ContactsInfo* info = &g_address_book.contacts[id];
	printf("请输入新的姓名:(%s)\n", info->name);
	char name[1024] = { 0 };
	scanf("%s", name);
	if (strcmp(name, "*") != 0) {
		strcpy(info->name, name);
	}
	char phonenum[1024] = { 0 };
	printf("请输入新的电话:(%s)\n", info->phonenum);
	scanf("%s", phonenum);
	if (strcmp(phonenum, "*") != 0) {
		strcpy(info->phonenum, phonenum);
	}
	printf("更新联系人成功!\n");
}

void PrintAllContactsInfo() {
	printf("显示全部联系人\n");
	for (int i = 0; i < g_address_book.size; ++i) {
		ContactsInfo* info = &g_address_book.contacts[i];
		printf("[%d] %s\t%s\n",i, info->name, info->phonenum);
	}
	printf("一共显示%d条数据!\n", g_address_book.size);
	printf("显示全部联系人成功\n");
}

void SortContactsInfo() {

}

void ClearAllContactsInfo() {
	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");
	}
}

typedef void(*Func)();

int main() {
	Func arr[] = {
		Empty,
		AddContactsInfo,
		DelContactsInfo,
		FindContactsInfo,
		UpdateContactsInfo,
		PrintAllContactsInfo,
		SortContactsInfo,
		ClearAllContactsInfo
	};
	Init();
	while (1) {
		int choice = Menu();
		if (choice < 0 || choice >= sizeof(arr) / sizeof(arr[0])) {
			printf("您的输入有误!\n");
			continue;
		}
		if (choice == 0) {
			printf("再见!\n");
			break;
		};
		system("cls");
		arr[choice]();
	}
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值