当前位置:网站首页>Implementation of address book for dynamic memory management
Implementation of address book for dynamic memory management
2022-07-19 03:40: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;
};
// Create address book
struct Contaction* creat_contaction();
// Destroy the address book
void distory_contaction(struct Contaction* con);
// Add contact information
void add_peoinf(struct Contaction* con);
// Find contact information
struct PeoInf* search_peoname(struct Contaction* con);
// Delete contact information
void del_peoinf(struct Contaction* con);
// Show contact information
void show_peoinf(struct Contaction* con);
// Modify contact information
void modify_peoinf(struct Contaction* con);
// Clear address book information
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);
}
// Compatibilization function
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(" Capacity increase failed \n");
return 0;
}
else
{
printf(" Successful expansion \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(" Please enter the information to add :");
printf(" full name :");
scanf("%s", &(con->p[con->numbers].name));
printf(" Gender :");
scanf("%s", &(con->p + con->numbers)->sex);
printf(" Age :");
scanf("%d", &(con->p + con->numbers)->age);
printf(" Phone number :");
scanf("%s", &(con->p + con->numbers)->tel);
printf(" Address :");
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(" Please enter the name you want to search for :");
scanf("%s", &find_name);
for (int i = 0; i < con->numbers; i++)
{
if (strcmp(find_name, con->p[i].name) == 0)
{
printf(" eureka \n");
return con->p + i;
}
}
printf(" I didn't find it \n");
return NULL;
}
void del_peoinf(struct Contaction* con)
{
assert(con);
if (con->numbers == 0)
{
printf(" Address book is empty \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(" full name Gender Age Telephone Address \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(" Address book is empty \n");
return;
}
struct PeoInf* temp = search_peoname(con);
if (temp == NULL)
{
return;
}
else
{
printf(" Please enter modification information :");
printf(" full name :");
scanf("%s", &temp->name);
printf(" Gender :");
scanf("%s", &temp->sex);
printf(" Age :");
scanf("%d", &temp->age);
printf(" Phone number :");
scanf("%s", &temp->tel);
printf(" Address :");
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(" Input error , Please re-enter \n");
break;
}
} while (input);
return 0;
}
边栏推荐
- How to read and write a single document based on MFC
- 爬虫学习(5):手把手教你爬虫requests实战演练
- laravel的问题
- Net SNMP development I
- 运算符、赋值语句、结构说明语句
- CorelDRAW cannot be installed. Solution
- Envi: (the most detailed tutorial in 2022) custom coordinate system
- oracle 查询 主机名和对应的IP地址
- XX City high school network topology overall planning configuration
- HRNet
猜你喜欢
Neural network learning notes 2.2 -- write a simple convolution neural network image classifier with MATLAB
Transaction and storage engine in MySQL database
Visual analysis of ncnn param file and bin model
运算符、赋值语句、结构说明语句
Installing PWA application in Google Chrome browser will display more description information
Leetcode: subsequence problem in dynamic programming
leetcode 222. Number of nodes of a complete binary tree (required)
Install Net prompt "cannot establish a certificate chain to trust the root authority" (simple method with download address)
Rhce8 Learning Guide Chapter 1 installing rhel8.4
Yolov5 ncnn reasoning
随机推荐
Pure virtual function
Es6 notes d'étude - station B Xiao Ma Ge
laravel的问题
Unity解决同材质物体重叠产生Z-Fighting的问题
Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
Binary search (leetcode704. very simple and necessary)
STM32串口发送和接收多个数据教程基于气体传感器实战
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
mysqldump: [Warning] Using a password on the command line interface can be insecure.
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“ )
374. 猜数字大小(入门 必会)
Thinkphp5.0模型操作使用page进行分页
leetcode 222. Number of nodes of a complete binary tree (required)
Reptile learning (5): teach you reptile requests practice hand in hand
[MySQL] data query operation (select statement)
Win10 onedrive failure reinstallation
Bisenetv2 face segmentation ncnn reasoning
MySQL 增删查改(基础)
Unity using Sqlite
By voting for the destruction of STI by Dao, seektiger is truly community driven