实现一个简单的静态通讯录可以用来存储有限个人的信息
其中每个人的信息包括:姓名、性别、年龄、电话、住址
具体实现成员的添加、删除、查找、修改、显示、清空、排序等功能
根据要求,分别创建三个文件(contact.h<头文件>,contact.c<具体函数实现>,test.c<函数测试>)
contact.h
#ifndef __CONTACT_H__
#define __CONTACT_H__
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 15
#define ADDER_MAX 30
#include<stdio.h>
#include<assert.h>
#include<string.h>
typedef struct PeoInfo//定义个人信息结构体
{
char name[NAME_MAX];
long int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDER_MAX];
}PeoInfo;
typedef struct Contact
{
PeoInfo data[MAX];
int sz;//统计放入的个数
}Contact, *pContact;
void InitContact(pContact pcon);//结构体初始化
void AddContact(pContact pcon);
int SearchContact(pContact pcon);
void DelContact(pContact pcon);
void ModifyContact(pContact pcon);
void ShowContact(const pContact pcon);
void SortContact(pContact pcon);
void ClearContact(pContact pcon);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
enum
{
Exit,
Add,
Del,
Search,
Modify,
Show,
Sort,
Clear
};
void menu()
{
printf("########### 0.exit ###########\n");
printf("########### 1.add ###########\n");
printf("########### 2.del ###########\n");
printf("########### 3.search ###########\n");
printf("########### 4.modify ###########\n");
printf("########### 5.show ###########\n");
printf("########### 6.sort ###########\n");
printf("########### 7.clear ###########\n");
}
void test()
{
Contact my_con;
InitContact(&my_con);//初始化通讯录
int input = 0;
do
{
menu();
printf("请选择>: ");
scanf("%d", &input);
switch (input)
{
case Exit:
break;
case Add:
AddContact(&my_con);//增加联系人
break;
case Del:
DelContact(&my_con);//删除联系人
break;
case Search:
SearchContact(&my_con);//查询联系人
break;
case Modify:
ModifyContact(&my_con);//修改联系人
break;
case Show:
ShowContact(&my_con);//显示联系人
break;
case Sort:
SortContact(&my_con);//对联系人排序,按名字排序
break;
case Clear:
ClearContact(&my_con);//清空通讯录
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void InitContact(pContact pcon)
{
memset(pcon->data, 0, MAX*sizeof(PeoInfo));
pcon->sz = 0;
}
void AddContact(pContact pcon)
{
assert(pcon->data);
if (pcon->data == MAX)
{
printf("通讯录已满\n");
}
printf("请输入名字>: ");
scanf("%s", pcon->data[pcon->sz].name);
printf("请输入性别>: ");
scanf("%s", pcon->data[pcon->sz].sex);
printf("请输入年龄>: ");
scanf("%d", &pcon->data[pcon->sz].age);
printf("请输入电话>: ");
scanf("%s", pcon->data[pcon->sz].tele);
printf("请输入地址>: ");
scanf("%s", pcon->data[pcon->sz].addr);
pcon->sz++;
printf("录入成功\n");
}
static int FindEntry(pContact pcon, char name[])//查找函数入口
{
int i = 0;
for (i = 0; i < pcon->sz; i++)
{
if (strcmp(pcon->data[i].name, name) == 0)
{
return i;//找到,返回下标
}
}
return -1;//没找到
}
int SearchContact(pContact pcon)//查找信息
{
char name[NAME_MAX];
printf("请输入要查找的姓名: ");
scanf("%s", name);
int pos = 0;
pos = FindEntry(pcon, name);
if (pos == -1)
{
printf("你要查找的人不存在\n");
return;
}
else
{
printf("name:%10s\t sex:%5s\t age:%5d\t tele:%10s\t addr:%15s\n",
pcon->data[pos].name,
pcon->data[pos].sex,
pcon->data[pos].age,
pcon->data[pos].tele,
pcon->data[pos].addr);
}
}
void ShowContact(pContact pcon)
{
printf("联系人信息:\n");
int i = 0;
printf("%10s\t%5s\t%5s\t%15s\t%20s\n", "name", "sex", "age", "tele", "addr");
for (i = 0; i < pcon->sz; i++)
{
printf("%10s\t%5s\t%5d\t%10s\t%15s\n",
pcon->data[i].name,
pcon->data[i].sex,
pcon->data[i].age,
pcon->data[i].tele,
pcon->data[i].addr);
}
}
void DelContact(pContact pcon)//删除联系人
{
char name[NAME_MAX];
printf("请输入要删除的联系人\n");
scanf("%s", name);
int pos = 0;//利用pos接收FindEntry返回的地址
pos = FindEntry(pcon, name);
if (pos == -1)
{
printf("要删除的联系人不存在");
return;
}
else
{
int j = 0;
for (j = pos; j < pcon->sz; j++)
{
pcon->data[j] = pcon->data[j + 1];//利用后面一个覆盖前一个,从而达到删除的效果
}
pcon->sz--;
printf("删除成功\n");
}
}
void ModifyContact(pContact pcon)
{
char name[NAME_MAX];
printf("请输入要修改的联系人\n");
scanf("%s", name);
int pos = 0;//利用pos接收FindEntry返回的地址
pos = FindEntry(pcon, name);
if (pos != -1)
{
printf("请输入修改后的姓名: ");
scanf("%s", pcon->data[pcon->sz].name);
printf("请输入修改后的性别: ");
scanf("%s", pcon->data[pcon->sz].sex);
printf("请输入修改后的年龄: ");
scanf("%d", &pcon->data[pcon->sz].age);
printf("请输入修改后的电话: ");
scanf("%s", pcon->data[pcon->sz].tele);
printf("请输入修改后的地址: ");
scanf("%s", pcon->data[pcon->sz].addr);
printf("修改完毕\n");
}
else
{
printf("要修改的联系人不存在!\n");
}
}
void SortContact(pContact pcon)
{
int i = 0;
int j = 0;
int k = 0;
for (i = 0; i < pcon->sz - 1; i++)
{
for (j = 0; j<pcon->sz - i - 1; j++)
{
if (strcmp(pcon->data[i].name, pcon->data[i + 1].name) > 0)
{
struct PeoInfo tmp;
tmp = pcon->data[j];
pcon->data[j] = pcon->data[j+1];
pcon->data[j + 1] = tmp;
}
}
}
printf("%10s\t%5s\t%5s\t%15s\t%20s\n", "name", "sex", "age", "tele", "addr");
for (k= 0; k< pcon->sz; k++)
{
printf("%10s\t%5s\t%5d\t%10s\t%15s\n",
pcon->data[k].name,
pcon->data[k].sex,
pcon->data[k].age,
pcon->data[k].tele,
pcon->data[k].addr);
}
}
void ClearContact(pContact pcon)
{
pcon->sz = 0;
}
#endif