1.目的
写一个实用型通讯录,它有如下功能:
显示目录
void ShowMenu()
{
printf("#######################\n");
printf("#1.Add 2.Del 3.Search #\n");
printf("#4.Mod 5.Show 6.Clear #\n");
printf("#7.Sort 0.Exit #\n");
printf("#######################\n");
printf("please select#");
}
1.添加联系人 2.删除联系人 3.查找联系人 4.修改联系人 5.显示通讯录 6.清除通讯录 7.排序通讯录
2.分部流程
1.初始化通讯录
void InitContact(contact_t **ct)
{
*ct = (contact_t *)malloc(sizeof(contact_t) + INIT_NUM*sizeof(person_t));
if (*ct == NULL){
perror("malloc");
exit(1);
}
(*ct)->size = 0;
(*ct)->cap = INIT_NUM;
printf("Using Default Init!\n");
}
2.添加联系人
添加联系人人时要考虑几个问题 1.新加的联系人是否已经存在了 2.通讯录是否已经添加满了 : a.没有满,继续添加 b.满了,实现自动扩容
void AddFriend(contact_t **ct)
{
assert(ct);
if (!IsFull(*ct) || Inc(ct))
{
person_t p;
printf("请输入新增用户的姓名:");
scanf("%s", p.name);
printf("请输入新增用户的性别:");
scanf("%s", p.sex);
printf("请输入新增用户的年纪:");
scanf("%d", &(p.age));
printf("请输入新增用户的电话:");
scanf("%s", p.telphone);
printf("请输入新增用户的地址:");
scanf("%s", p.address);
if (IsExist(*ct, &p)){
printf("%s 已经存在,请不要重复插入\n", p.name);
return;
}
memcpy((*ct)->friends + (*ct)->size, &p, sizeof(p));
(*ct)->size += 1;
printf("新增用户%s成功\n", p.name);
}
else{
printf("扩容失败\n");
}
}
3.判断联系人是否存在
static int IsExist(contact_t *ct, person_t *p){
assert(ct);
assert(p);
int i = 0;
for (; i < ct->size; i++){
if (strcmp(ct->friends[i].name, p->name) == 0){
return 1;
}
}
return 0;
}
4.判断通讯录是否已满
static int IsFull(contact_t *ct)
{
return ct->cap == ct->size;
}
5.判断通讯录是否为空
static int IsEmpty(contact_t *ct)
{
return ct->size == 0;
}
6.通讯录扩容
static int Inc(contact_t **ct)
{
assert(ct);
contact_t *ct_temp = (contact_t *)realloc(*ct, sizeof(contact_t) + ((*ct)->cap + INC_SIZE)*sizeof(person_t));
if (ct_temp == NULL){
return 0;
}
*ct = ct_temp;
(*ct)->cap += INC_SIZE;
printf("自动扩容成功\n");
return 1;
}
7.核心函数
实现在通讯录里找到目标联系人,如果有此人,返回这个联系人所在数组中的索引
static int SearchCore(contact_t *ct, const char *name)
{
assert(ct);
assert(name);
int i = 0;
for (; i < ct->size; i++)
{
person_t *p = ct->friends + i;
if (strcmp(name, p->name) == 0){
return i;
}
}
return -1;
}
8.查找联系人
1.先判断是否存在这个联系人 2.存在,输出这个联系人的信息 3.不存在,数组不存此联系人
void SearchFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要查找的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
printf("|%-10s|%-10s|%-10s|%-10s|%10s|\n", "姓名", "性别", "年纪", "电话", "地址");
if (i >= 0){
person_t *p = ct->friends + i;
printf("| %-10s | %-10s | %-10d | %-10s | % 10s | \n", p->name, p->sex, p->age, p->telphone, p->address);
}
else{
printf("你要查找的人%s不存在\n", name);
}
return;
}
9.修改联系人
1.在输入想修改的联系人姓名之后,先判断这个联系人是否存在于通讯录中 2.如果存在,定义一个指针指向该联系人存在的位置
void ModFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要修改的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
person_t *p= (ct->friends + i);
printf("请输入修改用户的姓名:");
scanf("%s", p->name);
printf("请输入修改用户的性别:");
scanf("%s", p->sex);
printf("请输入修改 用户的年纪:");
scanf("%d", &(p->age));
printf("请输入修改用户的电话:");
scanf("%s", p->telphone);
printf("请输入修改用户的地址:");
scanf("%s", p->address);
memcpy((ct)->friends + (ct)->size, &p, sizeof(p));
printf("修改用户%s成功\n", p->name);
}
else{
printf("你要修改的联系人%s不存在\n",name);
}
}
10.清空通讯录
只要令size=0,就清空了
void ClearContact(contact_t *ct)
{
assert(ct);
ct->size = 0;
}
11.删除联系人
不考虑顺序问题,可直接让最后一个联系人的信息覆盖此要删除的联系人
void DelFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要删除的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
ct->friends[i] = ct->friends[ct->size - 1];
ct->size -= 1;
printf("删除成功\n");
}
else
{
printf("你要删除的人%s不存在\n", name);
}
}
12.显示通讯录
void ShowContact(contact_t *ct){
assert(ct);
int i = 0;
printf("|cap:%d|size:%d|\n", ct->cap, ct->size);
printf("|%-10s|%-10s|%-10s|%-10s|%10s|\n", "姓名", "性别", "年纪", "电话", "地址");
for (; i < ct->size; i++){
person_t *p = ct->friends + i;
printf("|%-10s|%-10s|%-10d|%-10s|%10s|\n", p->name, p->sex, p->age, p->telphone, p->address);
}
}
13.比较联系人
static int CmpPerson(const void *p1,const void *p2)
{
assert(p1);
assert(p2);
person_t *_p1 = (person_t *)p1;
person_t *_p2 = (person_t *)p2;
return strcmp(_p1->name, _p2->name);
}
14.通讯录排序
void SortContact(contact *ct)
{
assert(ct);
if (!IsEmpty(ct)){
qsort(ct->friends, ct->size, sizeof(person_t), CmpPerson);
}
}
3.总代码展示
main.c
#include "contact.h"
void ShowMenu()
{
printf("#######################\n");
printf("#1.Add 2.Del 3.Search #\n");
printf("#4.Mod 5.Show 6.Clear #\n");
printf("#7.Sort 0.Exit #\n");
printf("#######################\n");
printf("please select#");
}
int main(){
contact_t *ct = NULL;
InitContact(&ct);
int quit = 0;
while (!quit){
int select = 0;
ShowMenu();
scanf("%d", &select);
switch (select){
case 1:
AddFriend(&ct);
break;
case 2:
DelFriend(ct);
break;
case 3:
SearchFriend(ct);
break;
case 4:
ModFriend(ct);
break;
case 5:
ShowContact(ct);
break;
case 6:
ClearContact(ct);
break;
case 7:
SortContact(ct);
break;
case 0:
return 0;
default:
break;
}
}
system("pause");
return 0;
}
contact.c
#include "contact.h"
void InitContact(contact_t **ct)
{
*ct = (contact_t *)malloc(sizeof(contact_t) + INIT_NUM*sizeof(person_t));
if (*ct == NULL){
perror("malloc");
exit(1);
}
(*ct)->size = 0;
(*ct)->cap = INIT_NUM;
}
static int IsExist(contact_t *ct, person_t *p){
assert(ct);
assert(p);
int i = 0;
for (; i < ct->size; i++){
if (strcmp(ct->friends[i].name, p->name) == 0){
return 1;
}
}
return 0;
}
static int IsFull(contact_t *ct)
{
return ct->cap == ct->size;
}
static int IsEmpty(contact_t *ct)
{
return ct->size == 0;
}
static int Inc(contact_t **ct)
{
assert(ct);
contact_t *ct_temp = (contact_t *)realloc(*ct, sizeof(contact_t) + ((*ct)->cap + INC_SIZE)*sizeof(person_t));
if (ct_temp == NULL){
return 0;
}
*ct = ct_temp;
(*ct)->cap += INC_SIZE;
printf("自动扩容成功\n");
return 1;
}
void AddFriend(contact_t **ct)
{
assert(ct);
if (!IsFull(*ct) || Inc(ct))
{
person_t p;
printf("请输入新增用户的姓名:");
scanf("%s", p.name);
printf("请输入新增用户的性别:");
scanf("%s", p.sex);
printf("请输入新增用户的年纪:");
scanf("%d", &(p.age));
printf("请输入新增用户的电话:");
scanf("%s", p.telphone);
printf("请输入新增用户的地址:");
scanf("%s", p.address);
if (IsExist(*ct, &p)){
printf("%s 已经存在,请不要重复插入\n", p.name);
return;
}
memcpy((*ct)->friends + (*ct)->size, &p, sizeof(p));
(*ct)->size += 1;
printf("新增用户%s成功\n", p.name);
}
else{
printf("扩容失败\n");
}
}
static int SearchCore(contact_t *ct, const char *name)
{
assert(ct);
assert(name);
int i = 0;
for (; i < ct->size; i++)
{
person_t *p = ct->friends + i;
if (strcmp(name, p->name) == 0){
return i;
}
}
return -1;
}
void SearchFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要查找的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
printf("|%-10s|%-10s|%-10s|%-10s|%10s|\n", "姓名", "性别", "年纪", "电话", "地址");
if (i >= 0){
person_t *p = ct->friends + i;
printf("| %-10s | %-10s | %-10d | %-10s | % 10s | \n", p->name, p->sex, p->age, p->telphone, p->address);
}
else{
printf("你要查找的人%s不存在\n", name);
}
return;
}
void ModFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要修改的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
person_t *p= (ct->friends + i);
printf("请输入修改用户的姓名:");
scanf("%s", p->name);
printf("请输入修改用户的性别:");
scanf("%s", p->sex);
printf("请输入修改 用户的年纪:");
scanf("%d", &(p->age));
printf("请输入修改用户的电话:");
scanf("%s", p->telphone);
printf("请输入修改用户的地址:");
scanf("%s", p->address);
memcpy((ct)->friends + (ct)->size, &p, sizeof(p));
printf("修改用户%s成功\n", p->name);
}
else{
printf("你要修改的联系人%s不存在\n",name);
}
}
void ClearContact(contact_t *ct)
{
assert(ct);
ct->size = 0;
}
void DelFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要删除的人的姓名#");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
ct->friends[i] = ct->friends[ct->size - 1];
ct->size -= 1;
printf("删除成功\n");
}
else
{
printf("你要删除的人%s不存在\n", name);
}
}
void ShowContact(contact_t *ct){
assert(ct);
int i = 0;
printf("|cap:%d|size:%d|\n", ct->cap, ct->size);
printf("|%-10s|%-10s|%-10d|%-10s|%10s|\n", "姓名", "性别", "年纪", "电话", "地址");
for (; i < ct->size; i++){
person_t *p = ct->friends + i;
printf("|%-10s|%-10s|%-10d|%-10s|%10s|\n", p->name, p->sex, p->age, p->telphone, p->address);
}
}
static int CmpPerson(const void *p1,const void *p2)
{
assert(p1);
assert(p2);
person_t *_p1 = (person_t *)p1;
person_t *_p2 = (person_t *)p2;
return strcmp(_p1->name, _p2->name);
}
void SortContact(contact *ct)
{
assert(ct);
if (!IsEmpty(ct)){
qsort(ct->friends, ct->size, sizeof(person_t), CmpPerson);
}
}
contact.h
#ifndef _CINTACT_H_
#define _CONTACT_H_
#define NAME_SIZE 32
#define SEX_SIZE 8
#define TELPHONE_SIZE 16
#define ADDRESS_SIZE 128
#include <stdio.h>
#include <Windows.h>
#include <assert.h>
#pragma warning(disable:4996)
#define INIT_NUM 1000
#define INC_SIZE 5
#define SAVE_FILE "save.txt"
typedef struct person{
char name[NAME_SIZE];
char sex[SEX_SIZE];
int age;
char telphone[TELPHONE_SIZE];
char address[ADDRESS_SIZE];
}person_t;
typedef struct contact{
FILE *save;
int cap;
int size;
person_t friends[0];
}contact_t;
void InitContact(contact_t **ct);
void AddFriend(contact_t **ct);
void ShowContact(contact_t *ct);
void ModFriend(contact_t *ct);
void SearchFriend(contact_t *ct);
void ClearContact(contact_t *ct);
void DelFriend(contact_t *ct);
void SortContact(contact_t *ct);
#endif
|