C语言学习小结_升级版通讯录

实现一个通讯录
通讯录可以用来存储有一定容量人的信息,当存到最大容量时候,通讯录自动扩容,并且每次程序关闭时候要将程序中的通讯录持久化到硬盘或者数据库中。
提供方法:
1、添加联系人信息
2、删除指定联系人信息
3、显示所有联系人信息
4、修改指定联系人信息
5、显示所有联系人信息
6、清空所有联系人
0、退出通讯录程序
涉及到的知识点
多文件、结构体中的应用、柔性数组的应用、static关键字使用、动态内存管理、利用文件指针持久化和读取数据等。
头文件如下:

#include "contact.h"

int main()
{
	//初始化通讯录
	contact_t* ct = InitContact();
	int quit = 0;
	int select = 0;
	while (!quit){
		Menu();
		printf("please input your select:");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			AddPerson(&ct);
			break;
		case 2:
			DelPerson(ct);
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			ShowContact(ct);
			break;
		case 6:
			Clear(ct);
			break;
		case 7:
			break;
		default:
			quit = 1;
			SaveContact(ct);//在通讯录退出的时候保存通讯录到硬盘
			break;
		}
	}
	printf("ByeBye!\n");
	system("pause");
	return 0;
}

头文件中相关函数的定义:

#include"contact.h"

void Menu()
{
	printf("###################################\n");
	printf("##1.Add  2.Del   3.Search 4.Mod####\n");
	printf("##5.Show 6.Clear 7.Sort   0.Exit###\n");
}
//初始化结构体
contact_t* InitContact(){
	contact_t *ct = NULL;
	FILE* fp = fopen(SAVE_FILE, "rb");
	if (NULL == fp)
	{
		printf("use default:init Contact ing...\n");
		int size = sizeof(contact_t)+sizeof(person_t)*NUM;
		ct = (contact_t*)malloc(size);
		if (NULL == ct){
			printf("Contact malloc error!\n");
			exit(1);
		}
		ct->cap = NUM;
		ct->size = 0;
		printf("use default:init Contact done!\n");
	}
	else{
		//读入通讯录
		printf("read in contact from disk ing....\n");
		contact_t temp;
		fread(&temp, sizeof(contact_t), 1, fp);
		int size = sizeof(contact_t)+sizeof(person_t)*temp.cap;
		 ct = (contact_t*)malloc(size);
		if (NULL == ct){
			printf("Contact malloc error!\n");
			exit(1);
		}
		ct->cap = temp.cap;
		ct->size = temp.size;
		//先把通讯录头读进来,知道cap和size,就可以申请空间了,
		//申请完空间再把柔性数组中的人读到我申请的堆空间里
		fread(ct->persons, sizeof(person_t), temp.size, fp);
		fclose(fp);
		printf("read in contact from disk done....\n");
	}

	return ct;
}
static int FindPerson(contact_t* ct, const char* tel){
	assert(ct&&tel);
	int i;
	for (i = 0; i < ct->size; i++){
		if (strcmp(ct->persons[i].tel, tel) == 0){
			return i;
		}
	}
	return -1;
}
static int IsFull(contact_t* ct){
	return ct->cap == ct->size;
}
static int Inc(contact_t ** ct){
	printf("contact is full,incing....\n");

	*ct = (contact_t *)realloc(*ct, sizeof(**ct) + INC_SIZE*sizeof(person_t));

	if (NULL == *ct){
		return 0;
	}

	((*ct)->cap) += INC_SIZE;
	printf("contact is full,inc done....\n");
	return 1;
}
void AddPerson(contact_t** ct){
	if (!IsFull(*ct) || Inc(ct)){
		//正常添加
		person_t* p = &((*ct)->persons[(*ct)->size]);
		//姓名,性别,年龄,电话,住址
		printf("姓名:");
		scanf(" %s", p->name);
		printf("性别:");
		scanf(" %c", &(p->sex));
		printf("年龄:");
		scanf(" %d", &(p->age));
		printf("电话:");
		scanf(" %s", p->tel);
		printf("住址:");
		scanf(" %s", p->addr);
		
		(*ct)->size += 1;
		
	}
	else{
		//空间满了,申请又失败了
		printf("inc error!\n");
	}

}

void ShowContact(contact_t* ct){
	assert(ct);
	printf("| %10s|%10s\n |\n", "姓名", "电话");
	for (int i = 0; i < ct->size; i++){
		printf("| %10s | %10s\n |\n", ct->persons[i].name, ct->persons[i].tel);
	}
}
static int IsEmpty(contact_t* ct){
	return ct->size == 0;//1为空,0为非空
}

void DelPerson(contact_t* ct){
	assert(ct);
	if (!IsEmpty(ct)){
		printf("请输入你要删除人的电话:");
		char tel[SIZE / 6] = {0};
		scanf(" %s", tel);
		int pos = FindPerson(ct, tel);
		if (pos >= 0){
			ct->persons[pos] = ct->persons[ct->size - 1];
			ct->size -= 1;
		}
		else{
			printf("你要删除的人不存在!\n");
		}

	}
	else{
		printf("通讯录为空!\n");
	}
}

void Clear(contact_t* ct){
	ct->size = 0;
}

void SaveContact(contact_t* ct){
	assert(ct);
	FILE* fp = fopen(SAVE_FILE, "wb");
	if (NULL == fp){
		printf("file open error and save failure!\n");
	}
	//将用通讯录结构体和里面的柔性数组分别写入到硬盘中
	fwrite(ct, sizeof(contact_t), 1, fp);
	fwrite(ct->persons, sizeof(person_t), ct->size, fp);
	fclose(fp);
}
``

main.c主要框架的设计

```c
#include "contact.h"

int main()
{
	//初始化通讯录
	contact_t* ct = InitContact();
	int quit = 0;
	int select = 0;
	while (!quit){
		Menu();
		printf("please input your select:");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			AddPerson(&ct);
			break;
		case 2:
			DelPerson(ct);
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			ShowContact(ct);
			break;
		case 6:
			Clear(ct);
			break;
		case 7:
			break;
		default:
			quit = 1;
			SaveContact(ct);//在通讯录退出的时候保存通讯录到硬盘
			break;
		}
	}
	printf("ByeBye!\n");
	system("pause");
	return 0;
}

有些函数设计并不是很严谨,有兴趣的小伙伴可以基于我的代码去完善功能,比如输入性别的时候我并没有检查输入的准确性,只能输入f和m。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值