C语言实现简单通讯录_C 语言_更新简易制作教程抛砖引玉MOD制作工具

时间:2021年04月11日 06:23:43

本文实例为大家分享了C语言实现简易通讯录的具体代码,供大家参考,具体内容如下

一、概述

通讯录的实现其中包括了指针、结构体、动态内存管理、文件的读写操作等知识点,是对这些知识点应用巩固的非常好的例子;

通讯录功能如下:

添加联系人删除联系人查找联系人修改联系人显示所有联系人加载联系人保存联系人联系人排序

二、代码如下

contact.h

#ifndef CONTACT_H#define CONTACT_H#define NAME_MAX 20#define SEX_MAX 10#define TEL_MAX 12#define ADDR_MAX 20#define MEMBER_MAX 1typedef struct info{ char name[NAME_MAX]; char sex[SEX_MAX]; int age; char tele[TEL_MAX]; char addr[ADDR_MAX];}info;typedef struct contact{ info *data; int size; int capacity;}contact, *pcontact;//初始化通讯录void InitContact(pcontact list);//添加联系人void AddContact(pcontact list);//删除联系人void DellContact(pcontact list);//查找联系人void FindContact(pcontact list);//修改联系人void ModContact(pcontact list);//显示所有联系人void ShowContact(pcontact list);//联系人排序(选择法)void SortContact(pcontact list);//检查容量int CheckSize(pcontact list);//查找联系人在数组data第几个位置int FindContactAddr(pcontact list);//从文件中加载联系人到内存void LoadContact(pcontact list);//将联系人从内从中保存到文件void SaveContact(pcontact list);#endif

contact.c

#include #include #include #include "contact.h"//初始化通讯录void InitContact(pcontact list){ list->data = (info *)malloc(sizeof(info)* MEMBER_MAX); list->size = 0; list->capacity = MEMBER_MAX; //每次初始化是加载一遍文件,防止最新录入的在保存在文件时覆盖上次 //打开程序时保存的信息 LoadContact(list);}//添加联系人void AddContact(pcontact list){ info tmp_member; if (CheckCapacity(list) == 0) { printf("成员已满,添加失败!"); return; } printf("请输入姓名:"); scanf("%s", tmp_member.name); printf("请输入性别:"); scanf("%s", tmp_member.sex); printf("请输入年龄:"); scanf("%d", &tmp_member.age); printf("请输入电话:"); scanf("%s", tmp_member.tele); printf("请输入地址:"); scanf("%s", tmp_member.addr); list->data[list->size] = tmp_member; printf("添加成功!\n"); list->size++;}//删除联系人void DellContact(pcontact list){ char tmp_name[NAME_MAX] = { 0 }; int ret = 0; printf("请输入姓名:"); scanf("%s", tmp_name); ret = FindContactAddr(list, tmp_name); if (ret == -1) { printf("无此联系人!\n"); return; } for (ret; ret < list->size - 1; ret++) { list->data[ret] = list->data[ret + 1]; } printf("删除成功!\n"); list->size--;}//查找联系人void FindContact(pcontact list){ char tmp_name[NAME_MAX] = { 0 }; printf("请输入姓名:"); scanf("%s", tmp_name); for (int i = 0; i < list->size; i++) { if (strcmp(tmp_name, list->data[i].name) == 0) { printf("===============================\n"); printf("姓名:%s\n", list->data[i].name); printf("性别:%s\n", list->data[i].sex); printf("年龄:%d\n", list->data[i].age); printf("电话:%s\n", list->data[i].tele); printf("地址:%s\n", list->data[i].addr); printf("===============================\n"); return; } } printf("无此联系人!\n");}//查找联系人在数组data第几个位置int FindContactAddr(pcontact list, char *tmp_name){ for (int i = 0; i < list->size; i++) { if (strcmp(tmp_name, list->data[i].name) == 0) return i; } return -1;}//修改联系人void ModContact(pcontact list){ info tmp_member; int ret = 0; printf("请输入姓名:"); scanf("%s", tmp_member.name); ret = FindContactAddr(list, tmp_member.name); if (ret == -1) { printf("无此联系人!\n"); return; } printf("请输入性别:"); scanf("%s", tmp_member.sex); printf("请输入年龄:"); scanf("%s", &tmp_member.age); printf("请输入电话:"); scanf("%s", tmp_member.tele); printf("请输入地址:"); scanf("%s", tmp_member.addr); list->data[ret] = tmp_member; printf("修改成功!\n");}//联系人排序(选择法)void SortContact(pcontact list){ info tmp_member; for (int i = 0; i < list->size - 2; i++) { for (int j = 1; j < list->size - 1; j++) { if (strcmp(list->data[i].name, list->data[j].name) > 0) { tmp_member = list->data[i]; list->data[i] = list->data[j]; list->data[j] = tmp_member; } } }}//检查上次动态开辟的空间是否够用,不够时继续开辟int CheckCapacity(pcontact list){ if (list->size == list->capacity) { info *tmp = (info *)malloc(sizeof(info)* (list->capacity + 10)); if (tmp == NULL) return 0; memcpy(tmp, list->data, sizeof(info) * list->size); free(list->data); list->data = tmp; list->capacity += 10; } return 1;}//显示所有联系人void ShowContact(pcontact list){ for (int i = 0; i < list->size; i++) { printf("============第%d位==============\n", i + 1); printf("姓名:%s\n", list->data[i].name); printf("性别:%s\n", list->data[i].sex); printf("年龄:%d\n", list->data[i].age); printf("电话:%s\n", list->data[i].tele); printf("地址:%s\n", list->data[i].addr); printf("===============================\n"); }}//从文件中加载联系人到内存void LoadContact(pcontact list){ info tmp_member; FILE *pf = NULL; pf = fopen("./contatc.dat", "rb"); if (pf == NULL) perror("fopen error"); //按照内存块读取,一次读1块,1块大小是sizeof(info) while (fread(&tmp_member, sizeof(info), 1, pf)) { if (CheckCapacity(list)) { list->data[list->size] = tmp_member; list->size++; } } if (pf != NULL) { fclose(pf); pf = NULL; printf("加载成功!\n"); }}//将联系人从内从中保存到文件void SaveContact(pcontact list){ FILE *pf = NULL; pf = fopen("./contatc.dat", "wb"); if (pf == NULL) perror("fopen error"); for (int i = 0; i < list->size; i++) { //按照内存块写入,一次写1块,1块大小是sizeof(info) fwrite(list->data + i, sizeof(info), 1, pf); } if (pf != NULL) { fclose(pf); pf = NULL; printf("保存成功!\n"); }}

