今天分享一个通讯录的代码,代码可以正常运行,但是可能有一点点缺陷,完成的是我们的通讯录功能
contact.h
#pragma once#include#include#include#define MAX 1000#define NAME_MAX 20#define SEX_MAX 5#define TEL_MAX 12#define ADDR_MAX 30typedef struct people{char name[NAME_MAX];//名字char sex[SEX_MAX];//性别int age;//年龄char tel[TEL_MAX];//电话char addr[ADDR_MAX];//地址}people;//定义这个struct people为peopletypedef struct contact{people data[MAX];//存放一千个人信息int sz;//记录通讯录已经保存的信息个数}contact;enum optiom {EXIT,ADD,DEL,SEARCH,MODIFY,SORT,PRINT};//初始化通讯录void Initcontact(contact* pc);//添加用户信息void AddContact(contact* pc);//打印信息void PrintContact(const contact* pc);//删除信息void DelContact(contact* pc);//查找void SearchContact(const contact* pc);//修改功能void ModifyContact(contact* pc);//排序功能没写
contact.c
#define _CRT_SECURE_NO_WARNINGS 1#include"contact.h"//初始化通讯录void Initcontact(contact* pc){pc->sz = 0;memset(pc->data, 0, sizeof(pc->data));} void AddContact(contact* pc){if (pc->sz == MAX){printf("通讯录已满,无法添加\n");return;}//录入信息printf("请输入名字:>");scanf("%s", pc->data[pc->sz].name);printf("请输入年龄:>");scanf("%d", &(pc->data[pc->sz].age));printf("请输入性别:>");scanf("%s", pc->data[pc->sz].sex);printf("请输入电话:>");scanf("%s", pc->data[pc->sz].tel);printf("请输入地址:>");scanf("%s", pc->data[pc->sz].addr);//把数据放入下标为sz的底下pc->sz++;//不能单独 写sz++printf("添加成功\n");}void PrintContact(const contact* pc){assert(pc);int i = 0;printf("%-20s%-10d%-5s%-12s%-30s\n","姓名","年龄","性别","电话","地址");for (i = 0; i < pc->sz; i++){printf("%-20s%-10d%-5s%-12s%-30s\n",pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tel, pc->data[i].addr);}}int FindName(contact* pc, char name[]){int i = 0;for (i = 0; i < pc->sz; i++){if (0 == strcmp(name, pc->data[i].name)){return i;}}return -1;}void DelContact(contact* pc){assert(pc);if (pc->sz == 0){printf("通讯录已空,无法删除\n");return;}char name[NAME_MAX] = { 0 };printf("要删除的人>");scanf("%s", name);int pos = FindName(pc, name);if (pos ==-1){printf("要删除的人不存在\n");}int j = 0;for (j = pos; pos < pc->sz - 1; j++){pc->data[j] = pc->data[j + 1];}pc->sz--;//最后一个元素也就访问不到,这样就不会越界printf("删除成功\n");}void SearchContact(const contact* pc){char name[NAME_MAX] = { 0 };printf("要查找的人>");scanf("%s", name);int pos = FindName(pc, name);if (pos == -1){printf("要查找的人不存在\n");}printf("%-20s%-10d%-5s%-12s%-30s\n", "姓名", "年龄", "性别", "电话", "地址");printf("%-20s%-10d%-5s%-12s%-30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tel, pc->data[pos].addr);}//修改联系人void menu1(){printf("********************************\n");printf("*******1.add 2.del *********\n");printf("*******3.search4.modify*******\n");printf("*******5.sort0.exit ******\n");printf("*******6.print******************\n");printf("********************************\n");}void ModifyContact(contact* pc){int input = 0;char name[NAME_MAX] = { 0 };printf("请输入你要修改的联系人姓名:>");scanf("%s", name);//查找int pos = FindName(pc, name);int i = 0;if (pos == -1){printf("找不到所要查找的联系人\n");return;}//修改printf("请输入要修改的具体信息:>\n");menu1();scanf("%d", &input);switch (input){case 1:printf("请输入新的姓名:>");scanf("%s", pc->data[pos].name);break;case 2:printf("请输入新的年龄:>");scanf("%d", &pc->data[pos].age);break;case 3:printf("请输入新的性别:>");scanf("%s", pc->data[pos].sex);break;case 4:printf("请输入新的电话:>");scanf("%s", pc->data[pos].tel);break;case 5:printf("请输入新的地址:>");scanf("%s", pc->data[pos].addr);break;default:printf("输入有误,修改失败\n");return;}printf("修改成功\n");}
test.c
#define _CRT_SECURE_NO_WARNINGS 1#include"contact.h"void menu(){printf("********************************\n");printf("*******1.add 2.del *********\n");printf("*******3.search4.modify*******\n");printf("*******6.print0.exit *****\n");printf("********************************\n");printf("********************************\n");}void test(){int input = 0;contact con;//初始化通讯录Initcontact(&con);do{menu();printf("请选择:>");scanf("%d", &input);switch (input){case ADD:AddContact(&con);break;case DEL:DelContact(&con);break;case SEARCH:SearchContact(&con);break;case MODIFY:ModifyContact(&con);break;case PRINT:PrintContact(&con);break;case EXIT:printf("退出通讯录\n");break;default:printf("选择错误,请重新选择\n");break;}} while (input);}int main(){test();return 0;}
以上就是我们一个简单通讯录的实现,这就是我们今天的分享,谢谢大家