test.c(测试逻辑)
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"int main()
{int input = 0;Contact con;InitContact(&con);void (*function[])(Contact*) = { AddContact,DelContact,SearchContact,ModifyContact,ShowContact,SortContact };do{menu();if (scanf("%d", &input) == 1){if (input > 0 && input <= 6){function[input - 1](&con);}else if (input == 0){printf("退出程序\n");}else{printf("输入错误\n");}}else{printf("输入错误\n");while (getchar() != '\n');input = -1;}} while (input);return 0;
}
contact.c(功能实现)
#define _CRT_SECURE_NO_WARNINGS
#include "contact.h"void menu(void)
{printf("******************************************\n");printf("****** 1.add 2.del ******\n");printf("****** 3.search 4.modify ******\n");printf("****** 5.show 6.sort ******\n");printf("****** 0.Exit ******\n");printf("******************************************\n");
}void InitContact(Contact* con)
{memset(con, 0, sizeof(Contact));
}void AddContact(Contact* con)
{assert(con);if (con->count <= MAX){printf("请输入姓名 -> ");scanf("%s", con->contacts[con->count].name);printf("请输入性别 -> ");scanf("%s", con->contacts[con->count].sex);printf("请输入年龄 -> ");scanf("%d", &con->contacts[con->count].age);printf("请输入电话 -> ");scanf("%11s", con->contacts[con->count].tele);printf("请输入地址 -> ");scanf("%s", con->contacts[con->count].address);con->count++;}else{printf("通讯录已满\n");}
}void ShowContact(Contact* con)
{assert(con);if (con->count > 0){printf("%-8s\t%-4s\t%-4s\t%-11s\t%-25s\n", "姓名", "性别", "年龄", "电话", "地址");int i = 0;for (i = 0; i < con->count; i++){printf("%-8s\t%-4s\t%-4d\t%-11s\t%-25s\n", con->contacts[i].name,con->contacts[i].sex,con->contacts[i].age,con->contacts[i].tele,con->contacts[i].address);}}else{printf("通讯录为空\n");}
}static int Search(Contact* con, char* str)
{assert(con);int i = 0;for (i = 0; i < con->count; i++){if ((strcmp(str, con->contacts[i].name) == 0) || (strcmp(str, con->contacts[i].tele) == 0)){return i;}}return -1;
}void DelContact(Contact* con)
{assert(con);if (con->count > 0){int ret = 0;int i = 0;char tmp_str[NAME_max] = { 0 };printf("请输入姓名或电话:>");scanf("%s", tmp_str);ret = Search(con, tmp_str);if (ret == -1){printf("没有这个人\n");}else {for (i = ret; i < con->count - 1; i++){con->contacts[i] = con->contacts[i + 1];}con->count--;printf("删除成功\n");}}else{printf("联系人为空\n");}
}void SearchContact(Contact* con)
{assert(con);if (con->count > 0){int ret = 0;char tmp_str[NAME_max] = { 0 };printf("请输入姓名或电话:>");scanf("%s", tmp_str);ret = Search(con, tmp_str);if (ret == -1){printf("没有这个人\n");}else{printf("%-8s\t%-4s\t%-4s\t%-11s\t%-25s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%-8s\t%-4s\t%-4d\t%-11s\t%-25s\n", con->contacts[ret].name,con->contacts[ret].sex,con->contacts[ret].age,con->contacts[ret].tele,con->contacts[ret].address);}}else{printf("联系人为空\n");}
}void ModifyContact(Contact* con)
{assert(con);if (con->count > 0){int ret = 0;char tmp_str[NAME_max] = { 0 };printf("请输入姓名或电话:>");scanf("%s", tmp_str);ret = Search(con, tmp_str);if (ret == -1){printf("没有这个人\n");}else{char input = 0;do{printf("%-8s\t%-4s\t%-4s\t%-11s\t%-25s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%-8s\t%-4s\t%-4d\t%-11s\t%-25s\n", con->contacts[ret].name,con->contacts[ret].sex,con->contacts[ret].age,con->contacts[ret].tele,con->contacts[ret].address);printf(" 请选择要修改的信息:\n");printf("******************************************\n");printf("****** 1.姓名 2.性别 ******\n");printf("****** 3.年龄 4.电话 ******\n");printf("****** 5.地址 6.全部 ******\n");printf("****** 0.取消 ******\n");printf("******************************************\n");while ((input = getchar()) == '\n');switch (input){case '1':printf("修改姓名为:> ");scanf("%s", con->contacts[ret].name);break;case '2':printf("修改性别为:> ");scanf("%s", con->contacts[ret].sex);break;case '3':printf("修改年龄为:> ");scanf("%d", &con->contacts[ret].age);break;case '4':printf("修改电话为:> ");scanf("%11s", con->contacts[ret].tele);break;case '5':printf("修改地址为:> ");scanf("%s", con->contacts[ret].address);break;case '6':printf("修改姓名为:> ");scanf("%s", con->contacts[ret].name);printf("修改性别为:> ");scanf("%s", con->contacts[ret].sex);printf("修改年龄为:> ");scanf("%d", &con->contacts[ret].age);printf("修改电话为:> ");scanf("%11s", con->contacts[ret].tele);printf("修改地址为:> ");scanf("%s", con->contacts[ret].address);break;case '0':printf("取消\n");break;default:printf("输入错误\n");break;}if (input >= '1' && input <= '6'){printf("%-8s\t%-4s\t%-4s\t%-11s\t%-25s\n", "姓名", "性别", "年龄", "电话", "地址");printf("%-8s\t%-4s\t%-4d\t%-11s\t%-25s\n", con->contacts[ret].name,con->contacts[ret].sex,con->contacts[ret].age,con->contacts[ret].tele,con->contacts[ret].address);printf("修改成功!\n");}} while (input < '0' || input>'6');}}else{printf("联系人为空\n");}
}//根据姓名排序
static int cmp_by_name(const void* e1, const void* e2)
{return strcmp(((PeoInfor*)e1)->name, ((PeoInfor*)e2)->name);
}
//根据年龄排序
static int cmp_by_age(const void* e1, const void* e2)
{return ((PeoInfor*)e1)->age - ((PeoInfor*)e2)->age;
}
void SortContact(Contact* con)
{int input = 0;int (*cmp)(const void*, const void*);int (*ArrFun[])(const void*, const void*) = { cmp_by_name , cmp_by_age };do{printf("******************************************\n");printf("****** 1.根据姓名排序 ******\n");printf("****** 2.根据年龄排序 ******\n");printf("****** 0. 取消 ******\n");printf("******************************************\n");if (scanf("%d", &input) == 1){if (input == 1 || input == 2){cmp = ArrFun[input - 1];qsort(con->contacts, con->count, sizeof(con->contacts[0]), cmp);printf("排序完成!\n");}else if (input == 0){printf("取消排序\n");}else{printf("输入错误\n");}}else{printf("输入错误\n");while (getchar() != '\n');input = -1;}} while (input < 0 || input > 2);
}
contact.h(声明部分)
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
#define MAX 100
#define NAME_max 20
#define TELE_max 12
#define SEX_max 10
#define ADDRESS_max 30typedef struct PeopleInformation
{int age;char name[NAME_max];char tele[TELE_max];char sex[SEX_max];char address[ADDRESS_max];
}PeoInfor;typedef struct Contact
{PeoInfor contacts[MAX];size_t count;
}Contact;//打印菜单
void menu(void);//初始化通讯录
void InitContact(Contact* con);//添加联系人
void AddContact(Contact* con);//查看联系人
void ShowContact(Contact* con);//查找联系人
void SearchContact(Contact* con);//删除联系人
void DelContact(Contact* con);//修改联系人
void ModifyContact(Contact* con);//通讯录排序
void SortContact(Contact* con);