当前位置:网站首页>通讯录(一)
通讯录(一)
2022-07-25 18:26:00 【菜菜小蒙】
数组通讯录简单实现:
本通讯录通过数组实现,通过创建通讯录联系人结构体,再创建新结构通讯录内含联系人数组,再通过一个变量来统计联系人数组中的个数。
函数申明:
#pragma once
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#define MAX_CONTACT 100
#define MAX_NAME 20
#define MAX_SEX 5
#define MAX_TELE 15
#define MAX_ADDR 10
typedef struct People
{
char name[MAX_NAME];
int age;
char sex[MAX_SEX];
char tele[MAX_TELE];
char addr[MAX_ADDR];
} Peo;
typedef struct Contact
{
Peo data[MAX_CONTACT];
int count;
}Contact;
//菜单
void menu();
//初始化
void Init(Contact* p);
//增加联系人
void Add(Contact* p);
//删除联系人
void Delete(Contact* p);
//查找联系人
void Search(Contact* p);
//修改联系人
void Modify(Contact* p);
//输出通讯录
void Show(Contact* p);
//排序通讯录
void Sort(Contact* p);
//销毁通讯录
void Destroy(Contact* p);函数实现:
//输入
void scan(Peo arr[], int n)
{
printf("请输入联系人的信息\n");
printf("请输入姓名:");
scanf("%s", arr[n].name);
printf("请输入年龄:");
scanf("%d", &arr[n].age);
printf("请输入性别:");
scanf("%s", arr[n].sex);
printf("请输入电话:");
scanf("%s", arr[n].tele);
printf("请输入地址:");
scanf("%s", arr[n].addr);
}
//比较
int compare(const void* e1, const void* e2)
{
return(strcmp(((Peo*)e1)->name, ((Peo*)e2)->name));
}
//菜单
void menu()
{
printf("*********************************************\n");
printf("****** 1. add 2. del *******\n");
printf("****** 3. search 4. modify *******\n");
printf("****** 5. show 6. sort *******\n");
printf("****** 7. destroy 0. exit *******\n");
printf("*********************************************\n");
}
//初始化
void Init(Contact* p)
{
assert(p);
memset(p->data, 0, sizeof(p->data));
p->count = 0;
}
//增加联系人
void Add(Contact* p)
{
assert(p);
//判断通讯录是否已满
if (p->count == MAX_CONTACT)
{
printf("通讯录已满,无法添加联系人\n");
return;
}
//输入
scan(p->data, p->count);
printf("添加联系人成功\n");
p->count++;
}
//删除联系人
void Delete(Contact* p)
{
assert(p);
char name[20] = { 0 };
if (p->count == 0)
{
printf("通讯录为空\n");
return;
}
printf("请输入联系人的姓名:");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
for (int j = i; j < p->count - 1; j++)
{
p->data[j] = p->data[j + 1];
}
printf("删除成功!\n");
p->count--;
return;
}
}
printf("不存在该联系人\n");
}
//查找联系人
void Search(Contact* p)
{
assert(p);
char name[20] = { 0 };
printf("请输入查询联系人的姓名:");
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(p->data[i].name, name) == 0)
{
printf("%-10s%-15s%-20s%-15s%-8s\n", "姓名", "年龄", "性别", "电话", "住址");
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
return;
}
}
printf("不存在该联系人\n");
}
//修改联系人
void Modify(Contact* p)
{
assert(p);
printf("请输入修改联系人的姓名:");
char name[20] = { 0 };
scanf("%s", name);
for (int i = 0; i < p->count; i++)
{
if (strcmp(name, p->data[i].name) == 0)
{
scan(p->data, i);
return;
}
}
printf("不存在该联系人\n");
}
//输出通讯录
void Show(Contact* p)
{
assert(p);
printf("%-10s%-15s%-20s%-15s%-8s\n", "姓名", "年龄", "性别", "电话", "住址");
for (int i = 0; i < p->count; i++)
{
printf("%-10s%-15d%-20s%-15s%-8s\n", p->data[i].name, p->data[i].age, p->data[i].sex, p->data[i].tele, p->data[i].addr);
}
}
//排序通讯录
void Sort(Contact* p)
{
assert(p);
qsort(p->data, p->count, sizeof(p->data[0]), compare);
printf("排序成功\n");
}
//销毁通讯录
void Destroy(Contact* p)
{
assert(p);
p->count = 0;
printf("通讯录已销毁\n");
}主函数:
int main()
{
int input = 0;
Contact PC;
Init(&PC);
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
Add(&PC);
break;
case 2:
Delete(&PC);
break;
case 3:
Search(&PC);
break;
case 4:
Modify(&PC);
break;
case 5:
Show(&PC);
break;
case 6:
Sort(&PC);
break;
case 7:
Destroy(&PC);
break;
case 0:
printf("程序已退出\n");
break;
default:
printf("输入错误\n请重新输入\n");
break;
}
} while (input);
return 0;
}边栏推荐
- Today's sleep quality record is 84 points
- [QNX hypervisor 2.2 user manual]9.4 dryrun
- Software testing -- common testing tools
- Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
- How to create an effective help document?
- pd.melt() vs reshape2::melt()
- C语言 整数与字符串的相互转换
- 泛域名配置方法
- Practice of RTC performance automation tool in memory optimization scenario
- STM8S003F3 uart的使用
猜你喜欢
![Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?](/img/1f/a2d50ec6bc97d52c1e7566a42e564b.png)
Why is the index in [mysql] database implemented by b+ tree? Is hash table / red black tree /b tree feasible?

GAN的详细介绍及其应用(全面且完整)

"Jargon" | what kind of experience is it to efficiently deliver games with Devops?

Could not stop Cortex-M device! Please check the JTAG cable solution

Pan domain name configuration method

Stm8s003f3 internal flash debugging

泛域名配置方法

SQL那些事

Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree

想要做好软件测试,可以先了解AST、SCA和渗透测试
随机推荐
C语言 整数与字符串的相互转换
Save the image with gaussdb (for redis), and the recommended business can easily reduce the cost by 60%
使用sqldeveloper连接mysql
Nc78 reverse linked list
Detailed introduction and application of GaN (comprehensive and complete)
[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
testng执行顺序的3中控制方法
One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
程序的编译
STM32F105RBT6 内部flash调试
11.2-HJ86 求最大连续bit数
NC78 反转链表
这是一张机器&深度学习代码速查表
Nc68 jumping steps
Uniapp scroll bar topping effect, customized page scroll bar position (sorting)
11.1-CM24 最近公共祖先
国际权威认可!OceanBase入选Forrester Translytical数据平台报告
Bl602 development environment setup
如何将exe文件添加到开机启动
Construction of Huffman tree