当前位置:网站首页>c language: address book
c language: address book
2022-08-07 04:33:00 【The fish king is fat】
前言
大家好~A very interesting thing to do today–通讯录.
通讯录需求分析
In order to realize the address book management system,为此,To ensure that the following functions are implemented:
能够存放1000个联系人的信息、Each person's information contains:名字、年龄、性别、电话、地址、除此之外,还是实现:增加人的信息、删除人的信息、修改指定人的信息、查找指定人的信息、Empty the contact information、显示联系人的信息、排序通讯录的信息.
功能介绍
1.增加联系人信息
2.删除联系人信息
3.查找联系人信息
4.修改联系人信息
5.显示联系人信息
6.排序联系人信息
7.清空联系人信息
实现思路
静态版本的通讯录首先声明一个结构体类型(struct PeoInfo)用来描述一个人的联系人的各种信息;
然后再声明一个结构体类型(struct Contact)用来描述通讯录,成员使用数组开辟1000个联系人的内存空间实现通讯录,数组的类型为struct PeoInfo
最后就是对存放联系人信息数组的一系列访问操作,封装各个函数实现各部分功能!
代码实现
️test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
printf("*******************************\n");
printf("***** 1.add 2.del *****\n");
printf("***** 3.search 4.modify *****\n");
printf("***** 5.sort 6.print *****\n");
printf("***** 7.clear 0.exit *****\n");
printf("*******************************\n");
}
enum Option
{
EXIT,
ADD,
DEL,
SEARCH,
MODIFY,
SORT,
PRINT,
CLEAR
};
int main()
{
int input = 0;
//创建通讯录,The structure type is defined in the header file,所以要引头文件,MAXDefinitions can be defined in the header file or in the source file,因为包含了头文件
//But if the array and the number of array elementsszdefined as a structure,Go to the header file to define,need thereMAX,如果MAX在test.c中定义的话,contact.h中就会报错
//既然这样,就直接把MAX定义在contact.h中去,即可.
//PeoInfo data[MAX] = { 0 };//不完全初始化
//How many elements are currently in the address book:
//int sz = 0;
//创建通讯录
Contact con;
//con就是通讯录,can also be initialized directly,But in order to better reflect the modularity,Just encapsulate a function to initialize the address book.
//If you want to initialize part of it as0,You have to use a function to do it.
//初始化通讯录
InitContact(&con);
//When initializing the address book, it is necessary to modify the content in the address book,如果传值调用的话,Does not modify the content of the actual parameter,而且效率低,So call by address.
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case ADD:
//增加联系人信息
AddContact(&con);
//Passing both the array and the number of elements in the array,会比较麻烦,Define both as a new structure
break;
case DEL:
//删除联系人信息
DelContact(&con);
break;
case SEARCH:
//查找联系人信息
SearchContact(&con);
break;
case MODIFY:
//修改联系人信息
ModifyContact(&con);
break;
case SORT:
//排序联系人信息
SortContact(&con);
break;
case PRINT:
//Although only print information,Does not change the argument information,But considering the efficiency,Better to use call by reference,It is best to pass the address of the structure parameter.
PrintContact(&con);
break;
case CLEAR:
//清空所有联系人信息
ClearContact(&con);
break;
case EXIT:
printf("退出通讯录\n");
break;
default:
printf("选择错误,请重新选择:>");
break;
}
} while (input);
//如果有同名的,Always operate on the first member with the name,Because the traversal is from front to back,The case of the same name is not considered here.
return 0;
}
️contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
//初始化通讯录
void InitContact(Contact* pc)
{
assert(pc);
//内存设置函数 — memset() - 内存设置
(pc->sz) = 0;
memset(pc->data, 0, sizeof(pc->data));
//pc->data is equivalent to finding the entire array,The entire array can be represented using the array name,所以可以使用datato represent the entire array
//即:pc->data === data ,,,The array name is missing in the first parameter&和sizeof,代表的是数组首元素的地址,第二个参数是把
//Each byte is set to0,十进制的0,Converted to hexadecimal representation is0x 00,,pc->data === data,数组名单独放在
//sizeof中,代表的是整个数组,计算的是整个数组的大小,Units are bytes and can also be written as:MAX * sizeof(Contact)
}
//增加联系人
void AddContact(Contact* pc)
{
assert(pc);
if (pc->sz == MAX)
{
printf("通讯录已满、无法添加新的联系人\n");
return; //返回类型是void,也可以写return,But can't take out the return value.
}
//The address book under,Can add new members,增加一个人的信息
printf("请输入名字:>");
scanf("%s", pc->data[pc->sz].name);
printf("请输入年龄:>");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入性别:>");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入电话:>");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:>");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("增加联系人成功\n");
//在这里,[ ]的优先级等于->,,但是,data和[ ]cannot be combined first.,因为,This is in a calling function,The part of the formal parameter accepts only pointer variablespc
//也就是说,If the latter two are combined,The system doesn't knowdata是什么东西,So it's wrong to combine,即,即使[ ]的优先级等于->,But the latter two cannot
//Combining will go wrong,And because of associativity from left to right, so,or letdata和->进行结合,即先进行pc->data的操作,So the entire array is found in the structure member variabledata
//The entire array can be represented using the array name,Once you know the name of the array, you can access the elements of the array by subscripting.
}
//显示联系人信息
void PrintContact(const Contact* pc)
{
assert(pc);
//print out all information,即sz个人的信息.
int i = 0;
//打印标题
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-50s\n", "名字", "年龄", "性别", "电话", "地址");// \t === tab
//打印数据
for (i = 0; i < (pc->sz); i++)
{
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-50s\n", pc->data[i].name,
pc->data[i].age,
pc->data[i].sex,
pc->data[i].tele,
pc->data[i].addr);
}
//通过pc->data[i]找到数组data中下标为i的元素,i.e. found a variable,Then tap to access the person's name etc.,,Since names are an array,find the entire array,整个
//Arrays can also be represented using the array name,即:pc->data[i].name === name ,没有sizeof和&,代表数组首元素的地址,再通过%sprint string,except age is a variable,Everything else is an array,和name同理.
}
//because the function is just to satisfy the delete,查找,The need to modify the function,The functions corresponding to these three functions will be implemented in this .c implementation in the file,所以,对于这个函数
//只需要在该 .c Execute in the file,Do not expose to others,,所以,在前面加上static,it fixes the function only in the current .c Work within the file.
//static 修饰函数,Essentially changing the link property of the function.
static int Find_By_Name(const Contact* pc, char name[])//数组形式接收,If it is accepted in the form of an array, it will not be consideredconst的使用了.
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)//相等
//The first parameter finds the entire array first,可以使用数组名来表示,不是特例,That is, the address of the first element of the array,The second parameter is also not a special case,也是数组首元素的地址.
{
return i;
}
}
return -1;
}
//删除联系人信息
void DelContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = {
0 };
if (pc->sz == 0)
{
printf("通讯录为空、Delete operation is no longer possible\n");
return;
}
//delete someone's information
printf("请输入要删除人员的姓名:>");
scanf("%s", name);
//1、查找要删除的人
//Whether to delete or find or modify,You need to use this function to find,So take out this function separately and encapsulate a function;
int pos = Find_By_Name(pc, name);//First-level pointer parameter and array name parameter
//the person does not exist
if (pos == -1)
{
printf("Want to delete the staff does not exist\n");
return;
}
//2、the person exists,要进行删除,After deleting the person at the position of the array,The people behind the array move forward one position in turn.
int i = 0;
for (i = pos; i < (pc->sz - 1); i++)
{
pc->data[i] = pc->data[i + 1];
}
//pc->sz -= 1;
pc->sz--;
//If you want to delete the last,是删除不掉的,因为,如果10个元素,最后一个下标为9,判断条件是<9,,所以不进入循环,但是
//After the loop there ispc->sz--,,less members1,When the personnel information is displayed again,Can't access the last person,,even if not deleted,can't access,
//The final result is the same as deleting the last person.
//假设MAX=3,把最后一个元素删除,is not essentially removed from the array,而是因为sz减1,Do not access the last element when printing,It looks the same as delete,现在由于
//looks deleted,Essentially not deleted,What if new elements are added??
//因为删除完之后,The number of elements will be subtracted1,由原来的3变成了2,,when adding new elements,will directly put the content of the new element in the subscript as2的位置上,这样的话,even before the last element
//did not delete,will also be overwritten by new elements,After adding, the number of elements is added1,Then print out the information after adding members,是对的.
printf("Delete contacts successfully\n");
}
//查找联系人信息
void SearchContact(const Contact* pc)
{
assert(pc);
char name[MAX_NAME] = {
0 };
//Find someone's information
printf("请输入要查找人员的姓名:>");
scanf("%s", name);
int pos = Find_By_Name(pc, name);
//The person you are looking for does not exist
if (pos == -1)
{
printf("The person you are looking for does not exist\n");
return;
}
else
{
//2、the person exists,After finding out and printing out the member's information
//打印标题
printf("%-10s\t%-5s\t%-5s\t%-12s\t%-50s\n", "名字", "年龄", "性别", "电话", "地址");// \t === tab
//打印数据
printf("%-10s\t%-5d\t%-5s\t%-12s\t%-50s\n",
pc->data[pos].name,
pc->data[pos].age,
pc->data[pos].sex,
pc->data[pos].tele,
pc->data[pos].addr);
}
}
//修改指定联系人的信息
void ModifyContact(Contact* pc)
{
assert(pc);
char name[MAX_NAME] = {
0 };
//Edit someone's information
printf("请输入要修改人员的姓名:>");
scanf("%s", name);
int pos = Find_By_Name(pc, name);
//The person to be modified does not exist
if (pos == -1)
{
printf("The person to be modified does not exist\n");
return;
}
else
{
printf("Please enter the modified person's name:>");
scanf("%s", pc->data[pos].name);
printf("Please enter the age of the modified person:>");
scanf("%d", &(pc->data[pos].age));
printf("Please enter the gender of the modified person:>");
scanf("%s", pc->data[pos].sex);
printf("Please enter the modified person's address:>");
scanf("%s", pc->data[pos].addr);
printf("Please enter the phone number of the modified person:>");
scanf("%s", pc->data[pos].tele);
printf("Modify contact personnel information successfully\n");
}
}
int cmp_Per_by_name(const void* e1, const void* e2)
{
return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name);
}
void SortContact(Contact* pc)
{
assert(pc);
qsort(pc->data, pc->sz, sizeof(PeoInfo), cmp_Per_by_name);
PrintContact(pc);
}
void ClearContact(Contact* pc)
{
assert(pc);
InitContact(pc);
printf("清空成功\n");
}
️contact.h
#pragma once
//类型的定义、to put in the address book1000个人的信息,
//Each person's information should include name、年龄、性别、电话、地址等,
//So define it as a struct,And the structure is to be used in both source files,
//So it's better to define it in the header file,In this way, you only need to include the header file and you can use it frequently.
//头文件的包含
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
//定义结构体类型
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 12
#define MAX_ADDR 30
#define MAX 1000
typedef struct PeoInfo
{
//char name[20];
//Write it directly as a fixed value and it will be dead.,Inconvenient for later modification,
//所以使用#define 来定义一个常量,later direct change#define中的内容即可.
char name[MAX_NAME];
char sex[MAX_SEX];
int age;
char tele[MAX_TELE];
char addr[MAX_ADDR];
}PeoInfo;//使用typedefRename the struct type to:PeoInfo
//结构体嵌套
//通讯录
typedef struct Contact//重命名
{
PeoInfo data[MAX];//Store the information of the added contacts
int sz;//记录当前通讯录中有效信息的个数
}Contact;
//初始化通讯录
void InitContact(Contact* pc);
//增加联系人
void AddContact(Contact* pc);
//打印信息
void PrintContact(const Contact* pc);
//删除联系人信息
void DelContact(Contact* pc);
//查找联系人信息
void SearchContact(const Contact* pc);
//修改指定联系人的信息
void ModifyContact(Contact* pc);
//排序联系人的信息
void SortContact(Contact* pc);
//Clear all contacts
void ClearContact(Contact* pc);
效果图:

