将下面的三个代码按照下面的形式拉入就可以正常运行。
test.c
主体结构
#include "contact.h" void mun() { printf("**************************************************\n"); printf("*** 1.添加通讯录成员 2.展示通讯录 *********\n"); printf("*** 3.查询成员 4.删除通讯录成员 *****\n"); printf("*** 5.排序 0.退出通讯录 *********\n"); printf("**************************************************\n"); } int main() { int input = 0; struct Contact con; Init_Contact(&con); do { mun(); printf("请输入=>"); scanf("%d", &input); switch (input) { case 1: AddFolk(&con); break; case 2: ShowContact(&con); break; case 3: SearchContact(&con); break; case 4: DelContact(&con); break; case 5: Sort(&con); break; case 0: printf("退出通讯录\n"); break; default: printf("输入错误\n"); break; } } while (input); return 0; }
contact.c
封装的函数
#include "contact.h" //初始化通讯录 void Init_Contact(struct Contact* pc) { pc->sz = 0; memset(pc->data,0, MAX_FOKL *sizeof(struct ContactFolk)); } //添加成员 void AddFolk(struct Contact* pc) { if (pc->sz == MAX_FOKL) { printf("通讯已满\n"); return; } else { printf("请输入姓名\n"); scanf("%s", pc->data[pc->sz].name); printf("请输入性别\n"); scanf("%s", pc->data[pc->sz].sex); printf("请输入年龄\n"); scanf("%s", pc->data[pc->sz].age); printf("请输入电话号\n"); scanf("%s", pc->data[pc->sz].tele); //scanf("%-20s\t%-10s\t%-5d\t%-20s\t", pc->data[pc->sz].name,pc->data[pc->sz].sex,pc->data[pc->sz].age,pc->data[pc->sz].tele); printf("添加成功\n"); pc->sz++; } } //展示通讯录 void ShowContact(struct Contact* pc) { int i = 0; printf("%-20s\t%-10s\t%-5s\t%-20s\t\n", "姓名", "性别", "年龄", "电话"); for (i = 0; i <pc->sz; i++) { printf("%-20s\t%-10s\t%-5s\t%-20s\n", pc->data[i].name, pc->data[i].sex, pc->data[i].age, pc->data[i].tele); } } static int FindByName(const struct Contact* pc,const char n[]) { int i = 0; for (i = 0; i <pc->sz ; i++) { if (strcmp(pc->data[i].name, n) == 0) { printf("查有此人\n"); return i; } } return -1; } //查询 void SearchContact(const struct Contact* pc) { char name[MAX_NAME]; printf("请输入名字->"); scanf("%s", &name); int ret = FindByName(pc,name); if (ret == -1) { printf("查无此人\n"); } else { printf("%-20s\t%-10s\t%-5s\t%-20s\t\n", "姓名", "性别", "年龄", "电话"); printf("%-20s\t%-10s\t%-5s\t%-20s\n", pc->data[ret].name, pc->data[ret].sex, pc->data[ret].age, pc->data[ret].tele); } } //删除通讯录成员 void DelContact(struct Contact* pc) { char name[MAX_NAME]; printf("请输入名字->"); scanf("%s", &name); int ret = FindByName(pc, name); if (ret == -1) { printf("查无此人\n"); } else { memmove(&pc->data[ret], &pc->data[ret+1],(pc->sz-ret+1)*sizeof(pc->data[0])); } } static int CmpByAge(const void* e1, const void* e2) { return *(int*)e1-*(int*)e2; } //排序 void Sort(struct Contact* pc) { qsort(pc->data, pc->sz, sizeof(struct ContactFolk), CmpByAge); printf("排序成功\n"); }
contact.h
函数头文件
#pragma once #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_NAME 20 #define MAX_SEX 10 #define MAX_AGE 5 #define MAX_TELE 20 #define MAX_FOKL 100 //修改通讯录存储大小 //创建通讯录人员 struct ContactFolk { char name[MAX_NAME]; char sex[MAX_SEX]; char age[MAX_SEX]; char tele[MAX_TELE]; }; //创建通讯录 struct Contact { struct ContactFolk data[MAX_FOKL]; int sz;//数据的位数 }; //初始化通讯录 void Init_Contact(struct Contact* con); //添加成员 void AddFolk(struct Contact* con); //展示通讯录 void ShowContact(struct Contact* con); //查询成员 void SearchContact(struct Contact* con); //删除成员 void DelContact(struct Contact* con); //排序 void Sort(struct Contact* pc);
