頭文件Contact.h
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#pragma once
#define MAX 100
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30//表示一個人的信息
//struct PeoInfo
//{
// char name[20];
// int age;
// char sex[5];
// char tele[12];
// char addr[30];
//};
typedef struct PeoInfo
{char name[MAX_NAME];int age;char sex[MAX_SEX];char tele[MAX_TELE];char addr[MAX_ADDR];
}PeoInfo;typedef struct Contact
{PeoInfo data[MAX];//存放數據int sz;//記錄通訊錄中的有效信息個數
}Contact;//初始化通訊錄
void InitContact(Contact* pc);//增加指定聯系人
void AddContact(Contact* pc);//顯示聯系人信息
void ShowContact(const Contact* pc);//刪除指定聯系人
void DelContact(Contact* pc);//查找指定聯系人
void SearchContact(const Contact* pc);//修改通訊錄
void ModifyContact(Contact* pc);//排序通訊錄元素
void SortContact(Contact* pc);
測試文件test.c
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"contact.h"
void menu()
{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");printf("*****************************\n");
}
enum Option
{EXIT,ADD,DEL,SEARCH,MODIFY,SHOW,SORT
};
int main()
{int input = 0;Contact con;//???//????InitContact(&con);do{menu();printf("?:>");scanf("%d", &input);switch (input){case ADD:AddContact(&con);break;case DEL:DelContact(&con);break;case SEARCH:SearchContact(&con);break;case MODIFY:ModifyContact(&con);break;case SHOW:ShowContact(&con);break;case SORT:SortContact(&con);break;case EXIT:printf("????\n");break;default:printf("?\n");break;}} while (input);return 0;
}
功能實現文件Contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"void InitContact(Contact* pc)
{pc->sz = 0;memset(pc->data, 0, sizeof(pc->data));
}void AddContact(Contact* pc)
{if (pc->sz == MAX){printf("通訊錄已滿,無法增加\n");return;}printf("請輸入名字:>");scanf("%s", pc->data[pc->sz].name);printf("請輸入年齡:>");scanf("%d", &(pc->data[pc->sz].age));printf("請輸入性別:>");scanf("%s", pc->data[pc->sz].sex);printf("請輸入電話:>");scanf("%s", pc->data[pc->sz].tele);printf("請輸入地址:>");scanf("%s", pc->data[pc->sz].addr);pc->sz++;printf("添加成功\n");
}void ShowContact(const Contact* pc)
{int i = 0;//姓名 年齡 性別 電話 地址//zhangsan 20 男 123456 北京//打印標題printf("%-10s %-4s %-5s %-12s %-30s\n ", "姓名", "年齡", "性別", "電話", "地址");for (i = 0;i < pc->sz;i++){printf("%s %d %s %s %s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex,pc->data[i].tele, pc->data[i].addr);}
}static int FindByName(const Contact* pc, char name[])
{int i = 0;int pos = 0;for (i = 0;i < pc->sz;i++){if (0 == strcmp(pc->data[i].name, name)){return i;}}/*if (i == pc->sz){return -1;}*/return -1;
}
void DelContact(Contact* pc)
{char name[MAX_NAME] = { 0 };if (pc->sz == 0){printf("通訊錄為空,無法刪除\n");return;}//刪除//1.找到要刪除的人 - 位置 (下標)printf("輸入要刪除人的名字:>");scanf("%s", name);int pos = FindByName(pc, name);if (pos == -1){printf("要刪除的人不存在\n");return; }int i = 0;//2.刪除 - 刪除pos位置上的數據 for (i = pos;i<pc->sz-1;i++){pc->data[i] = pc->data[i + 1];}pc->sz--;printf("刪除成功\n");
}void SearchContact(const Contact* pc)
{char name[MAX_NAME] = { 0 };printf("請輸入要查找人的名字:>");scanf("%s", name);int pos = FindByName(pc, name);if (pos == -1){printf("要查找的人不存在\n");return;}//打印printf("%-10s %-4s %-5s %-12s %-30s\n ", "姓名", "年齡", "性別", "電話", "地址");//打印數據printf("%s %d %s %s %s\n",pc->data[pos].name,pc->data[pos].age,pc->data[pos].sex,pc->data[pos].tele,pc->data[pos].addr);
}void ModifyContact(Contact* pc)
{char name[MAX_NAME] = { 0 };printf("輸入要修改人的名字\n");scanf("%s", name); int pos = FindByName(pc, name);if (pos == -1){printf("要修改的人不存在\n");}//修改printf("請輸入名字:>");scanf("%s", pc->data[pos].name);printf("請輸入年齡:>");scanf("%d", &(pc->data[pos].age));printf("請輸入性別:>");scanf("%s", pc->data[pos].sex);printf("請輸入電話:>");scanf("%s", pc->data[pos].tele);printf("請輸入地址:>");scanf("%s", pc->data[pos].addr);printf("修改成功\n");
}//按照名字來排序
int cmp_by_name(const void* e1, const void* e2)
{return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{qsort(pc->data,pc->sz,sizeof(PeoInfo), cmp_by_name);
}