通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
可以有以下功能:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
Contact.h
#ifndef __Contact_H__
#define __Contact_H__
#pragma warning(disable:4996)
#include<stdio.h>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
enum SLECT
{
EXIT,
ADD,
DEL,
SEARCH,
ALERT,
SHOW,
SORT,
EMPTY
};
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_ADDR 30
#define MAX_TELE 20
#define MAX 1000
typedef struct People
{
char name[MAX_NAME];
short agel;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
}People;
typedef struct Contact
{
struct People data[MAX];
unsigned int sz;
int capacity;
}Contact;
void AddPeople(Contact *ps);
int SearchPeople(Contact *ps);
void DelePeople(Contact *ps);
void AlertPeople(Contact *ps);
void ShowPeople(Contact *ps);
void SortPeople(Contact *ps);
void EmptyPeople(Contact *ps);
void InitContact(Contact *ps);
#endif
Contact.c
#include"contact.h"
//添加联系人
void AddPeople(Contact *ps)
{
assert(ps != NULL);
if (ps->sz == MAX)
{
printf("通讯录已满\n");
return;
}
else
{
printf("请输入姓名:");
scanf("%s", ps->data[ps->sz].name);
printf("请输入性别:");
scanf("%s", ps->data[ps->sz].sex);
printf("请输入年龄:");
scanf("%d", &ps->data[ps->sz].agel);
printf("请输入电话:");
scanf("%s", ps->data[ps->sz].tele);
printf("请输入地址:");
scanf("%s", ps->data[ps->sz].addr);
ps->sz++;
printf("添加成功\n");
}
}
//查找联系人
int SearchPeople(Contact *ps)
{
char names[20];
printf("请输入姓名:");
scanf("%s", names);
size_t i = 0;
for (; i < ps->sz; i++)
{
if (0 == strcmp(ps->data[i].name, names))
{
return i;
}
}
return -1;
}
int FindPeople(Contact *ps)
{
char names[20];
printf("请输入姓名:");
scanf("%s", names);
size_t i = 0;
for (; i < ps->sz; i++)
{
if (0 == strcmp(ps->data[i].name, names))
{
return i;
}
}
return -1;
}
//删除联系人
void DelePeople(Contact *ps)
{
if (ps->sz == 0)
{
printf("通讯录已空\n");
return;
}
else
{
int ret = FindPeople(ps);
if (ret < 0)
{
printf("查无此人\n");
return;
}
else
{
size_t i = ret;
for (; i < ps->sz - 1; i++)
{
ps->data[i] = ps->data[i + 1];
}
ps->sz--;
}
}
}
//修改联系人信息
void AlertPeople(Contact *ps)
{
int ret = FindPeople(ps);
if (ret < 0)
{
printf("查无此人\n");
return;
}
else
{
printf("请输入要修改的姓名:");
scanf("%s", ps->data[ret].name);
printf("请输入要修改的性别:");
scanf("%s", ps->data[ret].sex);
printf("请输入要修改的年龄:");
scanf("%d", &ps->data[ret].agel);
printf("请输入要修改的电话:");
scanf("%s", ps->data[ret].tele);
printf("请输入要修改的地址:");
scanf("%s", ps->data[ret].addr);
printf("修改成功\n");
}
}
//显示所有联系人信息
void ShowPeople(Contact *ps)
{
if (ps->sz == 0)
{
printf("此通讯录为空\n");
return;
}
else
{
size_t i = 0;
printf("%-5s\t%-5s\t%-3s\t%-12s\t%-12s\n", "名字", "性别", "年龄", "电话", "地址");
for (; i < ps->sz; i++)
{
printf("%-5s\t%-5s\t%-3d\t%-12s\t%-17s\n",
ps->data[i].name,
ps->data[i].sex,
ps->data[i].agel,
ps->data[i].tele,
ps->data[i].addr);
}
}
}
//按姓名排序通讯录
void SortPeople(Contact *ps)
{
if (ps->sz > 1)
{
size_t i = 0;
for (; i < ps->sz - 1; i++)
{
size_t j = 0;
for (; j < ps->sz - 1 - i; j++)
{
if (strcmp(ps->data[j].name, ps->data[j + 1].name))
{
People temp = ps->data[j];
ps->data[j] = ps->data[j + 1];
ps->data[j + 1] = temp;
}
}
}
}
printf("排序成功\n");
}
//清空通讯录
void EmptyPeople(Contact *ps)
{
ps->sz = 0;
printf("清空成功\n");
}
//初始化通讯录
void InitContact(Contact *ps)
{
int size = sizeof(ps->data);
ps->sz = 0;
memset(ps->data, 0, size);
}
test.c
#include"contact.h"
void menu()
{
printf("**********************************\n");
printf("**** 1.add 2.del ****\n");
printf("**** 3.search 4.alert ****\n");
printf("**** 5.show 6.sort ****\n");
printf("**** 7.empty 0.exit ****\n");
printf("**********************************\n");
}
int main()
{
int input = 0;
Contact ps;
InitContact(&ps);
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case ADD:
AddPeople(&ps);
break;
case DEL:
DelePeople(&ps);
break;
case SEARCH:
SearchPeople(&ps);
break;
case ALERT:
AlertPeople(&ps);
break;
case SHOW:
ShowPeople(&ps);
case SORT:
SortPeople(&ps);
case EMPTY:
EmptyPeople(&ps);
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}