4. 利用自定义类型实现简易通讯录程序
通讯录.c
#include "contact.h"
void menu()
{
printf("******************************\n");
printf("**** 1. 添加 2. 删除 **\n");
printf("**** 3. 搜索 4. 修改 **\n");
printf("**** 5. 展示全部 6. 排序 **\n");
printf("**** 0. 退出 **\n");
printf("******************************\n");
}
int main()
{
int input = 0;
//创建一个通讯录
struct Contact con;
//初始化通讯录
InitContact(&con);
do
{
menu();
printf("请选择:>");
scanf_s("%d", &input);
switch (input)
{
case ADD:
AddContact(&con);
break;
case DEL:
DeletContact(&con);
break;
case SHOW:
ShowContact(&con);
break;
case MODIFY:
ModifyContact(&con);
break;
case SEARCH:
SearchContact(&con);
break;
case SORT:
SortContact(&con);
break;
case EXIT:
printf("退出通讯录\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
contact.c
#include "contact.h"
void InitContact(struct Contact* pc)
{
pc->sz = 0;//默认没有信息
//memset(pc->data, 0, MAX*sizeof(struct PeoInfo));
memset(pc->data, 0, sizeof(pc->data));
}
void AddContact(struct Contact* pc)
{
if (pc->sz == MAX)
{
printf("通讯录满了\n");
}
else
{
printf("请输入名字:>");
scanf_s("%s", pc->data[pc->sz].name,30);
printf("请输入年龄:>");
scanf_s("%d", &(pc->data[pc->sz].age));
printf("请输入性别:>");
scanf_s("%s", pc->data[pc->sz].sex,5);
printf("请输入电话:>");
scanf_s("%s", pc->data[pc->sz].tele,12);
printf("请输入地址:>");
scanf_s("%s", pc->data[pc->sz].addr,30);
printf("添加成功\n");
pc->sz++;
ShowContact(pc);
}
}
void DeletContact(struct Contact* pc)
{
printf("请输入需要删除的联系人姓名\n");
char name[30]="0";
scanf_s("%s", name, 30);
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(name, pc->data[i].name) == 0)
{
for (int j = i; j < pc->sz; j++)
{
strcpy_s(pc->data[j].name, 30, pc->data[j + 1].name);
strcpy_s(pc->data[j].sex, 5, pc->data[j + 1].sex);
strcpy_s(pc->data[j].tele, 12, pc->data[j + 1].tele);
strcpy_s(pc->data[j].addr, 30, pc->data[j + 1].addr);
pc->data[j].age = pc->data[j + 1].age;
}
printf("删除成功\n");
(pc->sz)--;
ShowContact(pc);
}
}
}
void ModifyContact(struct Contact* pc)
{
printf("请输入需要修改的联系人姓名\n");
char name[30]="0";
scanf_s("%s", name, 30);
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(name, pc->data[i].name) == 0)
{
printf("请输入名字:>");
scanf_s("%s", pc->data[i].name, 30);
printf("请输入年龄:>");
scanf_s("%d", &(pc->data[i].age));
printf("请输入性别:>");
scanf_s("%s", pc->data[i].sex, 5);
printf("请输入电话:>");
scanf_s("%s", pc->data[i].tele, 12);
printf("请输入地址:>");
scanf_s("%s", pc->data[i].addr, 30);
printf("修改成功!\n");
ShowContact(pc);
}
}
}
void ShowContact(struct Contact* pc)
{
int i = 0;
printf("序号\t%10s\t%10s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "tele", "addr");
for (i = 0; i < pc->sz; i++)
{
//打印每一个数据
printf("%d\t%10s\t%10d\t%8s\t%15s\t%30s\n",
i+1,
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
}
void SearchContact(struct Contact* pc)
{
printf("请输入需要搜索的联系人姓名\n");
char name[30]="0";
scanf_s("%s",name,30);
for (int i = 0; i < pc->sz; i++)
{
if (strcmp(name , pc->data[i].name) == 0)
{
printf("序号\t%10s\t%10s\t%8s\t%15s\t%30s\n", "name", "age", "sex", "tele", "addr");
printf("%d\t%10s\t%10d\t%8s\t%15s\t%30s\n",
i + 1,
pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
return;
}
}
printf("找不到联系人信息\n");
}
void SortContact(struct Contact* pc)
{
struct PeoInfo temp;
for(int j = 0;j < pc->sz-1;j++)
for (int i = 0; i < pc->sz-1-j; i++)
{
if (strcmp(pc->data[i].name, pc->data[i + 1].name) > 0)
{
temp = pc->data[i + 1];
pc->data[i + 1] = pc->data[i];
pc->data[i] = temp;
}
}
ShowContact(pc);
}
contact.h
#pragma once
#define NAME_MAX 30
#define SEX_MAX 5
#define TELE_MAX 12
#define ADDR_MAX 30
#define MAX 1000
#include <string.h>
#include <stdio.h>
#include <string.h>
//创建枚举变量
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SHOW,
SORT
};
//描述人的信息
struct PeoInfo
{
char name[NAME_MAX];
int age;
char sex[SEX_MAX];
char tele[TELE_MAX];
char addr[ADDR_MAX];
};
//通讯录
struct Contact
{
struct PeoInfo data[MAX];//1000个人的数据存放在data数组中
int sz;//记录当前通讯录有效信息的个数
};
//初始化通讯录
void InitContact(struct Contact* pc);
//增加联系人
void AddContact(struct Contact* pc);
//删除联系人
void DeletContact(struct Contact* pc);
//修改联系人信息
void ModifyContact(struct Contact* pc);
//搜索联系人信息
void SearchContact(struct Contact* pc);
//显示所有的联系人
void ShowContact(struct Contact* pc);
//按姓氏排序联系人信息
void SortContact(struct Contact* pc);
版权声明:本文为CSDN博主「敲代码的布莱恩特」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/DerrickWestbrook/article/details/120380553