首页 > 其他分享 >19.C语言实现【通讯录】

19.C语言实现【通讯录】

时间:2022-12-04 16:57:12浏览次数:49  
标签:sz addrBook 19 void C语言 Contact 通讯录 printf data

简单功能展示

image-20220518225558651

增加联系人功能。

image-20220518225605310

按照姓名排序功能。

image-20220518225612398

保存文件,重新启动重新加载功能。

头文件contact.h

//文件保存版
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
#include<errno.h>
#define MAXNAME 20
#define MAXNUMBER 20
#define MAXADDR 20
#define MAXSIZE 100
#define DEFAULT_SZ 3
typedef struct Information
{
	char name[MAXNAME];
	char number[MAXNUMBER];
	int age;
	char address[MAXADDR];
}Information;

typedef struct Contact
{
	Information* data;
	int sz;
	int capacity;
}Contact;

void menu();
void Init(Contact* addrBook);
void show(Contact* addrBook);
void Add(Contact* addrBook);
void Del(Contact* addrBook);
int Find(Contact* addrBook, char name[MAXNAME]);
void Search(Contact* addrBook);
void Modify(Contact* addrBook);
void Destroy(Contact* addrBook);
int cmp(const void* e1, const void* e2);
void sort(Contact* addrBook);
void Save(Contact* addBook);
void reload(Contact* addrBook);

函数体源文件contact.c

#include"contact.h"

void menu()
{
	printf("***************************\n");
	printf("*******1.add     2.del ****\n");
	printf("*******3.search  4.modify *\n");
	printf("*******5.show    0.exit****\n");
	printf("*******   6.sort **********\n");
}
void Init(Contact* addrBook)
{
	Information* tmp = (Information*)malloc(sizeof(Information) * DEFAULT_SZ);
	if (tmp != NULL)
	{
		addrBook->data = tmp;
	}
	else
	{
		printf("%s\n", strerror(errno));
		return;
	}
	addrBook->capacity = DEFAULT_SZ;
	addrBook->sz = 0;
}
void reload(Contact* addrBook)
{
	FILE* pf = fopen("contact.txt", "rb");
	if (pf == NULL)
	{
		printf("%s", strerror(errno));
		return;
	}
	Information* tmp = NULL;
	Information buf = { 0 };
	while (fread(&buf, sizeof(Information), 1, pf) != 0)
	{
		if (addrBook->sz == addrBook->capacity)
		{
			tmp = realloc(addrBook->data, sizeof(Information) * (addrBook->capacity + 2));
			if (tmp != NULL)
			{
				addrBook->data = tmp;
				addrBook->capacity += 2;
				printf("扩容成功\n");
			}
			else
			{
				printf("%s", strerror(errno));
				return;
			}
		}
		addrBook->data[addrBook->sz] = buf;
		addrBook->sz++;
	}
	printf("加载成功\n");
	fclose(pf);
	pf = NULL;
}
void show(Contact* addrBook)
{
	printf("%-15s\t", "姓名");
	printf("%-15s\t", "电话");
	printf("%-15s\t", "年龄");
	printf("%-15s\t", "地址");
	printf("\n");
	for (int i = 0; i < addrBook->sz; i++)
	{
		printf("%-15s\t", addrBook->data[i].name);
		printf("%-15s\t", addrBook->data[i].number);
		printf("%-15d\t", addrBook->data[i].age);
		printf("%-15s\t", addrBook->data[i].address);
		printf("\n");
	}
}

void Add(Contact* addrBook)
{
	if (addrBook->sz == addrBook->capacity)
	{
		Information* tmp = (Information*)realloc(addrBook->data, (addrBook->capacity + 2) * sizeof(Information));
		if (tmp != NULL)
		{
			addrBook->data = tmp;
		}
		else
		{
			printf("%s\n", strerror(errno));
			return;
		}
		addrBook->capacity += 2;
		printf("扩容成功\n");
	}
	printf("请输入姓名\n");
	scanf("%s", addrBook->data[addrBook->sz].name);
	printf("请输入电话\n");
	scanf("%s", addrBook->data[addrBook->sz].number);
	printf("请输入年龄\n");
	scanf("%d", &addrBook->data[addrBook->sz].age);
	printf("请输入地址\n");
	scanf("%s", addrBook->data[addrBook->sz].address);
	addrBook->sz++;
	printf("添加联系人成功\n");
}

int Find(Contact* addrBook,char name[MAXNAME])
{
	for (int i = 0; i < addrBook->sz; i++)
	{
		if (strcmp(addrBook->data[i].name, name) == 0)
			return i;
	}
	return -1;
}

