test.c文件
#include "contact.c"
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");
}
int main()
{
int input = 0;
struct Contact con;
InitContact(&con);
do
{
menu();
printf("请选择操作的数->");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DelContact(&con);
break;
case SEARCH:
SearchContatc(&con);
break;
case MODIFY:
MondifContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case SORT:
break;
case EXIT:
printf("您已经退出!\n");
break;
default:
printf("输入格式有误,请重新输入\n");
break;
}
} while (input);
return 0;
}
contatc.c文件
#include "contact.h"
void InitContact(struct Contact *ps)
{
memset(ps->data, 0, sizeof(ps->data));
ps->size = 0;
}
void AddContact(struct Contact *ps)
{
if (ps->size == MAX)
{
printf("ͨ通讯录已满,请删除不必要的信息再添加\n");
}
else
{
printf("请输入姓名>");
scanf("%s", ps->data[ps->size].name);
printf("请输入年龄>");
scanf("%s", &(ps->data[ps->size].age));
printf("请输入信息>");
scanf("%s", ps->data[ps->size].sex);
printf("请输入电话>");
scanf("%s", ps->data[ps->size].tele);
printf("请输入地址>");
scanf("%s", ps->data[ps->size].addr);
ps->size++;
printf("添加成功\n");
}
}
void ShowContact(const struct Contact *ps)
{
if (ps->size == 0)
{
printf("ͨ通讯录为空\n");
}
else
{
int i = 0;
printf("%--20s\t%4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < (int)ps->size; i++)
{
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->data[i],
ps->data[i].age,
ps->data[i].sex,
ps->data[i].tele,
ps->data[i].addr);
}
}
}
static int FindByName(const struct Contact *ps, char name[MAX_NAME])
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (0 == strcmp(ps->data[i].name, name))
{
return i;
}
}
return -1;
}
void DelContact(struct Contact *ps)
{
char name[MAX_NAME];
printf("请输入删除人的名:>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("没有您查询的名字,请重新输入\n");
}
else
{
int j = 0;
for (j = pos; j < (ps->size - 1); j++)
{
ps->data[pos] = ps->data[j + 1];
}
ps->size--;
printf("删除成功\n");
}
}
void SearchContatc(const struct Contact *ps)
{
char name[MAX_NAME];
printf("请输入查找人的名:");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("查找的人不存在\n");
}
else
{
printf("%--20s\t%4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n",
ps->data[pos],
ps->data[pos].age,
ps->data[pos].sex,
ps->data[pos].tele,
ps->data[pos].addr);
}
}
void MondifContact(struct Contact *ps)
{
char name[MAX_NAME];
printf("请输入要修改的人的名字:>");
scanf("%s", name);
int pos = FindByName(ps, name);
if (pos == -1)
{
printf("%s信息不存在\n");
}
else
{
printf("请输入姓名>");
scanf("%s", ps->data[pos].name);
printf("请输入年龄>");
scanf("%s", &(ps->data[pos].age));
printf("请输入信息>");
scanf("%s", ps->data[pos].sex);
printf("请输入电话>");
scanf("%s", ps->data[pos].tele);
printf("请输入地址>");
scanf("%s", ps->data[pos].addr);
printf("修改成功\n");
}
}
contact.h文件
#include <stdio.h>
#include <string.h>
#define MAX 1000
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 12
#define MAX_ADDR 30
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
struct peoInfo
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
};
struct Contact
{
struct peoInfo data[MAX];
int size;
};
void InitContact(struct Contact *ps);
void AddContact(struct Contact *ps);
void ShowContact(const struct Contact *ps);
void DelContact(struct Contact *ps);
void SearchContatc(const struct Contact *ps);
void MondifContact(struct Contact *ps);