欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言實現(xiàn)通訊錄的方法(包括靜態(tài)版本和動態(tài)版本)

 更新時間:2021年09月27日 10:59:13   作者:小白想做程序員  
本文給大家分享C語言實現(xiàn)通訊錄的方法(包括靜態(tài)版本和動態(tài)版本),針對每種方法給大家介紹的非常詳細,需要的朋友參考下吧

1.靜態(tài)通訊錄的實現(xiàn)

實現(xiàn)的方法:

我們采用的方法就是工程形勢,實現(xiàn)將功能和定義以及測試分成三個文件,其中定義放在.h文件,實現(xiàn)和測試放在.c文件當(dāng)中。

(1)contact.h文件的基本實現(xiàn):

#pragma once//防止頭文件重復(fù)定義

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define MAX 1000  //靜態(tài)通訊錄,最大為1000

#include<stdio.h>
#include<string.h>

struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
};

//靜態(tài)的版本
struct Contact
{
	struct PeoInfo data[MAX];//容量為1000的通訊錄
	int sz; //已經(jīng)使用了多少個通訊錄
};

//初始化通訊錄
void InitContact(struct Contact* pc);

//通訊錄的增加
void PushContact(struct Contact* pc);


//通訊錄的刪除:
void PopContact(struct Contact* pc);

//通訊錄的打印
void ShowContact(const struct Contact* pc);

//通過名字來查找,找到返回下標(biāo),找不到返回-1。
int FindContactByName(const struct Contact* pc, const char* name);

//通訊的查找并打印
void SearchContact(const struct Contact* pc);

//修改聯(lián)系人的信息
void ModContact(struct Contact* pc);

//以名字排序
void SortByNameContact(struct Contact* pc);

//清空所有聯(lián)系人
void DestroyContact(struct Contact* pc);

(2)contact.c文件的基本實現(xiàn)

#include"contact.h"

//通訊錄的初始化
void InitContact(struct Contact* pc)
{
	pc->sz = 0;
	memset(pc->data, 0, sizeof(struct PeoInfo) * MAX);
}

//通訊錄增加
void PushContact(struct Contact* pc)
{
	if (pc->sz >= MAX)
	{
		printf("This contact is full!\n");
	}
	printf("please input name:>");
	scanf("%s", &pc->data[pc->sz].name);
	printf("please input age:>");
	scanf("%d", &pc->data[pc->sz].age);
	printf("please input sex:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("please input tele:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("please input addr:>");
	scanf("%s", pc->data[pc->sz].addr);
	printf("Add successful\n");
	pc->sz++;
}

//通過名字來查找,找到返回下標(biāo),找不到返回-1。
int FindContactByName(const struct Contact* pc, const char* ThisName)
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(ThisName, pc->data[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}

//通訊錄的打印
void ShowContact(const struct Contact* pc)
{
	printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age","sex","tele","addr");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
	}
}

//通訊錄的刪除:
void PopContact(struct Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty. Can Not be deleted\n");
		return;
	}
	char name[NAME_MAX] = { 0 };
	printf("Please enter the name of the person you want to delete:>");
	scanf("%s", name);
	//1.查找
	int pos = FindContactByName(pc, name);
	if ( pos == -1)
	{
		printf("There's no one by that name!\n");
	}
	else
	{
		//2.刪除
		int j = 0;
		for (j = pos; j < pc->sz - 1; j++)
		{
			pc->data[j] = pc->data[j + 1];
		}
		printf("Delection successful\n");
	}
	pc->sz--;
	
}

//通訊的查找
void SearchContact(const struct Contact* pc)
{
	char name[NAME_MAX] = { 0 };
	printf("please input name of you want to search:>");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("No this one!\n");
	}
	else
	{
		printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", 
		pc->data[pos].name, 
		pc->data[pos].age, 
		pc->data[pos].sex, 
		pc->data[pos].tele, 
		pc->data[pos].addr);
	}
}

