静态通讯录

时间:2022-12-03 07:13:20

主函数

#include "contect.h"
void mune()
{
printf("**********************************\n");
printf("*********1.add 2.del**********\n");
printf("*********3,search 4modify********\n");
printf("*********5show 6sqrt**********\n");
printf("*************0exit****************\n");
}
int main()
{
struct contact con;
Initcontact(&con);
int input = 0;
do
{
mune();
printf("选择");
scanf("%d", &input);
switch (input)
{
case EXIT:
printf("退出");
case ADD:
Addcontact(&con);
break;
case DEL:
Delcontact(&con);
break;
case SEARCH:
Searchcontact(&con);
break;
case MODIFY:
Modcontact(&con);
break;
case SHOW:
Showcontact(&con);
break;
case SORT:
break;
default:
printf("选择错误");
break;
}
} while (input);
return 0;
}

实现功能的文件

#include "contect.h"
struct peoinfo con[MAX];
static int Findpeo(contact*ps,char name[name_max])
{
int i = 0;
for (i = 0; i < ps->size; i++)
{
if (0 == strcmp(ps->date[i].name, name))
{
return i;//数据找到了
}
}
return - 1;//数据找不到
}
//初始化数据
void Initcontact( contact* ps)
{
memset(ps->date,0, sizeof(ps->date));
ps->size = 0;
}
//添加数据
void Addcontact( contact* ps)
{
if (ps->size == MAX)
printf("满了");
else
{
printf("输入名字\n");
scanf("%s", ps->date[ps->size].name);
printf("输入年龄\n");
scanf("%s", ps->date[ps->size].age);
printf("输入性别\n");
scanf("%s", ps->date[ps->size].sex);
printf("输入电话\n");
scanf("%s", ps->date[ps->size].tele);
ps->size++;
printf("成功\n");
}
}
//显示数据
void Showcontact(const contact* ps)
{
if (ps->size == 0)
{
printf("没有");
}
else
{
int i = 0;
printf("%-20s\t%-4s\t%-12s\t%-20s\n","名字","年龄","性别","电话");
for (i = 0; i < ps->size; i++)
{
printf("%-20s\t%-4s\t%-12s\t%-20s\n",
ps->date[i].name,
ps->date[i].age,
ps->date[i].sex,
ps->date[i].tele
);
}
}
}
//删除数据
void Delcontact( contact* ps)
{
char name[name_max];
int i = 0;
printf("选择删除人\n");
scanf("%s", name);
int pos = Findpeo(ps, name);
if (pos == -1)
{
printf("找不到\n");
}
else
{
int j = 0;
for (j = i; j < ps->size - 1; j++)
{
ps->date[j] = ps->date[j + 1];
}
ps->size--;
printf("删除成功\n");
}




}
//寻找数据
void Searchcontact(const contact* ps)
{
char name[name_max];
int i = 0;
printf("寻找人\n");
scanf("%s", name);
int pos = Findpeo(ps, name);
if (pos == -1)
{
printf("找不到\n");
}
else
{
printf("%-20s\t%-4s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话");
printf("%-20s\t%-4s\t%-12s\t%-20s\n",
ps->date[pos].name,
ps->date[pos].age,
ps->date[pos].sex,
ps->date[pos].tele);
}

}
void Modcontact(contact* ps)
{
char name[name_max];
int i = 0;
printf("修改人\n");
scanf("%s", name);
int pos = Findpeo(ps, name);
if (pos == -1)
{
printf("找不到\n");
}
else
{
printf("输入名字\n");
scanf("%s", ps->date[pos].name);
printf("输入年龄\n");
scanf("%s", ps->date[pos].age);
printf("输入性别\n");
scanf("%s", ps->date[pos].sex);
printf("输入电话\n");
scanf("%s", ps->date[ps->size].tele);
printf("成功\n");
}
}
void serachcontact(const contact* ps)
{
char name[name_max];
int i = 0;
printf("选择删除人\n");
scanf("%s", name);
int pos = Findpeo(ps, name);
if (pos == -1)
{
printf("找不到\n");
}
else
{
printf("%-20s\t%-4s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话");
printf("%-20s\t%-4s\t%-12s\t%-20s\n",
ps->date[pos].name,
ps->date[pos].age,
ps->date[pos].sex,
ps->date[pos].tele
);
}
}

头文件

#define name_max 10
#define age_max 3
#define MAX 1000
#define sex_max 10
#define tele_max 20
#include <string.h>
#include<stdio.h>
enum option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
struct peoinfo
{
char name[name_max];
char age[age_max];
char sex[sex_max];
char tele[tele_max];
};
typedef struct contact
{
struct peoinfo date[MAX];
int size;
}contact;
void Initcontact(struct contact* ps);
void Addcontact(struct contact* ps);
void Showcontact(const struct contact* ps);
void Delcontact(struct contact* ps);
void Searchcontact(struct contact* ps);
void Modcontact(contact* ps);
void serachcontact(contact* ps);