重庆响应式网站建设费用,南京百度小程序开发,企业网站程序制作,电信备案新增网站前言 在上一节中我们实现了顺序表#xff0c;现在我们将使用顺序表完成通讯录的实现。#xff08;注#xff1a;本人水平有限#xff0c;“小屎山”有些许bug#xff0c;代码冗余且语无伦次#xff0c;望谅解#xff01;#x1f605;#xff09; 文章目录 一、数据结构… 前言 在上一节中我们实现了顺序表现在我们将使用顺序表完成通讯录的实现。注本人水平有限“小屎山”有些许bug代码冗余且语无伦次望谅解 文章目录 一、数据结构设计二、初始化和内存管理初始化动态扩容 三、基础功能实现增删改查添加联系人删除联系人修改联系人查找联系人显示通讯录 四、文件操作保存至文件从文件读取 五、所有代码contact.hcontact.cmain.c 一、数据结构设计
首先定义一个结构体Contact来存储单个联系人的信息包括姓名、性别、年龄和手机号。
typedef struct Contact {char name[NAME_MAX]; //姓名char sex[SEX_MAX]; //性别int age; //年龄char phone[PHONE_MAX];//手机号
}Contact;然后定义一个结构体SeqList序列表用于存储所有联系人信息。该结构体包含一个指向Contact结构体数组的指针data以及两个整数size表示当前有效数据个数capacity表示当前数组容量。
typedef struct SeqList {Contact* data; //通讯录信息int size; //有效数据个数int capacity; //通讯录总容量
}ConList; 二、初始化和内存管理
初始化
在程序开始之前我们需要进行初始化操作即为SeqList中的data分配内存并设置初始值。
int ContactInit(ConList* list) { assert(list);Contact* tmp (Contact*)malloc(sizeof(Contact));if (tmpNULL) {printf(内存分配失败\n);return -1;}list-data tmp;list-size 0;list-capacity 1;return 0;
}动态扩容
如果SeqList中的data数组已满我们需要进行扩容。
int resizeConList(ConList* list) {assert(list);int newCapacity list-capacity * 2;Contact* newData (Contact*)realloc(list-data, sizeof(Contact) * newCapacity);if (newData NULL) {printf(内存分配失败\n);return -1;}list-data newData;list-capacity newCapacity;return 0;
}三、基础功能实现增删改查
添加联系人
添加联系人时需要先检查是否有足够的空间如果没有则扩容。然后通过标准输入获取联系人信息。
int ContactAdd(ConList* list) {assert(list);char sign N;do {if (list-size list-capacity) {int ret resizeConList(list);if (ret -1) {printf(通讯录扩大失败\n);return -1;}}printf(请输入姓名:);scanf( %20s, list-data[list-size].name); // 限制输入长度printf(请输入性别:);scanf( %7s, list-data[list-size].sex); // 限制输入长度printf(请输入年龄:);if (scanf(%d, (list-data[list-size].age)) ! 1) {printf(无效的年龄输入\n);return -1;}printf(请输入手机号:);scanf( %20s, list-data[list-size].phone); // 限制输入长度list-size;ContactShow(list);printf(Y 继续添加 N 结束 请输入:);scanf( %c, sign); // 注意空格用于吸收前一个输入后可能残留的换行符printf(***********************************\n);} while (sign Y || sign y);return 0;
}删除联系人
删除联系人时需要输入要删除的联系人的序号。
int ContactDel(ConList* list) {assert(list);ContactShow(list);int input 0;//要删除联系人序号char sign 0;do {printf(\n);printf(选择要删除的联系人的序号:);scanf(%d, input);for (int i input; i list-size; i) {list-data[i - 1] list-data[i];}list-size--;ContactShow(list);printf(Y 继续删除 N 结束 请输入:);scanf( %c, sign);} while (signY||signy);return 0;
} 修改联系人
修改联系人与添加联系人类似但需要先定位到要修改的联系人。
int ContactModify(ConList* list) {assert(list);ContactShow(list);int input 0;//要修改的联系人序号char sign 0;do {printf(\n);printf(选择要修改的联系人序号:);scanf(%d, input);//联系人信息修改printf(请输入姓名:);scanf( %20s, list-data[input-1].name); printf(请输入性别:);scanf( %7s, list-data[input - 1].sex);printf(请输入年龄:);if (scanf(%d, (list-data[input - 1].age)) ! 1) {printf(无效的年龄输入\n);return -1;}printf(请输入手机号:);scanf( %20s, list-data[input - 1].phone); ContactShow(list);printf(Y 继续修改 N 结束 请输入:);scanf( %c,sign);} while (sign Y || sign y);return 0;
}查找联系人
可以通过姓名或手机号进行查找。
int ContactFind(ConList* list) {assert(list);int input 0;char keyword[21] {0};//联系人关键字int sign 0;//联系人是否找到判断do {sign 0;printf(选择查找联系人方式 1 姓名查找 2 手机号查找 0 退出查找:);scanf(%d, input);if (!input) return 0;//退出查找printf(请输入查找关键字:);scanf( %20s, keyword); // 限制输入长度switch (input) {case 1: // 按姓名查找for (int i 0; i list-size; i) {if (strcmp(list-data[i].name, keyword) 0) {printf(找到联系人%d\n, i 1);sign 1;break;}}break;case 2: // 按手机号查找for (int i 0; i list-size; i) {if (strcmp(list-data[i].phone, keyword) 0) {printf(找到联系人的序号%d\n, i 1);sign 1;break;}}break;default:printf(无效的选项\n);break;}} while (input);if (!sign) {printf(未找到联系人\n);}return 0;
}显示通讯录
int ContactShow(ConList* list) {assert(list);int count 1;//序号printf(--------------------------------------------------------------\n);printf(|序号| 姓名 | 性别 |年龄| 手机号 |\n);while (count list-size) {printf(--------------------------------------------------------------\n);printf(|%-4d|%-20s|%-8s|%-4d|%-20s|\n,count,list-data[count - 1].name, list-data[count - 1].sex, list-data[count - 1].age,list-data[count - 1].phone);count;}printf(--------------------------------------------------------------\n);return 0;
}四、文件操作
保存至文件
使用二进制模式将所有联系人信息保存到文件。
int ContactSave(ConList* list) {assert(list);FILE* file fopen(contact.dat, wb);if (file NULL) {printf(无法创建或打开文件\n);return -1;}// 写入通讯录的大小元素数量fwrite((list-size), sizeof(int), 1, file);// 写入通讯录的数据fwrite(list-data, sizeof(Contact), list-size, file);fclose(file);fileNULL;return 0;
}从文件读取
与保存操作相反从文件中读取所有联系人信息。
int ContactLoad(ConList* list) {assert(list);FILE* file fopen(contact.dat, rb);if (file NULL) {printf(无法打开文件\n);return -1;}// 读取通讯录的大小元素数量fread((list-size), sizeof(int), 1, file);// 根据读取到的大小动态分配内存if (list-size) {Contact* tmp (Contact*)realloc(list-data, sizeof(Contact) * list-size);if (tmp NULL) {printf(内存分配失败\n);fclose(file);return -1;}list-data tmp;// 读取通讯录的数据fread(list-data, sizeof(Contact), list-size, file);list-capacity list-size; // 在这个简单示例中将容量设置为大小}fclose(file);return 0;
}五、所有代码
contact.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#includestdio.h
#includestdlib.h
#includeassert.h
#includestring.h#define NAME_MAX 20
#define SEX_MAX 7
#define PHONE_MAX 20typedef struct Contact {char name[NAME_MAX]; //姓名char sex[SEX_MAX]; //性别int age; //年龄char phone[PHONE_MAX];//手机号
}Contact;typedef struct SeqList {Contact* data; //通讯录信息int size; //有效数据个数int capacity; //通讯录总容量
}ConList;//添加联系人
int ContactAdd(ConList* list);
//删除联系人
int ContactDel(ConList* list);
//修改联系人
int ContactModify(ConList* list);
//查找指定联系人
int ContactFind(ConList* list);//通讯录的初始化
int ContactInit(ConList* list);
//通讯录扩容
int resizeConList(ConList* list);
//通讯录销毁
void ContactDestroy(ConList* list);
//查看通讯录
int ContactShow(ConList* list);//检测contact.dat存不存在如果不存在就创建一个
int checkAndCreateFile(const char* filename);
//保存通讯录到文件
int ContactSave(ConList* list);
//从文件中读取通讯录
int ContactLoad(ConList* list); contact.c
#includecontact.hint ContactInit(ConList* list) { assert(list);Contact* tmp (Contact*)malloc(sizeof(Contact));if (tmpNULL) {printf(内存分配失败\n);return -1;}list-data tmp;list-size 0;list-capacity 1;return 0;
}int resizeConList(ConList* list) {assert(list);int newCapacity list-capacity * 2;Contact* newData (Contact*)realloc(list-data, sizeof(Contact) * newCapacity);//通常扩容选择1.5倍或者2倍进行扩容if (newData NULL) {printf(内存分配失败\n);return -1;}list-data newData;list-capacity newCapacity;return 0;
}int ContactAdd(ConList* list) {assert(list);char sign N;do {if (list-size list-capacity) {int ret resizeConList(list);if (ret -1) {printf(通讯录扩大失败\n);return -1;}}printf(请输入姓名:);scanf( %20s, list-data[list-size].name); // 限制输入长度printf(请输入性别:);scanf( %7s, list-data[list-size].sex); // 限制输入长度printf(请输入年龄:);if (scanf(%d, (list-data[list-size].age)) ! 1) {printf(无效的年龄输入\n);return -1;}printf(请输入手机号:);scanf( %20s, list-data[list-size].phone); // 限制输入长度list-size;ContactShow(list);printf(Y 继续添加 N 结束 请输入:);scanf( %c, sign); // 注意空格用于吸收前一个输入后可能残留的换行符printf(***********************************\n);} while (sign Y || sign y);return 0;
}int ContactShow(ConList* list) {assert(list);int count 1;//序号printf(--------------------------------------------------------------\n);printf(|序号| 姓名 | 性别 |年龄| 手机号 |\n);while (count list-size) {printf(--------------------------------------------------------------\n);printf(|%-4d|%-20s|%-8s|%-4d|%-20s|\n,count,list-data[count - 1].name, list-data[count - 1].sex, list-data[count - 1].age,list-data[count - 1].phone);count;}printf(--------------------------------------------------------------\n);return 0;
}int ContactDel(ConList* list) {assert(list);ContactShow(list);int input 0;//要删除联系人序号char sign 0;do {printf(\n);printf(选择要删除的联系人的序号:);scanf(%d, input);for (int i input; i list-size; i) {list-data[i - 1] list-data[i];}list-size--;ContactShow(list);printf(Y 继续删除 N 结束 请输入:);scanf( %c, sign);} while (signY||signy);return 0;
}int ContactModify(ConList* list) {assert(list);ContactShow(list);int input 0;//要修改的联系人序号char sign 0;do {printf(\n);printf(选择要修改的联系人序号:);scanf(%d, input);//联系人信息修改printf(请输入姓名:);scanf( %20s, list-data[input-1].name); printf(请输入性别:);scanf( %7s, list-data[input - 1].sex);printf(请输入年龄:);if (scanf(%d, (list-data[input - 1].age)) ! 1) {printf(无效的年龄输入\n);return -1;}printf(请输入手机号:);scanf( %20s, list-data[input - 1].phone); ContactShow(list);printf(Y 继续修改 N 结束 请输入:);scanf( %c,sign);} while (sign Y || sign y);return 0;
}int ContactFind(ConList* list) {assert(list);int input 0;char keyword[21] {0};//联系人关键字int sign 0;//联系人是否找到判断do {sign 0;printf(选择查找联系人方式 1 姓名查找 2 手机号查找 0 退出查找:);scanf(%d, input);if (!input) return 0;//退出查找printf(请输入查找关键字:);scanf( %20s, keyword); // 限制输入长度switch (input) {case 1: // 按姓名查找for (int i 0; i list-size; i) {if (strcmp(list-data[i].name, keyword) 0) {printf(找到联系人%d\n, i 1);sign 1;break;}}break;case 2: // 按手机号查找for (int i 0; i list-size; i) {if (strcmp(list-data[i].phone, keyword) 0) {printf(找到联系人的序号%d\n, i 1);sign 1;break;}}break;default:printf(无效的选项\n);break;}} while (input);if (!sign) {printf(未找到联系人\n);}return 0;
}void ContactDestroy(ConList* list) {assert(list);// 释放动态分配的数组内存if (list-data ! NULL) {free(list-data);}// 将各成员变量设置为初始状态list-data NULL;list-size 0;list-capacity 0;
}int ContactSave(ConList* list) {assert(list);FILE* file fopen(contact.dat, wb);if (file NULL) {printf(无法创建或打开文件\n);return -1;}// 写入通讯录的大小元素数量fwrite((list-size), sizeof(int), 1, file);// 写入通讯录的数据fwrite(list-data, sizeof(Contact), list-size, file);fclose(file);fileNULL;return 0;
}int ContactLoad(ConList* list) {assert(list);FILE* file fopen(contact.dat, rb);if (file NULL) {printf(无法打开文件\n);return -1;}// 读取通讯录的大小元素数量fread((list-size), sizeof(int), 1, file);// 根据读取到的大小动态分配内存if (list-size) {Contact* tmp (Contact*)realloc(list-data, sizeof(Contact) * list-size);if (tmp NULL) {printf(内存分配失败\n);fclose(file);return -1;}list-data tmp;// 读取通讯录的数据fread(list-data, sizeof(Contact), list-size, file);list-capacity list-size; // 在这个简单示例中将容量设置为大小}fclose(file);return 0;
}int checkAndCreateFile(const char* filename) {FILE* file;// 尝试以读模式打开文件file fopen(filename, r);// 检查文件是否已经存在if (file ! NULL) {// 文件存在关闭文件fclose(file);printf(contact.dat存在\n);return 0; // 返回0表示文件已存在无需创建}else {// 文件不存在以写模式创建一个新文件file fopen(filename, w);if (file ! NULL) {fclose(file);printf(文件创建成功\n);return 1; // 返回1表示文件成功创建}else {// 文件创建失败可能是因为权限或磁盘空间不足等原因printf(文件创建失败\n);return -1; // 返回-1表示文件创建失败}}
}main.c
#includecontact.hvoid menu() {printf(-------------------------------------\n);printf(-----1 添加联系人 2 删除联系人-----\n);printf(-----3 修改联系人 4 查找联系人-----\n);printf(-------------0 退出通讯录------------\n);printf(-------------------------------------\n);}int main() {ConList list;ContactInit(list);//通讯录初始化checkAndCreateFile(contact.dat);//检测contact.dat存不存在如果不存在就创建一个//从文件中读取通讯录ContactLoad(list);int input 0;do {menu();printf(\n);printf(选择操作:);scanf(%d,input);switch(input){case 1:ContactAdd(list);break;case 2:ContactDel(list);break;case 3:ContactModify(list);break;case 4:ContactFind(list);break;default:break;}} while (input);ContactSave(list);//保存通讯录到文件ContactDestroy(list);//通讯录销毁return 0;
}如果你喜欢这篇文章点赞评论关注⭐️哦 欢迎大家提出疑问以及不同的见解。 文章转载自: http://www.morning.tzzkm.cn.gov.cn.tzzkm.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.tqsgt.cn.gov.cn.tqsgt.cn http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn http://www.morning.glncb.cn.gov.cn.glncb.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.sdkaiyu.com.gov.cn.sdkaiyu.com http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.lmcrc.cn.gov.cn.lmcrc.cn http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.qqfcf.cn.gov.cn.qqfcf.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.tqdlk.cn.gov.cn.tqdlk.cn http://www.morning.qnzgr.cn.gov.cn.qnzgr.cn http://www.morning.qptbn.cn.gov.cn.qptbn.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.fblkr.cn.gov.cn.fblkr.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.dkmzr.cn.gov.cn.dkmzr.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.wrlxt.cn.gov.cn.wrlxt.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.xkmrr.cn.gov.cn.xkmrr.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.wbrf.cn.gov.cn.wbrf.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.xhqr.cn.gov.cn.xhqr.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.ylljn.cn.gov.cn.ylljn.cn http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.hnhgb.cn.gov.cn.hnhgb.cn http://www.morning.xzlp.cn.gov.cn.xzlp.cn http://www.morning.ctrkh.cn.gov.cn.ctrkh.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.lwtld.cn.gov.cn.lwtld.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn http://www.morning.wbxrl.cn.gov.cn.wbxrl.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.wtsr.cn.gov.cn.wtsr.cn http://www.morning.jqjnx.cn.gov.cn.jqjnx.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.lkkkf.cn.gov.cn.lkkkf.cn http://www.morning.ylmxs.cn.gov.cn.ylmxs.cn http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn http://www.morning.rxrw.cn.gov.cn.rxrw.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.xpzkr.cn.gov.cn.xpzkr.cn http://www.morning.bkqdg.cn.gov.cn.bkqdg.cn