实现一个通讯录,可以保存联系人姓名、电话、年龄、性别、地址
头文件
`#ifndef CONTACT_H
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
enum Option
{
EXIT,
ADD,
SEAR,
DEL,
SHOW,
CLEAR,
SORT
};
#define MAX_NAME 20
#define MAX_TEL 11
#define MAX_ADDR 15
#define MAX_SEX 3
#define MAX_NUMPERSON 1000
#define MAX_STR 20
typedef struct Personlnfo
{
char name[MAX_NAME];
short age;
char tele[MAX_TEL];
char addr[MAX_ADDR];
char sex[MAX_SEX];
}Personlnfo;
//通讯录
typedef struct Contact
{
Personlnfo per[MAX_NUMPERSON];
int usedSize;//被使用的个数
}Contact;
void InitContact(Contact *pCon);
void AddContact(Contact *pCon);
int SearchContact(Contact *pCon);
void DelContact(Contact *pCon);
void ShowContact(Contact *pCon);
void ClearContact(Contact *pCon);
#endif`
contact.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void InitContact(Contact *pCon)
{
assert(pCon != NULL);
pCon->usedSize = 0;
memset(pCon->per, 0, sizeof(pCon->per));
}
//添加一个人物到通讯录
void AddContact(Contact *pCon)
{
if (pCon->usedSize == MAX_NUMPERSON)
{
printf("不好意思,通讯录满了\n");
return;
}
printf("请输入姓名;");
scanf("%s", pCon->per[pCon->usedSize].name);
printf("请输入性别;");
scanf("%s", pCon->per[pCon->usedSize].sex);
printf("请输入年龄;");
scanf("%d",&( pCon->per[pCon->usedSize].age));
printf("请输入电话;");
scanf("%s", pCon->per[pCon->usedSize].tele);
printf("请输入地址;");
scanf("%s", pCon->per[pCon->usedSize].addr);
pCon->usedSize++;
printf("添加成功\n");
}
int SearchContact(Contact *pCon)
{
int i = 0;
char name[MAX_NAME] = {0};
assert(pCon != NULL);
if (pCon->usedSize == 0)
{
printf("通讯录为空");
return -1;
}
printf("请输入姓名\n");
scanf("%s", name);
for (i = 0; i < pCon->usedSize; i++)
{
if (strcmp(pCon->per[i].name, name) == 0)
return 1;
else
return -1;
}
}
void DelContact(Contact *pCon)
{
int index = SearchContact(pCon);
int i = 0;
assert(pCon != NULL);
if (index == -1)
{
printf("查无此人\n");
return;
}
for (i = index; i <pCon->usedSize-1; i++)
{
pCon->per[i] = pCon->per[i + 1];
}
pCon->usedSize--;
printf("删除成功\n");
}
void ShowContact(Contact *pCon)
{
int i = 0;
assert(pCon != NULL);
printf("%-20s %-3s %-10s %-11s %-15s\n", "姓名", "性别", "年龄", "电话", "地址");
for (i = 0; i < pCon->usedSize; i++)
{
printf("%-20s %-3s %-10d %-11s %-15s\n", pCon->per[i].name, pCon->per[i].sex, pCon->per[i].age, pCon->per[i].tele, pCon->per[i].addr);
}
}
void ClearContact(Contact *pCon)
{
pCon->usedSize = 0;
}
test.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void menu()
{
printf("********1.add**********2.search*********\n");
printf("********3.del**********4.show***********\n");
printf("********5.clear********6.sort***********\n");
printf("********0.exit**************************\n");
}
void start()
{
int input = 0;
//初始化通讯录 数组
Contact con;
InitContact(&con);
do
{
menu();
printf("请输入你的操作>>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case SEAR:
SearchContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case CLEAR:
ClearContact(&con);
break;
}
} while (input);
}
int main()
{
start();
return 0;
}