实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
4. 修改指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
8. 保存联系人到文件
9. 加载联系人
10.排序所有联系人
//contact.h
//contact.h
#ifndef __CONTACT__H__
#define __CONTACT__H__
#include <stdio.h>
#include <string.h>
#include <assert.h>
enum Option
{
EXIT,
ADD,
SEAR,
DEL,
SHOW,
CLEAR,
SORT
};
#define MAX_NAME 20
#define MAX_TEL 11
#define MAX_ADDR 15
#define MAX_SEX 3
#define MAX_NUMPERSON 1000
//个人信息
typedef struct PersonInfo
{
//name age tele addr sex
char name[MAX_NAME];
short age;
char tele[MAX_TEL];
char addr[MAX_TEL];
char sex[MAX_SEX];
}PersonInfo;
//通讯录
typedef struct Contact
{
PersonInfo per[MAX_NUMPERSON];
int usedSize;//被使用的个数
}Contact;
void InitContact(Contact *pCon);
void ADDContact(Contact*pCon);
int SearchContact(Contact*pCon);
void DelContact(Contact*pCon);
void ShowContact(Contact*pCon);
void ClearContact(Contact*pCon);
void SqrtContact(Contact*pCon);
#endif// __CONTACT__H__
//contact.c
//contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
void InitContact(Contact *pCon)
{
assert(pCon != NULL);
pCon->usedSize = 0;
memset(pCon->per, 0, sizeof(pCon->per));
}
void ADDContact(Contact*pCon)
{
if (pCon->usedSize == MAX_NUMPERSON)
{
printf("通讯录满了!!!\n");
}
assert(pCon != NULL);
printf("请输入姓名: ");
scanf("%s", pCon->per[pCon->usedSize].name);
printf("请输入性别: ");
scanf("%s", pCon->per[pCon->usedSize].sex);
printf("请输入年龄: ");
scanf("%d", &(pCon->per[pCon->usedSize].age));
printf("请输入电话: ");
scanf("%s", pCon->per[pCon->usedSize].tele);
printf("请输入地址: ");
scanf("%s", pCon->per[pCon->usedSize].addr);
pCon->usedSize++;
}
int SearchContact(Contact*pCon)
{
int i = 0;
char name[20] = { 0 };
assert(pCon != NULL);
if (pCon->usedSize==0)
{
printf("通讯录为空!!!\n");
return -1;
}
printf("请输入要查找的姓名: ");
scanf("%s", &name);
for (i = 0; i < pCon->usedSize;i++)
{
if (strcmp(name, pCon->per[pCon->usedSize].name) == 0);
{
return i;
}
}
return -1;
printf("查无此人!!!\n");
}
void DelContact(Contact*pCon)
{
int index = SearchContact(pCon);
int i = 0;
assert(pCon != NULL);
if (index == -1)
{
printf("查无此人!!!\n");
return;
}
for (i = index; i < pCon->usedSize-1;i++)
{
pCon->per[i] = pCon->per[i + 1];
}
printf("删除成功\n");
}
void ShowContact(Contact*pCon)
{
int i = 0;
assert(pCon != NULL);
printf("%-10s%-10s%-10s%-10s%-11s%\n", "姓名", "性别", "年龄", "地址","电话");
for (i = 0; i < pCon->usedSize; i++)
{
printf("%-10s%-10s%-10d%-10s%-11s%\n", pCon->per[i].name, pCon->per[i].sex, pCon->per[i].age, pCon->per[i].addr, pCon->per[i].tele);
printf("\n");
}
}
void ClearContact(Contact*pCon)
{
for (int i = 0; i <pCon->usedSize; i++)
{
memset(pCon->per, 0, sizeof(pCon->per));
}
}
void SortContact(Contact *pCon)
{
int i = 0;
int j = 0;
for (i = 0; i < pCon->usedSize - 1; i++)
{
for (j = 0; j < pCon->usedSize - 1 - i; j++)
{
if ((strcmp(pCon->per[j].name, pCon->per[j + 1].name))>0)
{
PersonInfo tmp;
tmp = pCon->per[j];
pCon->per[j] = pCon->per[j + 1];
pCon->per[j + 1] = tmp;
}
}
}
printf("排序成功\n");
}
//通讯录.c
//通讯录.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "Contact.h"
/*
1. 添加联系人信息
2. 删除指定联系人信息
3. 查找指定联系人信息
5. 显示所有联系人信息
6. 清空所有联系人
7. 以名字排序所有联系人
8. 保存联系人到文件
9. 加载联系人
*/
void menu()
{
printf("*************欢迎进入通讯录系统****************\n");
printf("*********1.add*************2.search************\n");
printf("*********3.del*************4.show**************\n");
printf("*********5.clear***********6.sort**************\n");
printf("*******************0.exit**********************\n");
printf("***********************************************\n");
}
void start()
{
int input = 0;
Contact con;
InitContact(&con);
int ret = 0;
do
{
menu();
printf("请输入你的操作>>");
scanf("%d", &input);
switch (input)
{
case ADD:
ADDContact(&con);
break;
case SEAR:
ret = SearchContact(&con);
if (ret != -1)
{
printf("%-10s%-10s%-10s%-10s%-11s%\n", "姓名", "性别", "年龄", "地址", "电话");
printf("%-10s%-10s%-10d%-10s%-11s%\n", con.per[ret].name, con.per[ret].sex, con.per[ret].age, con.per[ret].addr, con.per[ret].tele);
}
break;
case DEL:
DelContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case CLEAR:
ClearContact(&con);
break;
case SORT:
SortContact(&con);
break;
default:
printf("输入错误!!!\n");
break;
}
} while (input);
}
int main()
{
start();
system("pause");
return 0;
}