//修改聯(lián)系人的信息
void ModContact(struct Contact* pc)
{
	char name[NAME_MAX] = { 0 };
	printf(" Please enter the name of the person you want to modify : > ");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("No this one\n");
	}
	else
	{
		printf("please input new name:>");
		scanf("%s", &pc->data[pos].name);
		printf("please input new age:>");
		scanf("%d", &pc->data[pos].age);
		printf("please input new sex:>");
		scanf("%s", pc->data[pos].sex);
		printf("please input new tele:>");
		scanf("%s", pc->data[pos].tele);
		printf("please input new addr:>");
		scanf("%s", pc->data[pos].addr);
		printf("Modify successful\n");
	}
}
//以名字排序
void SortByNameContact(struct Contact* pc)
{
	if (pc->sz == 0 || pc->sz == 1)
	{
		printf("Too few contacts to sort\n");
	}
	int i, j;
	for (i = 0; i < pc->sz ; i++)
	{
		int flag = 0;
		for (j = 0; j < pc->sz - 1 - i; j++)
		{
			if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
			{
				struct PeoInfo tmp = pc->data[j];
				pc->data[j] = pc->data[j + 1];
				pc->data[j + 1] = tmp;
				flag = 1;
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
	printf("Sort successful\n");
}


//清空所有聯(lián)系人
void DestroyContact(struct Contact* pc)
{
	pc->sz = 0;
	printf("Destroy successful\n");
}

(3)test.c文件的實現(xiàn)

#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
	printf("************************************\n");
	printf("******   1.add      2.del    *******\n");
	printf("******   3.search   4.modify *******\n");
	printf("******   5.show     6.sort   *******\n");
	printf("******   7.destroy  0.exit   *******\n");
	printf("************************************\n");
}

enum Option//使用枚舉,增加代碼的可讀性
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	SORT,
	DESTROY,
};

int main()
{
	int input;
	struct Contact con;
	//初始化通訊錄
	InitContact(&con);
	do
	{
		menu();
		printf("please select:>\n");
		scanf("%d", &input);
		switch (input)
		{
		case EXIT:
			printf("exit succseeful!\n");
			break;
		case ADD:
			PushContact(&con);
			break;
		case DEL:
			PopContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortByNameContact(&con);
			break;
		case DESTROY:
			DestroyContact(&con);
			break;
		default:
			printf("Select error, please select again!\n");
		}
	} while (input);
	return 0;
}

2.動態(tài)通訊錄的實現(xiàn)

實現(xiàn)的方法:

和靜態(tài)實現(xiàn)細節(jié)都差不多,只是靜態(tài)使用的數(shù)組,所以固定了通訊錄的大小,不能超出通訊錄大小的限制,且沒有使用完還會造成空間的浪費。所以使用動態(tài)內(nèi)存分配來實現(xiàn)動態(tài)的通訊錄。會節(jié)省空間并且是通訊錄的大小變得靈活。

(1)contact.h文件的基本實現(xiàn):

#pragma once//防止頭文件重復(fù)定義

#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define DEFINE_SZ  3

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

struct PeoInfo
{
	char name[NAME_MAX];
	int age;
	char sex[SEX_MAX];
	char tele[TELE_MAX];
	char addr[ADDR_MAX];
};

//動態(tài)的版本
struct Contact
{
	struct PeoInfo* data;
	int sz;//通訊錄已經(jīng)使用元素的個數(shù)
	int capacity; //當(dāng)前的最大容量	
};

//初始化通訊錄
void InitContact(struct Contact* pc);

//通訊錄的增加
void PushContact(struct Contact* pc);

//通訊錄的刪除:
void PopContact(struct Contact* pc);

//通訊錄的打印
void ShowContact(const struct Contact* pc);

//通過名字來查找,找到返回下標(biāo),找不到返回-1。
int FindContactByName(const struct Contact* pc, const char* name);

//通訊的查找并打印
void SearchContact(const struct Contact* pc);

//修改聯(lián)系人的信息
void ModContact(struct Contact* pc);

//以名字排序
void SortByNameContact(struct Contact* pc);

//清空所有聯(lián)系人
void DestroyContact(struct Contact* pc);

(2)contact.c文件的基本實現(xiàn)

#include"contact.h"

//通訊錄的初始化


void InitContact(struct Contact* pc)
{
	pc->sz = 0;
	pc->data = (struct PeoInfo*)malloc(sizeof(struct PeoInfo) * DEFINE_SZ);
	if (pc->data == NULL)
	{
		printf("malloc fail\n");
		return;
	}
	pc->capacity = DEFINE_SZ;
}

//通訊錄增加
void PushContact(struct Contact* pc)
{
	if (pc->sz == pc->capacity)
	{
		//增容:
		struct PeoInfo* ptr = (struct PeoInfo*)realloc(pc->data, sizeof(struct PeoInfo) * (pc->capacity + 2));
		if (ptr == NULL)
		{
			printf("realloc fail\n");
			return;
		}
		else
		{
			pc->data = ptr;
			pc->capacity += 2;
			printf("Compatibilization successful\n");
		}
		
	}
	//錄入新增成員信息:
	printf("please input name:>");
	scanf("%s", &pc->data[pc->sz].name);
	printf("please input age:>");
	scanf("%d", &pc->data[pc->sz].age);
	printf("please input sex:>");
	scanf("%s", pc->data[pc->sz].sex);
	printf("please input tele:>");
	scanf("%s", pc->data[pc->sz].tele);
	printf("please input addr:>");
	scanf("%s", pc->data[pc->sz].addr);
	printf("Add successful\n");
	pc->sz++;	
}

//通過名字來查找,找到返回下標(biāo),找不到返回-1。
int FindContactByName(const struct Contact* pc, const char* ThisName)
{
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		if (strcmp(ThisName, pc->data[i].name) == 0)
		{
			return i;
		}
	}
	return -1;
}