void Del(Contact* addrBook)
{
	if (addrBook->sz == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	char name[MAXNAME] = { 0 };
	printf("请输入要删除的人的名字\n");
	scanf("%s", name);
	int pos = Find(addrBook, name);
	if (pos == -1)
	{
		printf("查无此人\n");
		return;
	}
	for (int i = pos; i < addrBook->sz - 1; i++)
	{
		addrBook->data[i] = addrBook->data[i + 1];
	}
	addrBook->sz--;
	printf("删除成功\n");
}

void Search(Contact* addrBook)
{
	if (addrBook->sz == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	char name[MAXNAME] = { 0 };
	printf("请输入要查找的人的姓名\n");
	scanf("%s", name);
	int pos = Find(addrBook, name);
	if (pos == -1)
	{
		printf("查无此人\n");
		return;
	}
	printf("%-15s\t", "姓名");
	printf("%-15s\t", "电话");
	printf("%-15s\t", "年龄");
	printf("%-15s\t", "地址");
	printf("\n");
	printf("%-15s\t", addrBook->data[pos].name);
	printf("%-15s\t", addrBook->data[pos].number);
	printf("%-15d\t", addrBook->data[pos].age);
	printf("%-15s\t", addrBook->data[pos].address);
	printf("\n");
}

void Modify(Contact* addrBook)
{
	if (addrBook->sz == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	char name[MAXNAME] = { 0 };
	printf("请输入要修改的人的名字\n");
	scanf("%s", name);
	int pos = Find(addrBook, name);
	if (pos == -1)
	{
		printf("查无此人\n");
		return;
	}
	printf("请输入姓名\n");
	scanf("%s", addrBook->data[pos].name);
	printf("请输入电话\n");
	scanf("%s", addrBook->data[pos].number);
	printf("请输入年龄\n");
	scanf("%d", &addrBook->data[pos].age);
	printf("请输入地址\n");
	scanf("%s", addrBook->data[pos].address);
	printf("修改成功\n");
}

void Destroy(Contact* addrBook)
{
	free(addrBook->data);
	addrBook->data = NULL;
	addrBook->capacity = 0;
	addrBook->sz = 0;
}
int cmp(const void* e1, const void* e2)
{
	struct Information* a = (struct Information*)e1;
	struct Information* b = (struct Information*)e2;
	return strcmp(a->name, b->name);
}

void sort(Contact* addrBook)
{
	qsort(addrBook->data, addrBook->sz, sizeof(struct Information), cmp);
}

void Save(Contact* addrBook)
{
	FILE* pf = fopen("contact.txt", "wb");
	if (pf == NULL)
	{
		printf("%s", strerror(errno));
		return;
	}
	fwrite((void*)addrBook->data, sizeof(Information), addrBook->sz, pf);
	fclose(pf);
	pf = NULL;
}


主函数源文件test.c

#include"contact.h"
int main()
{
	int input = 0;
	Contact addrBook;
	Init(&addrBook);
	reload(&addrBook);
	do
	{
		menu();
		printf("请输入\n");
		scanf("%d", &input);
		switch (input)
		{
		case 0:
			Save(&addrBook);
			Destroy(&addrBook);
			printf("退出\n");
			break;
		case 1:
			Add(&addrBook);
			break;
		case 2:
			Del(&addrBook);
			break;
		case 3:
			Search(&addrBook);
			break;
		case 4:
			Modify(&addrBook);
			break;
		case 5:
			show(&addrBook);
			break;
		case 6:
			sort(&addrBook);
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
	return 0;
}

标签:sz,addrBook,19,void,C语言,Contact,通讯录,printf,data
From: https://www.cnblogs.com/ncphoton/p/16950165.html

相关文章

  • 18.【C语言进阶】程序的编译
    程序的翻译环境和执行环境在ANSIC的任何一种实现中,存在两个不同的环境。第1种是翻译环境,在这个环境中源代码被转换为可执行的机器指令。第2种是执行环境,它用于实际执行......
  • 2022-2023-1 20221419 《计算机基础与程序设计》第14周学习总结
    2022-2023-120221419《计算机基础与程序设计》第14周学习总结作业信息班级:[2022-2023-1-计算机基础与程序设计]https://edu.cnblogs.com/campus/besti/2022-2023-1-CFA......
  • 初识C语言(结构)
    前言:C语言是一种结构化的程序设计语言。对于新手来说要想学好一门编程语言,除了要了解编程的相关知识,更要了解其结构。“万层高楼平地起”结构就是地基中到比较重要的一环,所......
  • c语言第一个程序及编译运行过程中问题相关说明
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intmain(){printf("helloworld");return0;}1.当打开visualstudio,创建新项目后将如下所示页面不小心关闭则......
  • 每日食词—day019
    residualadj. n.剩余、残余的、残差、误差newfilev. n.新文件、新建文件livestreamn.现场直播、视频直播、直播频道trien.字典树、前缀树、单词查找树......
  • oracle19c 修改字符集
    环境:centos7.6oracle19c修改过程:shutdownimmediate;startupmount;ALTERSYSTEMENABLERESTRICTEDSESSION;ALTERSYSTEMSETJOB_QUEUE_PROCESSES=0;ALTE......
  • C语言:显示当前时间
    #include<stdio.h>#include<time.h>intmain(){time_ttt;//typedeflongtime_t;time_t实际上是long型,从一个时间点(一般是1970年1月1日0时0分0秒)到当前的秒......
  • delphi D11编程语言手册 学习笔记(P344-419) 接口/类操作/对象与内存
      这本书可以在Delphi研习社②群256456744的群文件里找到.书名:Delphi11AlexandriaEdition.pdfP344接口与类相比,接口侧重于封装,并提供与类之间一种比......
  • pwn | ciscn_2019_es_2
    pwn|ciscn_2019_es_2x86ret2text栈迁移这是我第一次做栈迁移的题目,浅浅记录一下思路关键的利用点在于利用leaveret的组合把esp搬到低地址的地方(也就是我们输入的地......
  • C语言会员管理系统
    C语言会员管理系统一、会员管理系统设计与实现设计一个会员管理系统。根据以下功能,分析使用的逻辑结构和存储结构。(1)增加功能:能够录入新数据(包括:姓名、电话号码、......