C語言如何實現通訊錄

蝸牛 互聯網技術資訊 2022-10-16 4 0

本篇內容主要講解“C語言如何實現通訊錄”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C語言如何實現通訊錄”吧!

一、Contact.h文件

包含函數的聲明

#pragma?once
#define?_CRT_SECURE_NO_WARNINGS?1
#include?<stdio.h>
#include?<string.h>
#include?<stdlib.h>
#include?<assert.h>

#define?MAX?100
#define?MAX_NAME?20
#define?MAX_SEX?10
#define?MAX_TELE?20
#define?MAX_ADDR?30
#define?MAX_LEG?5

//結構體的定義,用于儲存通訊錄數據
struct?Contact
{
	char?name[MAX_NAME];
	char?sex[MAX_SEX];
	char?tele[MAX_TELE];
	char?addr[MAX_ADDR];
	int?age;
};

struct?PeoInfo
{
	struct?Contact*?data;
	int?length;
	int?capacity;
};

//定義游戲菜單函數
void?menu();

//定義初始結構體函數
void?InitContact(struct?PeoInfo*?abs);

//添加通訊錄消息函數的聲明
void?AddConInfo(struct?PeoInfo*?abs);

//查詢信息是否存在函數
int?IsExist(struct?PeoInfo*?abs,?char*?name);

//刪除指定信息函數的定義
void?DelConInfo(struct?PeoInfo*?abs);

//修改指定信息函數的定義
void?ModefiInfo(struct?PeoInfo*?abs);

//查詢指定信息函數的定義
void?SearchInfo(struct?PeoInfo*?abs);

//展示通訊錄信息函數的定義
void?ShowInfo(struct?PeoInfo*?abs);

//清空通訊錄列表函數的定義
void?ClearInfo(struct?PeoInfo*?abs);

//排序通訊錄信息函數的定義
void?SortInfo(struct?PeoInfo*?abs);

//擴容函數的定義
int?IncMemmery(struct?PeoInfo*?abs);

//釋放內存函數的定義
void?FreeInfo(struct?PeoInfo*?abs);

//保存通訊錄函數的定義
void?saveInfo(struct?PeoInfo*?abs);

//加載通訊錄函數的定義
void?loadInfo(struct?PeoInfo*?abs);

二、Contact.c文件

包含函數的實現

#include?"contact.h";



//游戲菜單函數的實現
void?menu()
{
	printf("***********************************\n");
	printf("*******1.Add??????????2.Del********\n");
	printf("*******3.Modefi???????4.Search*****\n");
	printf("*******5.show?????????6.Clear******\n");
	printf("*******7.sort?????????0.exit*******\n");
	printf("***********************************\n");
}


//擴容函數的實現
int?IncMemmery(struct?PeoInfo*?abs)
{
	struct?Contact*?ptr?=?(struct?Contact*)realloc(abs->data,?(abs->capacity?+?3)?*?sizeof(struct?Contact));
	if?(ptr?==?NULL)
	{
		perror("GetMemmery():");
		return?-1;
	}
	else
	{
		abs->data?=?ptr;
		abs->capacity?+=?3;
		return?1;
	}
}

//初始化通訊錄函數的實現
void?loadInfo(struct?PeoInfo*?abs)
{
	FILE*?pf;
	pf?=?fopen("Contact.txt",?"rb");
	if?(pf?==?NULL)
	{
		perror("InitContact():");
		return;
	}
	struct?Contact?tmp?=?{?0?};
	while?(fread(&tmp,?sizeof(struct?Contact),?1,?pf))
	{
		if?(abs->length?==?abs->capacity)
		{
			IncMemmery(abs);
		}
		*(abs->data?+?abs->length)?=?tmp;
		abs->length++;
	}
	fclose(pf);
	pf?==?NULL;
}

void?InitContact(struct?PeoInfo*?abs)
{
	assert(abs);
	abs->length?=?0;
	abs->data?=?(struct?Contact*)malloc(MAX_LEG?*?sizeof(struct?Contact));
	abs->capacity?=?MAX_LEG;
	loadInfo(abs);
}

//查詢信息是否存在函數的實現
int?IsExist(struct?PeoInfo*?abs,?char*?name)
{
	for?(int?i?=?0;?i?<?abs->length;?i++)
	{
		if?(strcmp(abs->data[i].name,?name)?==?0)
		{
			return?i;
		}
	}
	return?-1;
}

//添加通訊錄消息函數的實現
void?AddConInfo(struct?PeoInfo*?abs)
{
	assert(abs);
	if?(abs->capacity?==?abs->length)
	{
		int?ret??=?IncMemmery(abs);
		if?(ret?==?1)
		{
			printf("擴容成功!\n");
		}
		else
		{
			printf("擴容失敗,內存不足!\n");
			return;
		}
	}
	
	printf("請輸入添加的姓名:>");
	scanf("%s",?abs->data[abs->length].name);
	printf("請輸入添加的姓別:>");
	scanf("%s",?abs->data[abs->length].sex);
	printf("請輸入添加的聯系方式:>");
	scanf("%s",?abs->data[abs->length].tele);
	printf("請輸入添加的住址:>");
	scanf("%s",?abs->data[abs->length].addr);
	printf("請輸入添加的年齡:>");
	scanf("%d",?&(abs->data[abs->length].age));

	abs->length++;
	printf("已成功添加聯系人!\n");
}

//刪除指定信息函數的實現
void?DelConInfo(struct?PeoInfo*?abs)
{
	assert(abs);
	char?name[MAX_NAME];
	printf("請輸入要刪除通訊錄的姓名:>");
	scanf("%s",?name);
	int?ret?=?IsExist(abs,?name);
	if?(ret?==?-1)
	{
		printf("不存在此聯系人!\n");
	}
	else
	{
		for?(int?i?=?ret;?i?<?abs->length;?i++)
		{
			abs->data[i]?=?abs->data[i?+?1];
		}
		abs->length--;
	}
}

