目录
1. 功能分析
用现有的知识创建一个通讯录,通讯录存储的人员信息包含姓名,年龄,电话,性别,住址。而通讯录能够实现人员信息的存储之外,还能要有对信息的增加,删除,修改,查看,展示等五个模块的功能。由此,我们来着手实现。
(注:IDE:VS2019)
2. 代码实现
2.1 模块一:通讯录信息基本存储单位的创建初始化与菜单界面整体逻辑的实现
菜单界面
enum option
{
EXIT,
ADD,
DEL,
MODIFY,
SEARCH,
SHOW,
SORT
};
void meuo()
{
printf("************************************\n");
printf("**** 1.add **** 2.del ****\n");
printf("**** 3.modify **** 4.search ****\n");
printf("**** 5.show **** 6.sort ****\n");
printf("**** 0.exit ********************\n");
}
void test()
{
meuo();
int input;
//通讯录1
perinf contact;
//初始化
Initper(&contact);
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
printf("增加信息\n");
add(&contact);
break;
case DEL:
del(&contact);
break;
case MODIFY:
modify(&contact);
break;
case SEARCH:
search(&contact);
break;
case SHOW:
show(&contact);
break;
case SORT:
//sort(&contact);
break;
case EXIT:
printf("退出\n");
break;
default:
printf("选择失败,请重新选择\n");
}
} while (input);
}
int main()
{
test();
return 0;
}
数据存储的实现与信息初始化
#define MAX 100
#define SEX_MAX 5
#define LOC_MAX 5
#define TEL_MAX 12
#define NAME_MAX 20
typedef struct people
{
int age;
char sex[SEX_MAX];
char loc[LOC_MAX];
char tel[TEL_MAX];
char name[NAME_MAX];
}p;
typedef struct person
{
p data[MAX];
int sz;
}perinf;
//初始化
void Initper(perinf* contact)
{
memset(contact->data, 0, sizeof(contact->data));
contact->sz = 0;
}
2.2 模块二:人员信息的添加
void add(perinf* contact)
{
int i = 0;
for (i = 0; i < contact->sz; i++)
{
;
}
//增加姓名
printf("姓名\n");
scanf("%s", contact->data[i].name);
//增加性别
printf("性别\n");
scanf("%s", contact->data[i].sex);
//增加年龄信息
printf("年龄\n");
scanf("%d", &(contact->data[i].age));
//增加电话
printf("电话\n");
scanf("%s", contact->data[i].tel);
//增加地址
printf("地址\n");
scanf("%s", contact->data[i].loc);
printf("添加新联系人成功\n");
(contact->sz)++;
}
2.3 模块三:人员信息的展示
void show(const perinf* contact)
{
printf("%-10s\t%-4s\t%-5s\t%-10s\t%-10s\n", "姓名", "年龄", "性别", "电话", "地址");
int i = 0;
for (i = 0; i < contact->sz; i++)
{
printf("%-10s\t%-4d\t%-5s\t%-10s\t%-10s\n",
contact->data[i].name,
contact->data[i].age,
contact->data[i].sex,
contact->data[i].tel,
contact->data[i].loc
);
}
}
2.4 模块四:人员信息的删除
void del(perinf* contact)
{
assert(contact != NULL);
//删除指定名字的对象
//名字遍历,返回下标
char name[NAME_MAX];
int del = 0;
int i = 0;
scanf("%s", &name);
if (Findname(contact, name) == -1)
{
printf("未有此联系人\n");
}
else
{
del = Findname(contact, name);
for (i = del; i < contact->sz - 1; i++)
{
contact->data[i] = contact->data[i + 1];
}
contact->sz--;
printf("删除成功\n");
}
}
//所调用自定义组件Findname()
int Findname(perinf* contact,char name[NAME_MAX])
{
assert(contact != NULL);
int i = 0;
for (i = 0; i < contact->sz; i++)
{
if (strcmp(contact->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
2.5 模块五:人员信息的修改
void modify(perinf* contact)
{
assert(contact != NULL);
char name[NAME_MAX];
scanf("%s", name);
int i = Findname(contact, name);
if (i == -1)
{
printf("未找到此联系人\n");
return;
}
//增加姓名
printf("姓名\n");
scanf("%s", contact->data[i].name);
//增加性别
printf("性别\n");
scanf("%s", contact->data[i].sex);
//增加年龄信息
printf("年龄\n");
scanf("%d", &(contact->data[i].age));
//增加电话
printf("电话\n");
scanf("%s", contact->data[i].tel);
//增加地址
printf("地址\n");
scanf("%s", contact->data[i].loc);
printf("修改联系人成功\n");
}
2.6 模块六:人员信息的查找
void search(perinf* contact)
{
assert(contact != NULL);
char name[NAME_MAX];
scanf("%s", name);
int i = Findname(contact, name);
if (i == -1)
{
printf("未找到此人\n");
}
else
{
printf("找到了,此人信息如下:\n");
printf("%-10s\t%-4s\t%-5s\t%-10s\t%-10s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-10s\t%-4d\t%-5s\t%-10s\t%-10s\n",
contact->data[i].name,
contact->data[i].age,
contact->data[i].sex,
contact->data[i].tel,
contact->data[i].loc);
}
}
3. 项目总览
contact.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX 100
#define SEX_MAX 5
#define LOC_MAX 5
#define TEL_MAX 12
#define NAME_MAX 20
typedef struct people
{
int age;
char sex[SEX_MAX];
char loc[LOC_MAX];
char tel[TEL_MAX];
char name[NAME_MAX];
}p;
typedef struct person
{
p data[MAX];
int sz;
}perinf;
//信息初始化
void Initper(perinf*);
//增
void add(perinf*);
//删
void del(perinf*);
//改
void modify(perinf*);
//查
void search(perinf*);
//显示
void show(const perinf*);
contact.c
#include "contact.h"
//初始化
void Initper(perinf* contact)
{
memset(contact->data, 0, sizeof(contact->data));
contact->sz = 0;
}
//查找名字
int Findname(perinf* contact,char name[NAME_MAX])
{
assert(contact != NULL);
int i = 0;
for (i = 0; i < contact->sz; i++)
{
if (strcmp(contact->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
//增加
void add(perinf* contact)
{
int i = 0;
for (i = 0; i < contact->sz; i++)
{
;
}
//增加姓名
printf("姓名\n");
scanf("%s", contact->data[i].name);
//增加性别
printf("性别\n");
scanf("%s", contact->data[i].sex);
//增加年龄信息
printf("年龄\n");
scanf("%d", &(contact->data[i].age));
//增加电话
printf("电话\n");
scanf("%s", contact->data[i].tel);
//增加地址
printf("地址\n");
scanf("%s", contact->data[i].loc);
printf("添加新联系人成功\n");
(contact->sz)++;
}
//显示
void show(const perinf* contact)
{
printf("%-10s\t%-4s\t%-5s\t%-10s\t%-10s\n", "姓名", "年龄", "性别", "电话", "地址");
int i = 0;
for (i = 0; i < contact->sz; i++)
{
printf("%-10s\t%-4d\t%-5s\t%-10s\t%-10s\n",
contact->data[i].name,
contact->data[i].age,
contact->data[i].sex,
contact->data[i].tel,
contact->data[i].loc
);
}
}
//删除
void del(perinf* contact)
{
assert(contact != NULL);
//删除指定名字的对象
//名字遍历,返回下标
char name[NAME_MAX];
int del = 0;
int i = 0;
scanf("%s", &name);
if (Findname(contact, name) == -1)
{
printf("未有此联系人\n");
}
else
{
del = Findname(contact, name);
for (i = del; i < contact->sz - 1; i++)
{
contact->data[i] = contact->data[i + 1];
}
contact->sz--;
printf("删除成功\n");
}
}
//修改
void modify(perinf* contact)
{
assert(contact != NULL);
char name[NAME_MAX];
scanf("%s", name);
int i = Findname(contact, name);
if (i == -1)
{
printf("未找到此联系人\n");
return;
}
//增加姓名
printf("姓名\n");
scanf("%s", contact->data[i].name);
//增加性别
printf("性别\n");
scanf("%s", contact->data[i].sex);
//增加年龄信息
printf("年龄\n");
scanf("%d", &(contact->data[i].age));
//增加电话
printf("电话\n");
scanf("%s", contact->data[i].tel);
//增加地址
printf("地址\n");
scanf("%s", contact->data[i].loc);
printf("修改联系人成功\n");
}
void search(perinf* contact)
{
assert(contact != NULL);
char name[NAME_MAX];
scanf("%s", name);
int i = Findname(contact, name);
if (i == -1)
{
printf("未找到此人\n");
}
else
{
printf("找到了,此人信息如下:\n");
printf("%-10s\t%-4s\t%-5s\t%-10s\t%-10s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("%-10s\t%-4d\t%-5s\t%-10s\t%-10s\n",
contact->data[i].name,
contact->data[i].age,
contact->data[i].sex,
contact->data[i].tel,
contact->data[i].loc);
}
}
test.c
#include "contact.h"
//简单通讯录的实现
//通讯录功能:增,删,改,查,显示,排序
//通讯录单位对象信息(100人份):
//姓名,年龄,性别,电话,住址
enum option
{
EXIT,
ADD,
DEL,
MODIFY,
SEARCH,
SHOW,
SORT
};
void meuo()
{
printf("************************************\n");
printf("**** 1.add **** 2.del ****\n");
printf("**** 3.modify **** 4.search ****\n");
printf("**** 5.show **** 6.sort ****\n");
printf("**** 0.exit ********************\n");
}
void test()
{
meuo();
int input;
//通讯录1
perinf contact;
//初始化
Initper(&contact);
do
{
scanf("%d", &input);
switch (input)
{
case ADD:
printf("增加信息\n");
add(&contact);
break;
case DEL:
del(&contact);
break;
case MODIFY:
modify(&contact);
break;
case SEARCH:
search(&contact);
break;
case SHOW:
show(&contact);
break;
case SORT:
sort(&contact);
break;
case EXIT:
printf("退出\n");
break;
default:
printf("选择失败,请重新选择\n");
}
} while (input);
}
int main()
{
test();
return 0;
}