当前位置:网站首页>动态管理内存的通讯录实现
动态管理内存的通讯录实现
2022-07-17 01:37:00 【JDSZGLLL】
contact.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<errno.h>
struct PeoInf
{
char name[20];
char sex[8];
int age;
char tel[12];
char address[30];
};
struct Contaction
{
struct PeoInf* p;
int numbers;
int capacity;
};
//创建通讯录
struct Contaction* creat_contaction();
//销毁通讯录
void distory_contaction(struct Contaction* con);
//添加联系人信息
void add_peoinf(struct Contaction* con);
//查找联系人信息
struct PeoInf* search_peoname(struct Contaction* con);
//删除联系人信息
void del_peoinf(struct Contaction* con);
//显示联系人信息
void show_peoinf(struct Contaction* con);
//修改联系人信息
void modify_peoinf(struct Contaction* con);
//清空通讯录信息
void clear_peoinf(struct Contaction* con);contact.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
struct Contaction* creat_contaction()
{
struct Contaction* ret = (struct Contaction*)malloc(sizeof(struct Contaction));
if (ret != NULL)
{
memset(ret, 0, sizeof(struct Contaction));
ret->p = (struct PeoInf*)malloc(10 * sizeof(struct PeoInf));
}
else
{
printf("%s", strerror(errno));
exit(-1);
}
assert(ret->p);
if (ret->p == NULL)
{
printf("%s", strerror(errno));
exit(-1);
}
else
{
memset(ret->p, 0, 10 * sizeof(struct PeoInf));
ret->capacity = 10;
}
return ret;
}
void distory_contaction(struct Contaction* con)
{
assert(con);
free(con->p);
free(con);
}
//增容函数
int improve_capacity(struct Contaction* con)
{
assert(con);
struct PeoInf* temp = (struct PeoInf*)realloc(con->p, con->capacity * 2 * sizeof(struct PeoInf));
if (temp == NULL)
{
printf("增容失败\n");
return 0;
}
else
{
printf("增容成功\n");
con->capacity *= 2;
return 1;
}
}
void add_peoinf(struct Contaction* con)
{
assert(con);
if (con->numbers == con->capacity)
{
if (improve_capacity(con) == 0)
return;
}
printf("请输入添加信息:");
printf("姓名:");
scanf("%s", &(con->p[con->numbers].name));
printf("性别:");
scanf("%s", &(con->p + con->numbers)->sex);
printf("年龄:");
scanf("%d", &(con->p + con->numbers)->age);
printf("电话号码:");
scanf("%s", &(con->p + con->numbers)->tel);
printf("地址:");
scanf("%s", &(con->p + con->numbers)->address);
con->numbers++;
}
struct PeoInf* search_peoname(struct Contaction* con)
{
assert(con);
struct PeoInf* ret = con->p;
char find_name[20] = { 0 };
printf("请输入要查找的姓名:");
scanf("%s", &find_name);
for (int i = 0; i < con->numbers; i++)
{
if (strcmp(find_name, con->p[i].name) == 0)
{
printf("找到了\n");
return con->p + i;
}
}
printf("没找着\n");
return NULL;
}
void del_peoinf(struct Contaction* con)
{
assert(con);
if (con->numbers == 0)
{
printf("通讯录为空\n");
return;
}
struct PeoInf* temp = search_peoname(con);
if (temp == NULL)
{
return;
}
else
{
for (int i = temp - con->p; i < con->numbers - 1; i++)
{
con->p[i] = con->p[i + 1];
}
}
con->numbers--;
}
void show_peoinf(struct Contaction* con)
{
assert(con);
if (con->numbers != 0)
{
printf("姓名 性别 年龄 电话 地址\n");
for (int i = 0; i < con->numbers; i++)
{
printf("%-6s%-6s%-6d%-14s%-s\n",
con->p[i].name,
con->p[i].sex,
con->p[i].age,
con->p[i].tel,
con->p[i].address);
}
}
}
void modify_peoinf(struct Contaction* con)
{
assert(con);
if (con->numbers == 0)
{
printf("通讯录为空\n");
return;
}
struct PeoInf* temp = search_peoname(con);
if (temp == NULL)
{
return;
}
else
{
printf("请输入修改信息:");
printf("姓名:");
scanf("%s", &temp->name);
printf("性别:");
scanf("%s", &temp->sex);
printf("年龄:");
scanf("%d", &temp->age);
printf("电话号码:");
scanf("%s", &temp->tel);
printf("地址:");
scanf("%s", &temp->address);
}
}
void clear_peoinf(struct Contaction* con)
{
assert(con);
con->numbers = 0;
}main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"contact.h"
void menu()
{
printf("***** 1.add 2.del *****\n");
printf("***** 3.search 4.modify*****\n");
printf("***** 5.show 6.clear *****\n");
printf("***** 0.exit *****\n");
}
int main()
{
enum function
{
exit,
add,
del,
search,
modify,
show,
clear
};
enum function input = exit;
struct Contaction* con = creat_contaction();
do
{
menu();
scanf("%d", &input);
switch (input)
{
case exit:
distory_contaction(con);
break;
case add:
add_peoinf(con);
break;
case del:
del_peoinf(con);
break;
case search:
search_peoname(con);
break;
case modify:
modify_peoinf(con);
break;
case show:
show_peoinf(con);
break;
case clear:
clear_peoinf(con);
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}边栏推荐
- [NoSQL] redis high availability and persistence
- 模块(block、module)的介绍
- [Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th
- Using gatekeeper to restrict kubernetes to create specific types of resources
- Note: light source selection and Application
- Through openharmony compatibility evaluation, the big brother development board and rich teaching and training resources have been ready
- Bisenetv2 face segmentation ncnn reasoning
- MYSQL主从搭建
- Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]
- RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
猜你喜欢

Latest installation tutorial of VMware Tools (rhel8)

Thinkphp5.0 model operation uses page for paging

10. Redis interview FAQ

论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation

Automatic assembly & set injection

The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)

GoogLeNet

Zabbix6.0 monitoring vcenter7.0

Authentication code for wireless

XX市高中网络拓扑整体规划配置
随机推荐
The third day of the three questions of Luogu daily (make up on the fourth day)
zsh: command not found: mysql
CorelDRAW cannot be installed. Solution
Authentication code for wireless
Unicast、Multicast、Broadcast
神经网络学习笔记2.2 ——用Matlab写一个简单的卷积神将网络图像分类器
MySQL optimized index
Yolov6 learning first chapter
leetcode:50. Pow(x, n)
Method of realizing horizontal and vertical centering of unknown width and height elements
2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
ES6学习笔记——B站小马哥
oracle 查询 主机名和对应的IP地址
Shell script receives and returns parameters
MySQL addition, deletion, query and modification (basic)
Installing PWA application in Google Chrome browser will display more description information
[template record] string hash to judge palindrome string
洛谷每日三题之第三天(第四天补做)
GNOME-BOXES虚拟机创建安装
10. Redis 面试常见问答