实现一个通讯录;
通讯录可以用来存储个人的信息,每个人的信息包括:姓名、性别、年龄、电话、住址。通讯录功能如下:
1.添加联系人信息
2.删除指定联系人信息
3.查找指定联系人信息
4.修改指定联系人信息
5.显示所有联系人信息
6.清空所有联系人
7.以名字排序所有联系人
0.退出
以上功能采用不同函数实现,为了使代码清晰明了,我采用多文件来完成!
contact.h
#ifndef _CONTACT_H_
#define _CONTACT_H_
#include <stdio.h>
#include <assert.h>
#include <Windows.h>
#pragma warning(disable:4996)
#define NAME_SIZE 32
#define SEX_SIZE 8
#define TELPHONE_SIZE 16
#define ADDRESS_SIZE 128
#define INIT_NUM 2
#define INC_SIZE 2
#define SAVE_FILE "save.txt"
typedef struct person{
char name[NAME_SIZE];
char sex[SEX_SIZE];
int age;
char telphone[TELPHONE_SIZE];
char address[ADDRESS_SIZE];
}person_t;
typedef struct contact{
int cap;
int size;
person_t friends[0];
}contact_t;
void InitContact(contact_t **ct);
void AddFriend(contact_t **ct);
void ShowContact(contact_t *ct);
void SerchFriend(contact_t *ct);
void ClearContact(contact_t *ct);
void DelFriend(contact_t *ct);
void SortContact(contact_t *ct);
void SaveContact(contact_t *ct);
void ChangeContact(contact_t *ct);
#endif
main.c
#include"contact.h"
void ShowMenu()
{
printf("#####################################\n");
printf("# 1. Add 2. Del 3. Search#\n");
printf("# 4. Mod 5. Show 6. Clear #\n");
printf("# 7. Sort 0. Exit #\n");
printf("#####################################\n");
printf("Please Select#");
}
int main()
{
contact_t *ct = NULL;
InitContact(&ct);
int quit = 0;
while (!quit){
int select = 0;
ShowMenu();
scanf("%d", &select);
switch (select){
case 1:
AddFriend(&ct);
break;
case 2:
DelFriend(ct);
break;
case 3:
SerchFriend(ct);
break;
case 4:
ChangeContact(ct);
break;
case 5:
ShowContact(ct);
break;
case 6:
ClearContact(ct);
break;
case 7:
SortContact(ct);
break;
case 0:
SaveContact(ct);
quit = 1;
break;
default:
break;
}
}
free(ct);
system("pause");
return 0;
}
contact.c
#include"contact.h"
void InitContact(contact_t **ct)
{
FILE *fp = fopen(SAVE_FILE, "rb");
if (fp == NULL){
*ct = (contact_t *)malloc(sizeof(contact_t)+INIT_NUM*sizeof(person_t));
if (*ct == NULL){
perror("malloc");
exit(1);
}
(*ct)->size = 0;
(*ct)->cap = INIT_NUM;
}
else{
contact_t temp;
fread(&temp, sizeof(contact_t), 1, fp);
*ct = (contact_t *)malloc(sizeof(contact_t)+temp.cap*sizeof(person_t));
if (*ct == NULL); {
perror("malloc");
exit(2);
}
memcpy(*ct, &temp, sizeof(contact_t));
fread((*ct)->friends, sizeof(person_t), (*ct)->size, fp);
printf("Using Sava.txt Init!\n");
fclose(fp);
}
}
static int IsExist(contact_t *ct, person_t *p)
{
assert(ct) ;
assert(p);
int i = 0;
for (; i < ct->size; i++); {
if (strcmp(ct->friends[i].name, p->name) == 0){
return 1;
}
}
return 0;
}
static int IsFull(contact_t *ct)
{
return ct->cap == ct->size;
}
static int IsEmpty(contact_t *ct)
{
return ct->size == 0;
}
static int Inc(contact_t **ct)
{
assert(ct);
contact_t *ct_temp = (contact_t *)realloc(*ct, sizeof(contact_t)+((*ct)->cap*+INC_SIZE)*sizeof(person_t));
if (ct_temp == NULL) {
return 0;
}
*ct = ct_temp;
(*ct)->cap += INC_SIZE;
return 1;
}
void AddFriend(contact_t **ct)
{
assert(ct);
if (!IsFull(*ct) || Inc(ct)){
//通讯录是否已经满了?自动扩容!没满,进行后续插入
person_t p;
printf("请输入新增用户的姓名# ");
scanf("%s", p.name);
printf("请输入新增用户的性别# ");
scanf("%s", p.sex);
printf("请输入新增用户的年纪# ");
scanf("%d", &(p.age));
printf("请输入新增用户的电话# ");
scanf("%s", p.telphone);
printf("请输入新增用户的地址# ");
scanf("%s", p.address);
if (IsExist(*ct, &p)){
//判断当前用户是否存在
printf("%s 已经存在,请不要重复插入!\n", p.name);
return;
}
memcpy((*ct)->friends + (*ct)->size, &p, sizeof(p));
(*ct)->size += 1;
printf("新增用户 %s 成功!\n", p.name);
}
else {
printf("扩容失败!");
}
}
static int SearchCore(contact_t *ct,const char *name)
{
assert(ct);
assert(name);
int i = 0;
for (; i < ct->size; i++){
person_t *p = ct->friends + i;
if (strcmp(name, p->name) == 0){
return i;
}
}
return -1;
}
void SerchFriend(contact_t *ct)
{
assert(ct);
printf("请输入你要查找的人姓名# ");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct,name);
if (i >= 0){
person_t *p = ct->friends + i;
printf("|%-7s|%-7s|%-7d|%-7s|%-7s|\n", p->name, p->sex, p->age, p->telphone, p->address);
}
else{
printf("你要查找的人 %s 不存在!\n", name);
}
return;
}
void ClearContact(contact_t *ct)
{
assert(ct);
ct->size = 0;
}
void DelFriend(contact_t *ct)
{
assert(ct);
assert(ct);
printf("请输入你要删除的人的姓名# ");
char name[NAME_SIZE];
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
ct->friends[i] = ct->friends[ct->size - 1];
ct->size -= 1;
}
else{
printf("你要删除的人 %s 不存在!\n", name);
}
}
void ShowContact(contact_t *ct)
{
assert(ct);
int i = 0;
printf("\n |cap: %6d |size: %6d | \n\n", ct->cap, ct->size);
printf("|%-7s|%-7s|%-7s|%-7s|%-7s|\n", "姓名","性别","年纪","电话","地址");
for (; i < ct->size; i++){
person_t *p = ct->friends + i;
printf("|%-7s|%-7s|%-7d|%-7s|%-7s|\n", p->name, p->sex, p->age, p->telphone, p->address);
}
printf("\n");
}
static int CmpPerson(const void*p1, const void*p2)
{
assert(p1);
assert(p2);
person_t *_p1 = (person_t *)p1;
person_t *_p2 = (person_t *)p2;
return strcmp(_p1->name, _p2->name);
}
void SortContact(contact_t *ct)
{
assert(ct);
if (!IsEmpty(ct)){
qsort(ct->friends, ct->size, sizeof(person_t), CmpPerson);
}
}
void SaveContact(contact_t *ct)
{
assert(ct);
FILE *fp = fopen(SAVE_FILE, "wb");
if (fp == NULL){
printf("save error!\n");
return;
}
fwrite(ct, sizeof(contact_t), 1, fp);
fwrite(ct->friends, sizeof(person_t),ct->size,fp);
fclose(fp);
}
void ChangeContact(contact_t *ct)
{
assert(ct);
char name[64];
printf("请输入要修改的用户的姓名:\n");
scanf("%s", name);
int i = SearchCore(ct, name);
if (i >= 0){
person_t *p = ct->friends + i;
printf("你想修改什么信息?1.姓名,2.年龄,3.性别,4.电话,5.地址,请输入:\n");
int get = 0;
scanf("%d", &get);
switch (get){
case 1:
printf("请输入修改后的姓名:\n");
scanf("%s",p->name);
break;
case 2:
printf("请输入修改后的年龄:\n");
scanf("%d", p->age);
break;
case 3:
printf("请输入修改后的性别:\n");
scanf("%s", p->sex);
break;
case 4:
printf("请输入修改后的电话:\n");
scanf("%s", p->telphone);
break;
case 5:
printf("请输入修改后的地址:\n");
scanf("%s", p->address);
break;
default:
printf("信息错误!\n");
break;
}
if (get){
printf("修改成功!\n");
}
else
printf("修改失败\n");
}
else
printf("该联系人不存在!\n");
}
欢迎大家点赞批评指正!