test.c

#define _CRT_SECURE_NO_WARNINGS#include #include "contact.h"enum opt{ Exit, Add, Dell, Find, Mod, Show, Load, Save, Sort,};void menu(void){ printf("/*========================通讯录======================*/\n"); printf("/*= 0.退出通讯录 1.添加联系人 =*/\n"); printf("/*= 2.删除联系人 3.查找联系人 =*/\n"); printf("/*= 4.修改联系人 5.显示所有联系人 =*/\n"); printf("/*= 6.加载联系人 7.保存联系人 =*/\n"); printf("/*= 8.联系人排序=*/\n"); printf("/*====================================================*/\n");}void TestContact(void){ contact pc; InitContact(&pc); int opt = 0; do { menu(); printf("请选择:>"); scanf("%d", &opt); switch (opt) { case Exit: printf("退出系统...\n"); break; case Add: AddContact(&pc); break; case Dell: DellContact(&pc); break; case Find: FindContact(&pc); break; case Mod: ModContact(&pc); break; case Show: ShowContact(&pc); break; case Load: LoadContact(&pc); break; case Save: SaveContact(&pc); break; case Sort: SortContact(&pc); break; default: printf("输入选项有误,重新输入!\n"); break; } } while (opt);}int main(void){ TestContact(); printf("\n"); system("pause"); return 0;}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:C语言实现一个简易通讯录C语言实现简单电子通讯录(2)C语言实现简单电子通讯录C语言实现简易通讯录功能基于C语言实现个人通讯录管理系统C语言实现通讯录C语言实现简易通讯录C语言实现个人通讯录管理系统C语言通讯录管理系统完整版C语言简易通讯录的实现代码