加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
addressboob.c 2.55 KB
一键复制 编辑 原始数据 按行查看 历史
谭亚飞 提交于 2021-10-31 20:54 . 通讯录
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
typedef struct personInfo {
char name[100];
char phone_num[20];
}personInfo;
typedef struct addressBook {
personInfo per[1024];
int size;
}aBook;
aBook abook;
void initaddressBook(aBook* abook) {
abook->size = 0;
memset(abook->per, 0, sizeof(abook->per));
}
int menu() {
printf("-------通讯录-------\n");
printf(" 1.添加联系人 \n");
printf(" 2.查看联系人\n");
printf(" 3.修改联系人\n");
printf(" 4.删除联系人\n");
printf(" 0.退出 \n");
printf("请输入你的选择:");
int choice = 0;
scanf("%d", &choice);
return choice;
}
void add(aBook* abook) {
assert(abook != NULL);
if (abook->size > 1024) {
printf("新增联系人失败");
return;
}
personInfo person = { 0 };
printf("请输入联系人的姓名:");
scanf("%s", person.name);
printf("请输入联系人的电话:");
scanf("%s", person.phone_num);
abook->per[abook->size] = person;
abook->size++;
printf("新增联系人完成\n");
}
void show(aBook* abook ) {
//这里最好用指针表示元素,
assert(abook != NULL);
for (int i = 0; i < abook->size; i++) {
personInfo* p = &abook->per[i];
printf("%d %s %s\n",i,p->name,p->phone_num);
}
}
void modify(aBook* abook) {
assert(abook != NULL);
printf("请输入要修改的序号");
int id = 0;
scanf("%d", &id);
if (id < 0 || id >= abook->size) {
printf("你输入的序号有误");
return;
}
personInfo* p = &abook->per[id];
char name[100] = {0};
char num[20] = {0};
printf("输入*表示该选项不可修改\n");
printf("请输入新的名字(旧的用户名为%s)",p->name);
scanf("%s", name);
if (strcmp(name, "*" ) != 0) {
strcpy(p->name, name);
}
printf("请输入新的手机号(旧的手机号为%s)", p->phone_num);
scanf("%s", num);
if (strcmp(num, "*" ) != 0) {
strcpy(p->phone_num, num);
}
printf("修改成功");
}
void drop(aBook* abook) {
assert(abook != NULL);
printf("请输入要删除的序号:");
int todelete = 0;
scanf("%d", &todelete);
if (todelete < 0 || todelete >= abook->size) {
printf("你输入的序号有误");
return;
}
if (todelete == abook->size - 1) {
abook->size--;
}
else {
abook->per[todelete] = abook->per[abook->size - 1];
abook->size--;
}
printf("删除联系人完成");
}
typedef void(*Func)(aBook*);
void main() {
initaddressBook(&abook);
Func funcs[] = { NULL,add,show,modify,drop};
while (1) {
int choice = menu();
if (choice < 0 || choice>sizeof(funcs) / sizeof(funcs[0])) {
printf("您的输入有误请重新输入");
continue;
}
if (choice == 0) {
printf("已退出");
break;
}
funcs[choice](&abook);
system("pause");
system("cls");
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化