Contact.c
刚学完结构体,用C实现一个增删查改、排序、打印的通讯录
#include"Contact.h"
//初始化通讯录
void InitContact(Contact* Con)
{
assert(Con != NULL);
Con->sz = 0;
memset(Con, 0, sizeof(Con));
}
//增加联系人
void AddContact(Contact* Con)
{
if (Con->sz >= ContactMax)
{
printf("通讯录满了,无法添加联系人\n");
return;
}
printf("姓名:\n");
scanf("%s", Con->date[Con->sz].Name);
printf("年龄:\n");
scanf("%d", &(Con->date[Con->sz].Age));
printf("地址:\n");
scanf("%s", Con->date[Con->sz].Gender);
printf("电话:\n");
scanf("%s",Con->date[Con->sz].Tell);
printf("性别:\n");
scanf("%s", Con->date[Con->sz].Address);
printf("添加成功\n");
Con->sz++;
}
//删除联系人
void DelContact(Contact* Con)
{
if (Con->sz == 0)
{
printf("通讯录没有成员\n");
return;
}
char name[NameMax] = "\0";
printf("请输入要删除的人姓名\n");
scanf("%s", name);
int ret = FindContact(Con,name);
if (ret == -1)
{
printf("通讯录没有该成员\n");
return;
}
int i = 0;
for (i = ret; i < Con->sz - 1; i++)
{
Con->date[i] = Con->date[i + 1];
}
printf("删除成功\n");
Con->sz--;
}
int FindContact(Contact* Con,char* peo)
{
int i = 0;
for (i = 0; i < Con->sz; i++)
{
if (0 == strcmp(peo, Con->date[i].Name))
{
printf("找到了\n");
return i;
}
}
printf("找不到\n");
return -1;
}
//查找联系人
void SearchContact(Contact* Con)
{
if (Con->sz == 0)
{
printf("通讯录没有成员\n");
}
char name[NameMax] = "\0";
printf("请输入要查找的人姓名\n");
scanf("%s", name);
int ret=FindContact(Con,name);
if (ret == -1)
{
printf("没有该联系人\n");
return;
}
printf("%-20s %-5d %-20s %-10s %-5s\n", Con->date[ret].Name, Con->date[ret].Age, Con->date[ret].Gender, Con->date[ret].Tell, Con->date[ret].Address);
printf("\n");
}
//打印联系人
void PrintCantact(Contact* Con)
{
if (Con->sz == 0)
{
printf("通讯录没有成员!\n");
return;
}
for (int j = 0; j < Con->sz; j++)
{
printf("%-20s %-5d %-20s %-10s %-5s\n", Con->date[j].Name, Con->date[j].Age, Con->date[j].Gender, Con->date[j].Tell, Con->date[j].Address);
printf("\n");
}
}
//修改联系人
void ModifyContact(Contact* Con)
{
if (Con->sz == 0)
{
printf("通讯录为空\n");
return;
}
char name[NameMax] = "\0";
printf("请输入要修改的对象\n");
scanf("%s", name);
int ret=FindContact(Con, name);
if (ret == -1)
{
printf("没有符合的联系人\n");
return;
}
printf("姓名:\n");
scanf("%s", Con->date[ret].Name);
printf("年龄:\n");
scanf("%d", &(Con->date[ret].Age));
printf("地址:\n");
scanf("%s", Con->date[ret].Gender);
printf("电话:\n");
scanf("%s", Con->date[ret].Tell);
printf("性别:\n");
scanf("%s", Con->date[ret].Address);
printf("修改成功\n");
}
void SortContact(Contact* Con,Peoinfo* Peo)
{
int i = 0;
int j = 0;
for (i = 0; i < Con->sz-1; i++)
{
for (j = 0; j < Con->sz-1-i; j++)
{
if (0 < strcmp(Con->date[j].Name, Con->date[j + 1].Name))
{
*Peo = Con->date[j + 1];
Con->date[j + 1] = Con->date[j];
Con->date[j] =* Peo;
}
}
}
PrintCantact(Con);
}
Contact.h
#define _CRT_SECURE_NO_WARNINGS 1
//声明
#include<stdio.h>
#include<string.h>
#include<assert.h>
#define NameMax 20
#define GenderMax 5
#define TellMax 12
#define AddressMax 20
#define ContactMax 1000
typedef struct Peoinfo
{
char Name[NameMax];
int Age;
char Gender[GenderMax];
char Tell[TellMax];
char Address[AddressMax];
}Peoinfo;
typedef struct Contcat
{
Peoinfo date[ContactMax];
int sz;
}Contact;
//函数声明
//初始化结构体函数声明
void InitContact(Contact* Con);
//增加联系人函数声明
void AddContact(Contact* Con);
//打印联系人函数声明
void PrintCantact(Contact* Con);
//删除联系人函数声明
void DelContact(Contact* Con);
//查找联系人声明
void SearchContact(Contact* Con);
//查找函数声明
int FindContact(Contact* Con,char* peo);
//修改联系人
void ModifyContact(Contact* Con);
//排序联系人函数声明
void SortContact(Contact* Con,Peoinfo* Peo);
Test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"Contact.h"
void menu()
{
printf(" <<HJContact>> \n");
printf(" 1.Add 2.Del \n");
printf(" 3.search 4.modify \n");
printf(" 5.sort 6.print \n");
printf(" 0.exit \n");
}
void test()
{
int input = 0;
Contact Con;
Peoinfo Peo;
InitContact(&Con);
do
{
menu();
printf("请输入要操作的数\n");
scanf("%d", &input);
//初始化结构体Con
switch (input)
{
case 0:
printf("退出程序\n");
break;
case 1:
AddContact(&Con);
break;
case 2:
DelContact(&Con);
break;
case 3:
SearchContact(&Con);
break;
case 4:
ModifyContact(&Con);
break;
case 5:
SortContact(&Con,&Peo);
break;
case 6:
PrintCantact(&Con);
break;
default:
printf("请输入数字0~6操作数据库\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}