contact.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define NAME_MAX 20
#define SEX_MAX 5
#define TELE_MAX 20
#define ADDR_MAX 30
#define MAX 1000
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDR_MAX];
}PeoInfo;
typedef struct Contact
{
PeoInfo data[MAX];
int sz;
}Contact,*pcontact;
void add(pcontact pc);
void show(pcontact pc);
void del(pcontact pc);
void emp(pcontact pc);
void init(pcontact pc);
void px(pcontact pc);
void find(pcontact pc);
void xiugai(pcontact pc);
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void init(pcontact pc) //初始化结构体
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void add(pcontact pc) //添加联系人信息
{
if (pc->sz == MAX)
{
printf("通讯录已满\n");
return;
}
printf("请输入名字>");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄>");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入性别>");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话>");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("添加成功\n");
}
int find_s(pcontact pc,char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(name, pc->data[i].name)==0)
{
return i;
}
}
return -1;
}
void del(pcontact pc) //删除联系人信息
{
char name[NAME_MAX] = { 0 };
int i = 0;
printf("请输入名字>");
scanf("%s", name);
int pos = 0;
pos = find_s(pc, name);
if (pos == -1)
{
printf("不存在\n");
return;
}
for (i = pos; i < pc->sz; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功\n");
}
void show(pcontact pc) //显示联系人
{
int i = 0;
printf("%10s%6s%5s%12s%10s\n", "name", "age", "sex", "tele", "addr");
for (i = 0; i < pc->sz; i++)
{
printf("%10s%6d%5s%12s%10s\n",
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void emp(pcontact pc) //清空联系人
{
pc->sz = 0;
printf("清空成功\n");
}
void px(pcontact pc) //以名字排序显示联系人
{
int i = 0;
int j = 0;
for (i = 0; i < pc->sz-1; i++)
{
for (j = 0; j < pc->sz - i-1; j++)
{
if (strcmp(pc->data[j].name, pc->data[j + 1].name)>0)
{
PeoInfo tmp;
tmp = pc->data[j];
pc->data[j] = pc->data[j + 1];
pc->data[j + 1] = tmp;
}
}
}
show(pc);
}
void find(pcontact pc) //按名字查找联系人
{
char name[NAME_MAX] = { 0 };
printf("请输入名字查找>");
scanf("%s", name);
int ret = find_s(pc, name);
if (ret == -1)
{
printf("联系人不存在\n");
return;
}
else
{
printf("%10s%6s%5s%12s%10s\n", "name", "age", "sex", "tele", "addr");
printf("%10s%6d%5s%12s%10s\n",
pc->data[ret].name,
pc->data[ret].age,
pc->data[ret].sex,
pc->data[ret].tele,
pc->data[ret].addr);
}
}
void xiugai(pcontact pc) //按名字修改联系人信息
{
char name[NAME_MAX] = { 0 };
printf("请输入名字进行修改>");
scanf("%s", name);
int ret = find_s(pc, name);
if (ret == -1)
{
printf("该联系人不存在\n");
return;
}
else
{
printf("请重新输入名字>");
scanf("%s", pc->data[ret].name);
printf("请重新输入年龄>");
scanf("%d", &(pc->data[ret].age));
printf("请重新输入性别>");
scanf("%s", pc->data[ret].sex);
printf("请重新输入电话>");
scanf("%s", pc->data[ret].tele);
printf("请重新输入地址>");
scanf("%s", pc->data[ret].addr);
printf("修改成功\n");
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
//1. 添加联系人信息
//2. 删除指定联系人信息
//3. 查找指定联系人信息
//4. 修改指定联系人信息
//5. 显示所有联系人信息
//6. 清空所有联系人
//7. 以名字排序所有联系人
void menu()
{
printf("**********************************************************\n");
printf("******0. 退出 1. 添加联系人信息***********\n");
printf("******2. 删除指定联系人信息 3. 查找指定联系人信息*******\n");
printf("******4. 修改指定联系人信息 5. 显示所有联系人信息*******\n");
printf("******6. 清空所有联系人 7. 以名字排序所有联系人*****\n");
printf("**********************************************************\n");
}
enum OP //枚举变量
{
EXIT, //0
ADD, //1
DEL,
FIND,
XIUGAI,
SHOW,
EMP,
PX //7
};
int main()
{
Contact my_con;
init(&my_con);
int input = 0;
do
{
menu();
printf("请选择>");
scanf("%d", &input);
switch (input)
{
case EXIT:
break;
case ADD:
add(&my_con);
break;
case SHOW:
show(&my_con);
break;
case DEL:
del(&my_con);
break;
case FIND:
find(&my_con);
break;
case EMP:
emp(&my_con);
break;
case PX:
px(&my_con);
break;
case XIUGAI:
xiugai(&my_con);
break;
default:
printf("选择错误\n");
break;
}
} while (input);
system("pause");
return 0;
}