C語言實(shí)現(xiàn)通訊錄的示例代碼
更新時間:2022年10月12日 17:02:14 作者:畫畫的北北164
這篇文章主要為大家詳細(xì)介紹了如何錄音C語言實(shí)現(xiàn)一個簡單的通訊錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
一、Contact.h文件
包含函數(shù)的聲明
#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
//結(jié)構(gòu)體的定義,用于儲存通訊錄數(shù)據(jù)
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;
};
//定義游戲菜單函數(shù)
void menu();
//定義初始結(jié)構(gòu)體函數(shù)
void InitContact(struct PeoInfo* abs);
//添加通訊錄消息函數(shù)的聲明
void AddConInfo(struct PeoInfo* abs);
//查詢信息是否存在函數(shù)
int IsExist(struct PeoInfo* abs, char* name);
//刪除指定信息函數(shù)的定義
void DelConInfo(struct PeoInfo* abs);
//修改指定信息函數(shù)的定義
void ModefiInfo(struct PeoInfo* abs);
//查詢指定信息函數(shù)的定義
void SearchInfo(struct PeoInfo* abs);
//展示通訊錄信息函數(shù)的定義
void ShowInfo(struct PeoInfo* abs);
//清空通訊錄列表函數(shù)的定義
void ClearInfo(struct PeoInfo* abs);
//排序通訊錄信息函數(shù)的定義
void SortInfo(struct PeoInfo* abs);
//擴(kuò)容函數(shù)的定義
int IncMemmery(struct PeoInfo* abs);
//釋放內(nèi)存函數(shù)的定義
void FreeInfo(struct PeoInfo* abs);
//保存通訊錄函數(shù)的定義
void saveInfo(struct PeoInfo* abs);
//加載通訊錄函數(shù)的定義
void loadInfo(struct PeoInfo* abs);
二、Contact.c文件
包含函數(shù)的實(shí)現(xiàn)
#include "contact.h";
//游戲菜單函數(shù)的實(shí)現(xiàn)
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");
}
//擴(kuò)容函數(shù)的實(shí)現(xià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;
}
}
//初始化通訊錄函數(shù)的實(shí)現(xiàn)
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);
}
//查詢信息是否存在函數(shù)的實(shí)現(xiàn)
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;
}
//添加通訊錄消息函數(shù)的實(shí)現(xiàn)
void AddConInfo(struct PeoInfo* abs)
{
assert(abs);
if (abs->capacity == abs->length)
{
int ret = IncMemmery(abs);
if (ret == 1)
{
printf("擴(kuò)容成功!\n");
}
else
{
printf("擴(kuò)容失敗,內(nèi)存不足!\n");
return;
}
}
printf("請輸入添加的姓名:>");
scanf("%s", abs->data[abs->length].name);
printf("請輸入添加的姓別:>");
scanf("%s", abs->data[abs->length].sex);
printf("請輸入添加的聯(lián)系方式:>");
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("已成功添加聯(lián)系人!\n");
}
//刪除指定信息函數(shù)的實(shí)現(xià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("不存在此聯(lián)系人!\n");
}
else
{
for (int i = ret; i < abs->length; i++)
{
abs->data[i] = abs->data[i + 1];
}
abs->length--;
}
}
//修改制定信息函數(shù)的實(shí)現(xiàn)
void ModefiInfo(struct PeoInfo* abs)
{
char name[MAX_NAME];
printf("請輸入要修改通訊錄信息的姓名:>");
scanf("%s", name);
int ret = IsExist(abs, name);
if (ret == -1)
{
printf("要修改指定聯(lián)系人不存在!\n");
}
else
{
printf("請輸入要修改的姓名:>");
scanf("%s", abs->data[ret].name);
printf("請輸入要修改的性別:>");
scanf("%s", abs->data[ret].sex);
printf("請輸入要修改的聯(lián)系方式:>");
scanf("%s", abs->data[ret].tele);
printf("請輸入要修改的住址:>");
scanf("%s", abs->data[ret].addr);
printf("請輸入要修改的年齡:>");
scanf("%d", &(abs->data[ret].age));
printf("修改成功!\n");
}
}
//查詢指定信息函數(shù)的實(shí)現(xià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","姓名","性別","聯(liá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");
}
}
//展示通訊錄信息函數(shù)的實(shí)現(xiàn)
void ShowInfo(struct PeoInfo* abs)
{
assert(abs);
printf("%-20s\t%-10s\t%-20s\t%-20s\t%-10s\n", "姓名", "性別", "聯(liá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);
}
}
//清空通訊錄列表函數(shù)的實(shí)現(xiàn)
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");
}
}
//排序通訊錄函數(shù)的實(shí)現(xià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");
}
}
//釋放內(nèi)存函數(shù)的實(shí)現(xiàn)
void FreeInfo(struct PeoInfo* abs)
{
free(abs->data);
abs->data = NULL;
}
//保存通訊錄函數(shù)的實(shí)現(xiàn)
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文件
包含主函數(shù)即代碼思想
#include "contact.h";
int main()
{
int input = 0;
struct PeoInfo con;
//初始化結(jié)構(gòu)體
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;
}
到此這篇關(guān)于C語言實(shí)現(xiàn)通訊錄的示例代碼的文章就介紹到這了,更多相關(guān)C語言通訊錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ 冒泡排序數(shù)據(jù)結(jié)構(gòu)、算法及改進(jìn)算法
冒泡排序是一種簡單排序。這種排序是采用“冒泡策略”將最大元素移到最右邊。在冒泡過程中,相鄰兩個元素比較,如果左邊大于右邊的,則進(jìn)行交換兩個元素。這樣一次冒泡后,可確保最大的在最右邊。然后執(zhí)行n次冒泡后排序即可完畢2013-04-04
C++實(shí)現(xiàn)教務(wù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)教務(wù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C++函數(shù)指針+對象指針+this指針+指向類靜態(tài)和非靜態(tài)成員的指針
這篇文章主要介紹了C++函數(shù)指針+對象指針+this指針+指向類靜態(tài)和非靜態(tài)成員的指針,函數(shù)指針定義和賦值的語法指其中數(shù)據(jù)類型代表指向函數(shù)的返回類型,形參表為指向函數(shù)的形參表,更多相關(guān)資料需要的朋友可以參考一下下面文章內(nèi)容2022-03-03

