代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。