当前位置:网站首页>通讯录(一)
通讯录(一)
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;
}边栏推荐
- Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
- c语言---25 扫雷游戏
- C语言 整数与字符串的相互转换
- Chapter 5 Basic Scripting: Shell Variables
- [QNX hypervisor 2.2 user manual]9.4 dryrun
- How to create an effective help document?
- [QNX Hypervisor 2.2用户手册]9.5 dump
- 如何创建一个有效的帮助文档?
- Safe operation instructions for oscilloscope probe that must be read by engineers
- Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
猜你喜欢

Interview shock: why does TCP need three handshakes?

柔性电流探头选型指南

1--- electronic physical cognition

【华为机试真题】字符串匹配

想要做好软件测试,可以先了解AST、SCA和渗透测试

ORB_ Slam3 recurrence - Part I

Stm8s003f3 internal flash debugging

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

408 Chapter 2 linear table

Pan domain name configuration method
随机推荐
MySQL 索引优化全攻略
Insufficient space on Disk C mklink solves vscode expansion migration to other disks
泛域名配置方法
MySQL optimistic lock
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
Tang's little helper
SQL things
Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
c语言---25 扫雷游戏
Connect to MySQL using sqldeveloper
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
[QNX Hypervisor 2.2用户手册]9.5 dump
Software testing -- common testing tools
OV7725 yuv 640*[email protected] 配置文件
Three control methods of TestNG execution sequence
7. Dependency injection
Error when starting MySQL on Linux
Today's sleep quality record is 84 points
This is a quick look-up table of machine & deep learning code
Express of nodejs simple example program