#pragma once
#define SUM 100
#define NAME 10
#define TELE 12
#define ADDR 20
#include
#include
#include
//枚举操作选项
enum Option
{
EXIT,
ADD,
DELETE,
FIND,
MODIFY,
SHOW,
SORT
};
//每个联系人信息类型
struct Peoinfo
{
char name[NAME];
int age;
char tele[TELE];
char address[ADDR];
};
//通讯录数据
struct Contact
{
struct Peoinfo data[SUM]; //通讯录
int sz; //统计联系人个
};
//菜单显示
void menu();
//添加联系人
void ADD_Contact(struct Contact* pA);
//显示联系人
void SHOW_Contact(struct Contact* pSH);
//查询联系人
void FIND_Contact(struct Contact* pF);
//修改联系人
void MODIFY_Contact(struct Contact* pM);
//删除联系人
void DELETE_Contact(struct Contact* pD);
//联系人排序
void SORT_Contact(struct Contact* pSO);
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
//菜单显示
void menu()
{
printf("*****************************\n");
printf("***** 1.ADD 2.DELETE *****\n");
printf("***** 3.FIND 4.MODIFY *****\n");
printf("***** 5.SHOW 6.SORT *****\n");
printf("***** 0.EXIT *****\n");
printf("*****************************\n");
}
//按姓名查找联系人(没有在头文件中声明,只能在该.c文件中使用)
int FindByName(const struct Contact* C,const char* N)
{
int i = 0;
for (i = 0; i < C->sz; i++)
{
if (0 == strcmp(&(C->data[i].name), N))
{
printf("找到了:\n");
printf("%-10s%-10s%-20s%-20s\n", "姓名", "年龄", "电话", "地址");
printf("%-10s%-10d%-20s%-20s\n", &(C->data[i].name), C->data[i].age, &(C->data[i].tele), &(C->data[i].address));
return i;
}
}
printf("找不到该联系人\n");
return -1;
}
//按姓名排序(没有在头文件中声明,只能在该.c文件中使用)
int CmpByName(const struct Contact* c1, const struct Contact* c2)
{
return strcmp(c1->data->name, c2->data->name);
}
//添加联系人
void ADD_Contact(struct Contact* pA)
{
//1.判断通讯录是否已满
if (pA->sz == SUM)
{
printf("通讯录已满,无法新建联系人\n");
return;
}
//新建联系人
printf("请输入姓名:\n");
scanf("%s", &((pA->data[pA->sz]).name));
printf("请输入年龄:\n");
scanf("%d", &((pA->data[pA->sz]).age));
printf("请输入电话:\n");
scanf("%s", &((pA->data[pA->sz]).tele));
printf("请输入地址:\n");
scanf("%s", &((pA->data[pA->sz]).address));
printf("添加成功\n");
pA->sz++;
}
//显示联系人
void SHOW_Contact(struct Contact* pSH)
{
printf("%-10s%-10s%-20s%-20s\n", "姓名", "年龄", "电话", "地址");
//%-10s:10指的是打印出来显示10个字符的长度空间;-10指的是左对齐(-号)占据10个字符长度空间载打印下一个数据
//如果是%10s,则是右对齐打印10个字符长度空间再打印下一个数据
int i = 0;
for (i = 0; i < pSH->sz; i++)
{
printf("%-10s%-10d%-20s%-20s\n", &(pSH->data[i].name), pSH->data[i].age, &(pSH->data[i].tele), &(pSH->data[i].address));
}
}
//查询联系人
void FIND_Contact(struct Contact* pF)
{
char name[20] = { 0 };
printf("请输入联系人姓名:\n");
scanf("%s", name);
FindByName(pF, name);
}
//修改联系人
void MODIFY_Contact(struct Contact* pM)
{
char name[20] = { 0 };
printf("请输入要修改的联系人姓名:\n");
scanf("%s", name);
int i = FindByName(pM, name);
if (i != -1)
{
printf("请输入姓名:\n");
scanf("%s", &(pM->data[i].name));
printf("请输入年龄:\n");
scanf("%d", &(pM->data[i].age));
printf("请输入电话:\n");
scanf("%s", &(pM->data[i].tele));
printf("请输入地址:\n");
scanf("%s", &(pM->data[i].address));
printf("修改成功\n");
}
}
//删除联系人
void DELETE_Contact(struct Contact* pD)
{
char name[20] = { 0 };
printf("请输入要修改的联系人姓名:\n");
scanf("%s", name);
int i = FindByName(pD, name);
if (i != -1)
{
printf("确认删除请输入:1\n取消删除请输入:2\n");
int j = 0;
scanf("%d", &j);
if (1 == j)
{
for (j = i; j < pD->sz - 1; j++)
{
pD->data[j] = pD->data[j + 1];
}
printf("删除联系人成功\n");
pD->sz--;
}
}
return;
}
//联系人排序
void SORT_Contact(struct Contact* pSO)
{
qsort(pSO->data, pSO->sz, sizeof(pSO->data[0]), CmpByName);//注意qsort()函数的传参问题,练习各类数据的qsort排序
}
#define _CRT_SECURE_NO_WARNINGS
#include"Contact.h"
int main()
{
int input;
struct Contact C = { {0} ,0 };
struct Contact* p = &C;
do
{
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input)
{
case ADD:
ADD_Contact(p);
break;
case DELETE:
DELETE_Contact(p);
break;
case FIND:
FIND_Contact(p);
break;
case MODIFY:
MODIFY_Contact(p);
break;
case SHOW:
SHOW_Contact(p);
break;
case SORT:
SORT_Contact(p);
break;
case EXIT:
break;
default:
break;
}
} while (input);
return 0;
}