1.构造通讯录
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<string.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include<stdlib.h>
#define MAX 1000
#define NAME_MAX 20
#define SEX_MAX 5
#define ADDR_MAX 30
#define TELE_MAX 12
typedef struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char addr[ADDR_MAX];
char tele[TELE_MAX];
}PeoInfo;
//通讯录的结构体
typedef struct Contact
{
PeoInfo data[MAX];//存放数据
int sz;//通讯录中有效信息的个数
}Contact;
动态版
typedef struct Contact
{
PeoInfo* data;//存放数据
int sz;//通讯录中有效信息的个数
int capacity;
}Contact;
2.基本框架
enum Oprion
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
SHOW,
DEST
};
void menu()
{
printf("***************************************\n");
printf("******** 1.add 2.del *****\n");
printf("******** 3.search 4.modify *****\n");
printf("******** 5.sort 6.show *****\n");
printf("******** 7.destory 0.exit *****\n");
printf("***************************************\n");
}
int main()
{
int input = 0;
Contact con = { 0 };//通讯录
//初始化通讯录
InitContact(&con);
do
{
menu();
printf("请选选择:>");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DeleteContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SORT:
SortContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case EXIT:
printf("退出通讯录\n");
break;
case DEST:
destoryContact(&con);
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
3.实现每个功能
1.初始化
void InitContact(Contact* pc)
{
assert(pc);
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
//动态版
void InitContact(Contact* pc)
{
assert(pc);
pc->sz = 0;
pc->data = (PeoInfo*)malloc(DEFAULT_SZ * sizeof(PeoInfo));
pc->capacity = DEFAULT_SZ;
}
2.增加联系人?
//动态版
void check_capacity(Contact* pc)
{
assert(pc);
if (pc->sz == pc->capacity)
{
PeoInfo* tmp = (PeoInfo*)realloc(pc->data, (pc->capacity + 2) * sizeof(PeoInfo));
if (tmp != NULL)
{
pc->data = tmp;
pc->capacity += 2;
printf("增容成功\n");
}
else
{
printf("check_capacity()::%s\n", strerror(errno));
}
}
}
void AddContact(Contact* pc)
{
assert(pc);
//check_capacity(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");
}
3.展示联系人?
void ShowContact(const Contact* pc)
{
assert(pc);
if (pc->sz == 0)
{
printf("无联系人\n");
return;
}
int i = 0;
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
for (i = 0; i < pc->sz; i++)
{
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr);
}
}
4.删除联系人?
将查找联系人的函数分离出来,找到该联系人,返回其下标
int FindByName(const Contact* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;//找不到
}
void DeleteContact(Contact* pc)
{
char name[NAME_MAX] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法删除\n");
return;
}
printf("请输入要删除人的名字:>");
scanf("%s", name);
//查找指定联系人
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要删除的人不存在\n");
return;
}
else
{
//删除
int j = 0;
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("删除指定联系人成功\n");
}
}
5.查找联系人
void SearchContact(Contact* pc)
{
char name[NAME_MAX] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法查找\n");
return;
}
printf("请输入要查找人的名字:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要查找的人不存在\n");
return;
}
else
{
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
}
}
?6.修改联系人
void ModifyContact(Contact* pc)
{
printf("请选择要修改的联系人\n");
char name[NAME_MAX] = { 0 };
if (pc->sz == 0)
{
printf("通讯录为空,无法修改\n");
return;
}
printf("请输被修改人的名字:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("被修改的人不存在\n");
return;
}
else
{
printf("%-10s\t%-5s\t%-5s\t%-13s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址");
printf("%-10s\t%-5d\t%-5s\t%-13s\t%-20s\n",
pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr);
printf("请输入要被修改的内容\n");
printf("****1.名字******2.年龄**********\n");
printf("****3.性别******4.电话**********\n");
printf("****5.住址**********************\n");
int input = 0;
scanf("%d", &input);
switch(input)
{case 1:
printf("请重新输入姓名\n");
scanf("%s", pc->data[pos].name);
printf("修改成功\n");
break;
case 2:
printf("请重新输入年龄\n");
scanf("%d", &(pc->data[pos].age));
printf("修改成功\n");
break;
case 3:
printf("请重新输入性别\n");
scanf("%s", pc->data[pos].sex);
printf("修改成功\n");
break;
case 4:
printf("请重新输入电话\n");
scanf("%s", pc->data[pos].tele);
printf("修改成功\n");
break;
case 5:
printf("请重新输入地址\n");
scanf("%s", pc->data[pos].addr);
printf("修改成功\n");
break;
default:
printf("选择错误\n");
break;
}
}
}
7.对联系人进行排序?
int compare_by_age(const void* e1,const void* e2)
{
return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
int compare_by_name(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
printf("*****1.名字*****2.年龄*****\n");
printf("请选择排序的方法\n");
int input = 0;
scanf("%d",&input);
switch (input)
{case 1:
qsort(pc->data, pc->sz, sizeof(pc->data[0]), compare_by_name);
printf("按名字排序成功\n");
break;
case 2:
qsort((pc->data), pc->sz, sizeof(pc->data[0]), compare_by_age);
printf("按年龄排序成功\n");
break;
default:
printf("选择错误\n");
break;
}
}
8.销毁通讯录
void destoryContact(Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
printf("以清空所有联系人\n");
}
//动态版
void destoryContact(Contact* pc)
{
free(pc->data);
pc->data = NULL;
pc->sz = 0;
pc->capacity = 0;
printf("以清空所有联系人\n");
}
|