//修改制定信息函數的實現
void?ModefiInfo(struct?PeoInfo*?abs)
{
	char?name[MAX_NAME];
	printf("請輸入要修改通訊錄信息的姓名:>");
	scanf("%s",?name);
	int?ret?=?IsExist(abs,?name);
	if?(ret?==?-1)
	{
		printf("要修改指定聯系人不存在!\n");
	}
	else
	{
		printf("請輸入要修改的姓名:>");
		scanf("%s",?abs->data[ret].name);
		printf("請輸入要修改的性別:>");
		scanf("%s",?abs->data[ret].sex);
		printf("請輸入要修改的聯系方式:>");
		scanf("%s",?abs->data[ret].tele);
		printf("請輸入要修改的住址:>");
		scanf("%s",?abs->data[ret].addr);
		printf("請輸入要修改的年齡:>");
		scanf("%d",?&(abs->data[ret].age));

		printf("修改成功!\n");

	}
}

//查詢指定信息函數的實現
void?SearchInfo(struct?PeoInfo*?abs)
{
	assert(abs);
	char?name[MAX_NAME];
	printf("請輸入要查詢通訊錄的姓名:>");
	scanf("%s",?name);
	int?ret?=?IsExist(abs,?name);
	if?(ret?==?-1)
	{
		printf("要查找的信息不存在!");
	}
	else
	{
		printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10s\n","姓名","性別","聯系方式","住址","年齡");
		printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10d\n",
			abs->data[ret].name,
			abs->data[ret].sex,
			abs->data[ret].tele,
			abs->data[ret].addr,
			abs->data[ret].age);

		printf("查詢成功!\n");
	}
	
}

//展示通訊錄信息函數的實現
void?ShowInfo(struct?PeoInfo*?abs)
{
	assert(abs);
	printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10s\n",?"姓名",?"性別",?"聯系方式",?"住址",?"年齡");
	for?(int?i?=?0;?i?<?abs->length;?i++)
	{
		printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10d\n",
			abs->data[i].name,
			abs->data[i].sex,
			abs->data[i].tele,
			abs->data[i].addr,
			abs->data[i].age);
	}
}

//清空通訊錄列表函數的實現
void?ClearInfo(struct?PeoInfo*?abs)
{
	assert(abs);
	struct?Contact*?ptr?=?(struct?Contact*)realloc(abs->data,?MAX_LEG?*?(sizeof(struct?Contact)));
	if?(ptr?==?NULL)
	{
		perror("ClearInfo():");
	}
	else
	{
		abs->length?=?0;
		abs->data?=?ptr;
		memset(abs->data,?0,?MAX_LEG?*?sizeof(struct?Contact));
		abs->capacity?=?MAX_LEG;
		printf("清空通訊錄成功!\n");
	}
}


//排序通訊錄函數的實現
int?CmpByAge(const?void*?e1,?const?void*?e2)
{
	return?((struct?Contact*)e1)->age?-?((struct?Contact*)e2)->age;
}

int?CmpByName(const?void*?e1,?const?void*?e2)
{
	return?strcmp(((struct?Contact*)e1)->name,?((struct?Contact*)e2)->name);
}

void?SortInfo(struct?PeoInfo*?abs)
{
	getchar();
	char?ch;
	printf("請輸入排序的方式:>N(姓名)、A(年齡),N?or?A:>");
	ch?=?getchar();
	if?(ch?==?'A')
	{
		qsort(abs->data,?abs->length,?sizeof(struct?Contact),?CmpByAge);
		printf("已按照年齡排序成功!\n");
	}
	else?if(ch?==?'N')
	{
		qsort(abs->data,?abs->length,?sizeof(struct?Contact),?CmpByName);
		printf("已按照姓名排序成功!\n");
	}
	else
	{
		printf("輸入錯誤!\n");
	}
}

//釋放內存函數的實現
void?FreeInfo(struct?PeoInfo*?abs)
{
	free(abs->data);
	abs->data?=?NULL;
}


//保存通訊錄函數的實現
void?saveInfo(struct?PeoInfo*?abs)
{
	FILE*?pf;
	pf?=?fopen("Contact.txt",?"wb");
	if?(pf?==?NULL)
	{
		perror("saveInfo():");
		return;
	}
	for?(int?i?=?0;?i?<?abs->length;?i++)
	{
		fwrite(abs->data?+?i,?sizeof(struct?Contact),?1,?pf);
	}
	fclose(pf);
	pf?=?NULL;
}

三、test.c文件

包含主函數即代碼思想

#include?"contact.h";

int?main()
{
	int?input?=?0;
	struct?PeoInfo?con;
	//初始化結構體
	InitContact(&con);
	do
	{
		menu();
		printf("請選擇:>");
		scanf("%d",?&input);
		switch?(input)
		{
		case?1:
			AddConInfo(&con);
			break;
		case?2:
			DelConInfo(&con);
			break;
		case?3:
			ModefiInfo(&con);
			break;
		case?4:
			SearchInfo(&con);
			break;
		case?5:
			ShowInfo(&con);
			break;
		case?6:
			ClearInfo(&con);
			break;
		case?7:
			SortInfo(&con);
			break;
		case?0:
			saveInfo(&con);
			FreeInfo(&con);
			printf("退出通訊錄!");
			break;
		default:
			printf("選擇錯誤!\n");
		}
	}?while?(input);
	return?0;
}

到此,相信大家對“C語言如何實現通訊錄”有了更深的了解,不妨來實際操作一番吧!這里是蝸牛博客網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:niceseo99@gmail.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

評論

日本韩欧美一级A片在线观看