//通訊錄的打印
void ShowContact(const struct Contact* pc)
{
	printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
	int i = 0;
	for (i = 0; i < pc->sz; i++)
	{
		printf("%15s\t%5d\t%8s\t%15s\t%20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
	}
}

//通訊錄的刪除:
void PopContact(struct Contact* pc)
{
	if (pc->sz == 0)
	{
		printf("Address book is empty. Can Not be deleted\n");
		return;
	}
	char name[NAME_MAX] = { 0 };
	printf("Please enter the name of the person you want to delete:>");
	scanf("%s", name);
	//1.查找
	int pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("There's no one by that name!\n");
	}
	else
	{
		//2.刪除
		int j = 0;
		for (j = pos; j < pc->sz - 1; j++)
		{
			pc->data[j] = pc->data[j + 1];
		}
		printf("Delection successful\n");
	}
	pc->sz--;

}

//通訊的查找
void SearchContact(const struct Contact* pc)
{
	char name[NAME_MAX] = { 0 };
	printf("please input name of you want to search:>");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("No this one!\n");
	}
	else
	{
		printf("%15s\t%5s\t%8s\t%15s\t%20s\n", "name", "age", "sex", "tele", "addr");
		printf("%15s\t%5d\t%8s\t%15s\t%20s\n",
			pc->data[pos].name,
			pc->data[pos].age,
			pc->data[pos].sex,
			pc->data[pos].tele,
			pc->data[pos].addr);
	}
}

//修改聯(lián)系人的信息
void ModContact(struct Contact* pc)
{
	char name[NAME_MAX] = { 0 };
	printf(" Please enter the name of the person you want to modify : > ");
	scanf("%s", name);
	int pos = FindContactByName(pc, name);
	if (pos == -1)
	{
		printf("No this one\n");
	}
	else
	{
		printf("please input new name:>");
		scanf("%s", &pc->data[pos].name);
		printf("please input new age:>");
		scanf("%d", &pc->data[pos].age);
		printf("please input new sex:>");
		scanf("%s", pc->data[pos].sex);
		printf("please input new tele:>");
		scanf("%s", pc->data[pos].tele);
		printf("please input new addr:>");
		scanf("%s", pc->data[pos].addr);
		printf("Modify successful\n");
	}
}

//以名字排序
void SortByNameContact(struct Contact* pc)
{
	if (pc->sz == 0 || pc->sz == 1)
	{
		printf("Too few contacts to sort\n");
	}
	int i, j;
	for (i = 0; i < pc->sz; i++)
	{
		int flag = 0;
		for (j = 0; j < pc->sz - 1 - i; j++)
		{
			if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0)
			{
				struct PeoInfo tmp = pc->data[j];
				pc->data[j] = pc->data[j + 1];
				pc->data[j + 1] = tmp;
				flag = 1;
			}
		}
		if (flag == 0)
		{
			break;
		}
	}
	printf("Sort successful\n");
}

//清空所有聯(lián)系人
void DestroyContact(struct Contact* pc)
{
	pc->sz = 0;
	pc->capacity = 0;
	free(pc->data);
	pc->data = NULL;
	printf("Destroy successful\n");
}

(3)test.c文件的實現(xiàn)

#include"contact.h"

void menu()
{
	printf("************************************\n");
	printf("******   1.add      2.del    *******\n");
	printf("******   3.search   4.modify *******\n");
	printf("******   5.show     6.sort   *******\n");
	printf("******   7.destroy  0.exit   *******\n");
	printf("************************************\n");
}

