1. 用结构体描述一个 通讯录
2.每个人的信息包括姓名住址
代码效果图
菜单
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8b264569cc6c4f2ff88f0f3600afee27.png)
增
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/00d870b0fe589d5c4d171e1bb877a6e8.png)
删
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cf52c4e0601dba09bc65610e86732f5f.png)
查
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1a82a8f15e3e9ce96be5813e21285087.png)
改
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8e715775d33a141c749816923b134778.png)
排序
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c952eaac041acb114e0c2474ee1ea72c.png)
清空列表
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0c975a6ac08144f5711da7b1ee05619f.png)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_PERSONINFO_SIZE 1000
typedef void(*Func)();
//1.描述信息
typedef struct People_Info{
char name[1024];
char phone_number[1024];
}People_Info;
//2.组织大量的数据 增删查改
//用数组表示IUYTREWQ
typedef struct AddressBook {
People_Info person[MAX_PERSONINFO_SIZE];
//定义size,记录存取了多少个信息
//初始的size = 0
int size;
}AddressBook;
//定义全局变量
AddressBook g_address_book;
//初始化通讯录
void init() {
int i;
//size [ )
g_address_book.size = 0;
for(i = 0;i<MAX_PERSONINFO_SIZE;i++) {
g_address_book.person[i].name[0] = '\0';
g_address_book.person[i].phone_number[0] = '\0';
}
}
//菜单
int menu() {
int choice;
printf("------------------\n");
printf("----1.添加联系人--\n");
printf("----2.删除联系人--\n");
printf("----3.查找联系人--\n");
printf("----4.修改联系人--\n");
printf("----5.显示所有联系人\n");
printf("----6.清空联系人--\n");
printf("----7.排序联系人--\n");
printf("----8.保存联系人--\n");
printf("----9.加载联系人--\n");
printf("----0.退出程序----\n");
printf("------------------\n");
//printf("请输入你的选择:");
//scanf("%d",&choice);
/*return choice;*/
}
//给转移表凑数
void empty(){
}
//1. 添加联系人信息
void add_linkman() {
//通过指针间修改
People_Info* people_Info = &g_address_book.person[g_address_book.size];
if(g_address_book.size>=MAX_PERSONINFO_SIZE) {
printf("新增联系人失败!\n");
return;
}
printf("新增联系人:\n");
printf("输入联系人的姓名:");
scanf("%s",people_Info->name);
printf("输入联系人的号码:");
scanf("%s",people_Info->phone_number);
g_address_book.size++;
printf("新增联系人成功!\n");
}
//2. 删除指定联系人信息
void delete_linkman () {
int number = 0;
if(g_address_book.size<=0) {
printf("删除失败通讯录为空!\n");
return ;
}
printf("删除指定联系人:\n");
printf("请输入要删除的序号:");
scanf("%d",&number);
if(number<0||number>g_address_book.size) {
printf("您输入序号的有误!\n");
return;
}
g_address_book.person[number] =
g_address_book.person[g_address_book.size-1];
--g_address_book.size;
printf("删除联系人成功!\n");
}
//3. 查找指定联系人信息
void search_linkman() {
char search_name[1024];
int i;
int j = 0;
printf("查找联系人信息:\n");
printf("请输入联系人的姓名:");
scanf("%s",search_name);
printf("查找联系人如下:\n");
for(i = 0;i<g_address_book.size;i++) {
if(strcmp(g_address_book.person[i].name,search_name) == 0) {
printf("第[%d]位联系人 :%s,%s \n",i,g_address_book.person[i].name,
g_address_book.person[i].phone_number);
j++;
}
}
if(j==0) {
printf("查无此人!\n");
}
}
//4. 修改指定联系人信息
void revise_linkman() {
int number;
char rev_name[1024];
char rev_phone_number[1024];
People_Info* people_Info = &g_address_book.person[g_address_book.size];
printf("修改联系人信息\n");
printf("请输入修改人序号:");
scanf("%d",&number);
if(number<0||number>=g_address_book.size) {
printf("输入有误重新输入!\n");
return;
}
printf("输入修改联系人姓名:");
scanf("%s",rev_name);
printf("请输入修改联系人电话:");
scanf("%s",rev_phone_number);
if(number == '*') {
strcpy(g_address_book.person[number].name,rev_name);
}
else if(*rev_name == '*') {
strcpy(g_address_book.person[number].phone_number,rev_phone_number);
}
else {
strcpy(g_address_book.person[number].name,rev_name);
strcpy(g_address_book.person[number].phone_number,rev_phone_number);
}
printf("修改联系人信息成功!\n");
}
//5. 显示所有联系人信息
void show_all_linkman() {
int i;
printf("显示所有联系人:\n");
for(i = 0;i<g_address_book.size;i++) {
printf("第[%d]位联系人 :%s,%s \n",i,g_address_book.person[i].name,
g_address_book.person[i].phone_number);
}
printf("共打印了%d条数据\n",g_address_book.size);
printf("显示所有联系人成功!\n");
}
//6. 清空所有联系人
void empty_all_linkman() {
char empty_char[1024];
printf("您是否进行清空联系人操作:Y/N\n");
scanf("%s",&empty_char);
if(strcmp (empty_char,"Y") == 0) {
printf("此项操作极为危险请您再次确认! Y/N\n");
scanf("%s",&empty_char);
if(strcmp (empty_char,"Y") == 0) {
init();
printf("清除联系人成功!\n");
}
else return;
}
else return;
}
//7. 以名字排序所有联系人
void sort_all_linkman() {
char temp_name[1024]={'\0'};
char temp_phone_number[1024]={'\0'};
int bound;
int i;
int size;
//冒泡排序法
for(bound = 0;bound<g_address_book.size-1;bound++) {
for(size = 0;size<g_address_book.size-bound-1;size++) {
if(strcmp (g_address_book.person[size].name,g_address_book.person[size+1].name)>0) {
strcpy(temp_name,g_address_book.person[size].name);
strcpy(g_address_book.person[size].name,g_address_book.person[size+1].name);
strcpy(g_address_book.person[size+1].name,temp_name);
//copy 电话号码
strcpy(temp_phone_number,g_address_book.person[size].phone_number);
strcpy(g_address_book.person[size].phone_number,g_address_book.person[size+1].phone_number);
strcpy(g_address_book.person[size+1].phone_number,temp_phone_number);
}
}
}
printf("排序所有联系人:\n");
printf("排序成功!\n");
}
//8. 保存联系人到文件
void conserve_all_linkman() {
FILE *fp;
int i;
printf("保存联系人到文件!\n");
if((fp = fopen("person_info.txt","w")) == NULL) {
printf("can not open file!\n");
return ;
}
for(i = 0;i<g_address_book.size;i++) {
fprintf (fp,"[%d] %s,%s\r\n",i,
g_address_book.person[i].name,g_address_book.person[i].phone_number);
}
fclose(fp);
printf("保存联系人成功!\n");
}
//9. 加载联系人
void load_linkman() {
FILE *fp;
int i;
char ch;
printf("加载联系人到文件!\n");
if((fp = fopen("person_info.txt","r")) == NULL) {
printf("can not open file!\n");
return ;
}
while(!feof(fp)) {
ch = fgetc(fp);
putchar(ch);
}
fclose(fp);
printf("加载联系人成功!\n");
}
int main() {
int choose;
//转移表
Func choice[] = { empty,
add_linkman,
delete_linkman,
search_linkman,
revise_linkman,
show_all_linkman,
empty_all_linkman,
sort_all_linkman,
conserve_all_linkman,
load_linkman
};
//初始化
init();
while(1) {
menu();
printf("请输入你的选择:");
scanf("%d",&choose);
//条件判定
if(choose<0|| choose>=sizeof(choice)/sizeof(choice[0])) {
printf("您的输入有误!重新输入!\n");
continue;
}
if(choose == 0){
printf("good-bye!\n");
break;
}
choice[choose]();
}
return 0;
}