边栏推荐
- Flutter环境配置遇到的问题
- Find My资讯|AirTag 正在帮更多人找到丢失的行李,Find My用处越来越大
- openssl 1.1.1k & libsrtp2-dev 2.3.0-2 installed
- tiup cluster help
- tiup cluster disable
- The second virtual camera: configure the v4l2loopback virtual camera as the front or rear camera
- Product system module of advertising e-commerce system development function
- 解析wpf控件内部的结构
- 多线程之原子数组
- 线性代数学习笔记6-4:行列式的应用(用伴随矩阵求逆矩阵、克莱姆法则解方程、行列式求面积/体积)
猜你喜欢

How to adjust the game settings of Ark Survival Evolved

typescript88-任务案例

What is SaaS service platform software?

量化风控的规则开发,如何更好做策略定规则,抓坏人

CefSharp方法汇总

【LeetCode每日一题】——34.在排序数组中查找元素的第一个和最后一个位置

Rainwater automatic monitoring telemetry terminal

From the rejection of my resume to the harvest of 8 big company offers, it took me 3 months to successfully break through the cocoon and become a butterfly

Empty suite

Find My资讯|AirTag 正在帮更多人找到丢失的行李,Find My用处越来越大
随机推荐
智慧路灯远程智能控制
golang服务器代理
线性代数学习笔记5-3:标准正交基、正交矩阵、施密特正交化、QR分解
从简历被拒到收割8个大厂offer,我用了3个月成功破茧成蝶
项目管理知识点
Backtracking and its Simple Question Example
Problems encountered with Flutter environment configuration
[LeetCode Daily Question] - 34. Find the first and last position of an element in a sorted array
tiup cluster edit-config
多线程之原子整数和原子引用
c#多线程同步执行
typescript83-属性的默认值
[LeetCode Daily Question] - 153. Find the minimum value in a rotated sorted array
Find My资讯|AirTag 正在帮更多人找到丢失的行李,Find My用处越来越大
openssl 1.1.1k & libsrtp2-dev 2.3.0-2安装
Reading Notes - RetinaFace: Single-stage Dense Face Localisation in the Wild
From the rejection of my resume to the harvest of 8 big company offers, it took me 3 months to successfully break through the cocoon and become a butterfly
计讯物联水环境综合管控方案,打造水清绿岸的生活环境
5 pits that machine learning beginners are easy to step on
Golang scope pit