enum Option
{
	EXIT,
	ADD,
	DEL,
	SEARCH,
	MODIFY,
	SHOW,
	SORT,
	DESTROY,
};

int main()
{
	int input;
	struct Contact con;
	//初始化通訊錄
	InitContact(&con);
	do
	{
		menu();
		printf("please select:>\n");
		scanf("%d", &input);
		switch (input)
		{
		case EXIT:
			DestroyContact(&con);
			printf("exit succseeful!\n");
			break;
		case ADD:
			PushContact(&con);
			break;
		case DEL:
			PopContact(&con);
			break;
		case SEARCH:
			SearchContact(&con);
			break;
		case MODIFY:
			ModContact(&con);
			break;
		case SHOW:
			ShowContact(&con);
			break;
		case SORT:
			SortByNameContact(&con);
			break;
		case DESTROY:
			DestroyContact(&con);
			break;
		default:
			printf("Select error, please select again!\n");
		}
	} while (input);
	return 0;
}

3.總結(jié)

其實通訊錄的實現(xiàn)就是數(shù)據(jù)結(jié)構(gòu)的一種體現(xiàn),我們需要學(xué)的東西還有很多,請大家一起跟我努力吧??!
再就是有問題請大家及時指正?。?!謝謝大家。

到此這篇關(guān)于C語言實現(xiàn)通訊錄的方法(包括靜態(tài)版本和動態(tài)版本)的文章就介紹到這了,更多相關(guān)C語言實現(xiàn)通訊錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用C語言編寫鋼琴小程序

    使用C語言編寫鋼琴小程序

    這篇文章主要為大家詳細介紹了使用C語言編寫鋼琴小程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • 一篇文章教你用C語言模擬實現(xiàn)字符串函數(shù)

    一篇文章教你用C語言模擬實現(xiàn)字符串函數(shù)

    這篇文章主要介紹了C語言模擬實現(xiàn)字符串函數(shù),開發(fā)程序的時候經(jīng)常使用到一些字符串函數(shù),例如求字符串長度,拷貝字符串……,需要的朋友可以參考下
    2021-09-09
  • C++線程池實現(xiàn)代碼

    C++線程池實現(xiàn)代碼

    C++11中,線程我們可以理解為對應(yīng)一個thread對象,任務(wù)可以理解為要執(zhí)行的函數(shù),通常是耗時的函數(shù)。線程過多或者頻繁創(chuàng)建和銷毀線程會帶來調(diào)度開銷,進而影響緩存局部性和整體性能
    2021-12-12
  • 基于c++中的默認拷貝函數(shù)的使用詳解

    基于c++中的默認拷貝函數(shù)的使用詳解

    本篇文章對c++中默認拷貝函數(shù)的使用進行了詳細的分析介紹。需要的朋友參考下
    2013-05-05
  • 基于C++編寫一個鍵盤提示音程序

    基于C++編寫一個鍵盤提示音程序

    首先講一下思路,這次制作的小黑子相當(dāng)于鍵盤提示音,輸入J,N,T,M,會發(fā)出“雞你太美”的聲音,連續(xù)按下JNTM則會發(fā)出“你干嘛啊,哎呦”的聲音,感興趣的可以了解一下
    2023-03-03
  • C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié)

    C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié)

    這篇文章主要介紹了C++中調(diào)用復(fù)制(拷貝)函數(shù)的三種情況總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Qt實現(xiàn)字幕無間隙滾動效果

    Qt實現(xiàn)字幕無間隙滾動效果

    這篇文章主要為大家詳細介紹了如何利用Qt實現(xiàn)字幕無間隙滾動效果,文中的實現(xiàn)過程講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-11-11
  • C語言實現(xiàn)學(xué)籍信息管理系統(tǒng)

    C語言實現(xiàn)學(xué)籍信息管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)學(xué)籍信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • C++設(shè)計模式之橋接模式

    C++設(shè)計模式之橋接模式

    這篇文章主要介紹了C++設(shè)計模式之橋接模式,本文講解了什么是橋接模式、為什么要使用橋接模式、什么時候使用橋接模式等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • C語言楊氏矩陣簡單實現(xiàn)方法

    C語言楊氏矩陣簡單實現(xiàn)方法

    楊氏矩陣是一個數(shù)字矩陣,矩陣的每一行從左到右一次遞增,矩陣從上到下遞增,在這樣的矩陣中查找一個數(shù)字是否存在。時間復(fù)雜度小于O(N),有需要的朋友可以借鑒參考下
    2023-02-02

最新評論