//通讯录可以用来存储1000个人的信息,每个人的信息包括:
//姓名、性别、年龄、电话、住址
//提供方法:
//1. 添加联系人信息
//2. 删除指定联系人信息
//3. 查找指定联系人信息
//4. 修改指定联系人信息
//5. 显示所有联系人信息
//6. 清空所有联系人
//7. 以名字排序所有联系人
contact.h
#ifndef _CONTACT_H_
#define _CONTACT_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, *pContact;
void InitContact(pContact pcon);
void AddContact(pContact pcon);
void ShowContact(const pContact pcon);
void DelContact(pContact pcon);
void SearchContact(pContact pcon);
void ModifyContact(pContact pcon);
void SortContach(pContact pcon);
void ClearContact(pContact pcon);
#endif
contact.c
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"contact.h"
void InitContact(pContact pcon)
{
int i = 0;
for (i = 0; i < MAX; i++)
{
strcpy(pcon->data[i].name, " ");
pcon->data[i].age = 0;
strcpy(pcon->data[i].sex, " ");
strcpy(pcon->data[i].addr, " ");
strcpy(pcon->data[i].tele, " ");
}
pcon->sz = 0;
}
void AddContact(pContact pcon)
{
if (pcon->sz <= MAX)
{
printf("请输入姓名:>\n");
scanf("%s", pcon->data[pcon->sz].name);
printf("请输入年龄:>\n");
scanf("%d", &(pcon->data[pcon->sz].age));
printf("请输入性别:>\n");
scanf("%s", pcon->data[pcon->sz].sex);
printf("请输入家庭住址:>\n");
scanf("%s", pcon->data[pcon->sz].addr);
printf("请输入联系电话:>\n");
scanf("%s", pcon->data[pcon->sz].tele);
(pcon->sz)++;
printf("添加成功!\n");
}
else
{
printf("通讯录已满!\n");
}
}
void ShowContact(const pContact pcon)
{
int i;
printf("%-3s\t\t%-3s\t\t%-3s\t\t%-3s\t\t%-3s\n", "name", "age", "sex", "addr", "tele");
for (i = 0; i < (pcon->sz);i++)
{
printf("%-3s\t\t%-d\t\t%-3s\t\t%-3s\t\t%-3s\n", pcon->data[i].name, pcon->data[i].age, pcon->data[i].sex, pcon->data[i].addr, pcon->data[i].tele);
}
}
int find(char* s, pContact pcon)
{
int i = 0;
int in = 0;
for (i = 0; i < pcon->sz; i++)
{
if (strcmp(pcon->data[i].name, s) == 0)
{
return i;
}
}
return -1;
}
void DelContact(pContact pcon)
{
char nam[NAME_MAX];
int f = 0;
printf("请输入要删除的人的姓名:\n");
scanf("%s", nam);
f = find(nam, pcon);
if (f != -1)
{
for (f; f < (pcon->sz) - 1; f++)
{
pcon->data[f] = pcon->data[f + 1];
}
(pcon->sz)--;
printf("删除成功!\n");
}
else
{
printf("没有此人!\n");
}
}
void SearchContact(pContact pcon)
{
char nam[NAME_MAX];
int f = 0;
printf("请输入要查找的人的姓名:\n");
scanf("%s", nam);
f = find(nam, pcon);
if (f != -1)
{
printf("%-3s\t\t%-3s\t\t%-3s\t\t%-3s\t\t%-3s\n", "name", "age", "sex", "addr", "tele");
printf("%-3s\t\t%-d\t\t%-3s\t\t%-3s\t\t%-3s\n", pcon->data[f].name, pcon->data[f].age, pcon->data[f].sex, pcon->data[f].addr, pcon->data[f].tele);
}
else
{
printf("没有找到此人!\n");
}
}
void ModifyContact(pContact pcon)
{
char nam[NAME_MAX];
int f = 0;
printf("请输入要修改的人的姓名:\n");
scanf("%s", nam);
f = find(nam, pcon);
if (f != -1)
{
printf("请输入姓名:>\n");
scanf("%s", pcon->data[f].name);
printf("请输入年龄:>\n");
scanf("%d", &(pcon->data[f].age));
printf("请输入性别:>\n");
scanf("%s", pcon->data[f].sex);
printf("请输入家庭住址:>\n");
scanf("%s", pcon->data[f].addr);
printf("请输入联系电话:>\n");
scanf("%s", pcon->data[f].tele);
printf("修改成功!\n");
}
else
{
printf("没有找到此人!\n");
}
}
void SortContach(pContact pcon)
{
int i = 0;
int in = 0;
printf("*********** 1.升序 2.降序 *************\n");
printf("请选择升序还是降序:\n");
scanf("%d", &in);
for (int j = 0; j < (pcon->sz) - 1; j++)
{
for (i = 0; i < (pcon->sz) - 1 - j; i++)
{
PeoInfo temp;
if (in == 1)
{
if (strcmp(pcon->data[i].name, pcon->data[i + 1].name) >0)
{
temp = pcon->data[i];
pcon->data[i] = pcon->data[i + 1];
pcon->data[i + 1] = temp;
}
}
else if (in == 2)
{
if (strcmp(pcon->data[i].name, pcon->data[i + 1].name) < 0)
{
temp = pcon->data[i];
pcon->data[i] = pcon->data[i + 1];
pcon->data[i + 1] = temp;
}
}
}
}
}
void ClearContact(pContact pcon)
{
int i = 0;
for (i = 0; i < pcon->sz; i++)
{
strcpy(pcon->data[i].name, " ");
pcon->data[i].age = 0;
strcpy(pcon->data[i].sex, " ");
strcpy(pcon->data[i].addr, " ");
strcpy(pcon->data[i].tele, " ");
}
pcon->sz = 0;
printf("已经成功清除所有通讯录!\n");
}
void menu(void)
{
printf("*************************************\n");
printf("********* 1.添加 2.删除 ***********\n");
printf("********* 3.查找 4.修改 ***********\n");
printf("********* 5.显示 6.清空 ***********\n");
printf("********* 7.排序 0.退出 ***********\n");
printf("*************************************\n");
}
int main()
{
Contact pcon;
InitContact(&pcon);
int input = 1;
while (input)
{
menu();
printf("请选择菜单:>\n");
scanf("%d", &input);
switch (input)
{
case 1:
AddContact(&pcon);
break;
case 2:
DelContact(&pcon);
break;
case 3:
SearchContact(&pcon);
break;
case 4:
ModifyContact(&pcon);
break;
case 5:
ShowContact(&pcon);
break;
case 6:
ClearContact(&pcon);
break;
case 7:
SortContach(&pcon);
break;
case 0:
exit(1);
break;
default:
printf("选择有误,请重新选择!\n");
break;
}
}
return 0;
}