目录
通讯录:
1. 可以存放1000个人的信息
2. 人的信息:名字,年龄,电话,住址,性别
3. 增加联系人
4. 删除联系人
5. 查找联系人
6. 修改联系人
7. 排序(名字 / 年龄)
contact.h
#pragma once
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
#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;
enum Oprtion
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
SHOW
};
void InitContact(Contact* pc);
//1
void AddContact(Contact* pc);
//2
void DeletContact(Contact* pc);
//3
void SearchContact(Contact* pc);
//4
void ModifyContact(Contact* pc);
//5
void SortContact(Contact* pc);
//6
void ShowContact(Contact* pc);
contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
void InitContact(Contact* pc)
{
pc->sz = 0;
memset(pc->data, 0, sizeof(pc->data));
}
void AddContact(Contact* 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].addr);
printf("请输入电话:>");
scanf("%s", pc->data[pc->sz].tele);
pc->sz++;
printf("添加联系人成功\n");
}
int FindByName(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 ShowContact(Contact* pc)
{
printf("%-10s %-10s %-10s %-20s %-20s\n", "姓名", "年龄", "性别", "地址", "电话");
int i = 0;
for (i = 0; i < pc->sz; i++)
printf("%-10s %-10d %-10s %-20s %-20s\n", pc->data[i].name,
pc->data[i].age, pc->data[i].sex, pc->data[i].addr, pc->data[i].tele);
}
void DeletContact(Contact* pc)
{
if (pc->sz == 0)
{
printf("通讯录已空\n");
return;
}
char name[10] = { 0 };
printf("请输入要删的姓名:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("要删的人不存在\n");
}
else
{
int j = 0;
for (j = pos; j < pc->sz - 1; j++)
{
pc->data[j] = pc->data[j + 1];
}
pc->sz--;
printf("成功删除\n");
}
}
void SearchContact(Contact* pc)
{
char name[10] = { 0 };
printf("请输入查找人的名字:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("搜索不到此人\n");
}
else
{
printf("有此人,信息是:>\n");
printf("%-10s %-10s %-10s %-20s %-20s\n", "姓名", "年龄", "性别", "地址", "电话");
printf("%-10s %-10d %-10s %-20s %-20s\n", pc->data[pos].name,
pc->data[pos].age, pc->data[pos].sex, pc->data[pos].addr, pc->data[pos].tele);
}
}
void ModifyContact(Contact* pc)
{
char name[10] = { 0 };
printf("请输入被修改人的名字:>");
scanf("%s", name);
int pos = FindByName(pc, name);
if (pos == -1)
{
printf("搜索不到此人\n");
}
else
{
printf("有此人,信息是:>\n");
printf("%-10s %-10s %-10s %-20s %-20s\n", "姓名", "年龄", "性别", "地址", "电话");
printf("%-10s %-10d %-10s %-20s %-20s\n", pc->data[pos].name,
pc->data[pos].age, pc->data[pos].sex, pc->data[pos].addr, pc->data[pos].tele);
printf("开始修改\n请输入姓名:>");
scanf("%s", pc->data[pos].name);
printf("请输入年龄:>");
scanf("%d", &(pc->data[pos].age));
printf("请输入性别:>");
scanf("%s", pc->data[pos].sex);
printf("请输入地址:>");
scanf("%s", pc->data[pos].addr);
printf("请输入电话:>");
scanf("%s", pc->data[pos].tele);
printf("修改成功\n");
}
}
int qsort_by_name(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
int qsort_by_age(const void* e1, const void* e2)
{
return ((PeoInfo*)e1)->age - ((PeoInfo*)e2)->age;
}
void SortContact(Contact* pc)
{
printf("按名字排序按1\n");
printf("按年龄排序按2:>\n");
int a = 0;
scanf("%d", &a);
if (a == 1)
{
qsort(pc->data, pc->sz, sizeof(pc->data[0]), qsort_by_name);
printf("按名字排序成功\n");
}
else
{
qsort(pc->data, pc->sz, sizeof(pc->data[0]), qsort_by_age);
printf("按年龄排序成功\n");
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
void menu()
{
printf("***************************************\n");
printf("******** 1.add 2.del *****\n");
printf("******** 3.search 4.modify *****\n");
printf("******** 5.sort 6.show *****\n");
printf("******** 0.exit *****\n");
printf("***************************************\n");
}
int main()
{
int input = 0;
Contact con;
InitContact(&con);
do
{
menu();
printf("请选择-> ");
scanf("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DeletContact(&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;
default:
printf("输入错误\n");
break;
}
} while (input);
return 0;
}