contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void InitContact(pContact pcon) //初始化电话本函数的实现
{
int i = 0;
pcon->count = 0;
memset(pcon->contact, 0, MAX * sizeof(PeoInfo)); //注意memset函数的使用
}
void add(pContact pcon) //添加功能的实现
{
if (pcon->count >= MAX)
{
printf("电话本已满!!!");
return;
}
printf("请输入名字:");
scanf("%s",pcon->contact[pcon->count].name);
printf("请输入年龄:");
scanf("%d", &(pcon->contact[pcon->count].age));
printf("请输入性别:");
scanf("%s", pcon->contact[pcon->count].sex);
printf("请输入号码:");
scanf("%s", pcon->contact[pcon->count].number);
printf("请输入地址:");
//scanf("%s", &PeoInfo.addr); //注意这种理解上的错误
scanf("%s",pcon->contact[pcon->count].addr);
pcon->count++;
printf("添加成功!!!\n");
}
void del(pContact pcon)
{
int i = 0;
char input = 0;
printf("请输入你要删除的人的姓名:");
scanf("%s",&input);
if (pcon->count == 0)
{
printf("电话本已空,不能进行删除操作!!!\n");
return;
}
else
{
for (i = 0; i < pcon->count; i++)
{
if (strcmp((pcon->contact[i].name), &input)==0)
{
pcon->contact[i] = pcon->contact[i + 1];
pcon->count--;
printf("删除成功!!!\n");
return;
}
}
printf("该联系人不存在!!!\n");
return;
}
}
void search(pContact pcon)
{
char input = 0;
int i = 0;
printf("请输入你要查找的联系人的姓名:");
scanf("%s",&input);
for (i = 0; i <= pcon->count; i++)
{
if (strcmp((pcon->contact[i].name), &input) == 0)
{
printf("%10s%10d%10s%10s%10s\n",
pcon->contact[i].name,
pcon->contact[i].age,
pcon->contact[i].sex,
pcon->contact[i].number,
pcon->contact[i].addr);
return;
}
}
printf("没有你要查找的联系人信息!!!\n");
return;
}
void modify(pContact pcon)
{
char input = 0;
int i = 0;
printf("请输入你要修改的联系人的姓名:");
scanf("%s",&input);
for (i = 0; i <= pcon->count; i++)
{
if (strcmp((pcon->contact[i].name), &input) == 0)
{
printf("请输入新的信息!!!\n\n");
printf("请输入名字:");
scanf("%s", pcon->contact[i].name);
printf("请输入年龄:");
scanf("%d", &(pcon->contact[i].age));
printf("请输入性别:");
scanf("%s", pcon->contact[i].sex);
printf("请输入号码:");
scanf("%s", pcon->contact[i].number);
printf("请输入地址:");
scanf("%s", pcon->contact[i].addr);
printf("修改成功\n");
return;
}
}
printf("没有你要修改的联系人信息!!!\n");
return;
}
void show(pContact pcon) //显示联系人信息的功能实现
{
int i = 0;
printf("%10s%10s%10s%10s%10s\n","name","age","sex","number","addr");
for (i = 0; i < pcon->count; i++)
{
printf("%10s%10d%10s%10s%10s\n",
pcon->contact[i].name,
pcon->contact[i].age,
pcon->contact[i].sex,
pcon->contact[i].number,
pcon->contact[i].addr);
}
}
void empty(pContact pcon) //清空通讯录
{
pcon->count = 0;
}
void sort(pContact pcon) //排序
{
int i = 0;
int j = 0;
for (i = 0; i < pcon->count; i++)
{
for (j = 0; j < pcon->count-i-1; j++)
{
if (strcmp(pcon->contact[j].name, pcon->contact[j + 1].name) > 0)
{
int cmp = *(pcon->contact[i].name);
*(pcon->contact[i].name) = *(pcon->contact[i+1].name);
*(pcon->contact[i].name) = cmp;
}
}
}
printf("排序完成!!!\n");
}
contact.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#define MAX_NAME 10
#define MAX_SEX 3
#define MAX_NUMBER 12
#define MAX_ADDR 30
#define MAX 1000
//通过结构体来保存一个联系人的信息
typedef struct {
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char number[MAX_NUMBER];
char addr[MAX_ADDR];
}PeoInfo;
//通过结构体来表示电话本的最大容量与当前已存联系人数量
typedef struct {
PeoInfo contact[MAX];
int count;
}Contact, *pContact;
void InitContact(pContact pcon);
void add(pContact pcon);
void del(pContact pcon);
void search(pContact pcon);
void modify(pContact pcon);
void show(pContact pcon);
void empty(pContact pcon);
void sort(pContact pcon);
test.c
#include"contact.h"
enum Option //通讯录各种操作的枚举
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
EMPTY,
SORT,
};
void menu() //通讯录的菜单
{
printf(" 通讯录 \n");
printf("***************************\n");
printf("*** 1.add ***\n");
printf("*** 2.del ***\n");
printf("*** 3.search ***\n");
printf("*** 4.modify ***\n");
printf("*** 5.show ***\n");
printf("*** 6.empty ***\n");
printf("*** 7.sort ***\n");
printf("*** 0.exit ***\n");
printf("***************************\n");
}
void test() //实现通讯录的主要思路
{
Contact con = {0}; //电话本置零
InitContact(&con); //初始化电话本函数
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d",&input);
switch(input)
{
case ADD:
add(&con); //添加联系人的功能实现
break;
case DEL:
del(&con); //删除联系人的功能实现
break;
case SEARCH:
{
//int ret = 0;
//int i = 0;
//ret = search(&con);
search(&con); //查找联系人的功能实现
//if (ret = 1)
//{
// printf("%10s%10d%10s%10s%10s\n",
// pcon->contact[i].name,
// pcon->contact[i].age,
// pcon->contact[i].sex,
// pcon->contact[i].number,
// pcon->contact[i].addr);
//}
//else
//{
// printf("未找到该联系人信息!!!\n");
//}
break;
}
case MODIFY:
modify(&con); //修改联系人信息的功能实现
break;
case SHOW:
show(&con); //显示联系人信息的功能实现
break;
case EMPTY:
empty(&con); //清空电话本的功能实现
break;
case SORT:
sort(&con); //对联系人进行排序
break;
case EXIT:
break;
default:
printf("输入错误,请重新输入!!!\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}