Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement address Book with java

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "java how to achieve the address book", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve java address book" this article.

Dynamic implementation of the address book requires the use of functions such as malloc and realloc. Let's first introduce these functions.

1) void * malloc (unsigned int size)

Size is the number of bytes that need to be allocated.

(2) void * calloc (unsigned int num_elements,unsigned int elements_size)

Num_elements is the number of elements allocated, and elements_size is the number of bytes per element.

(3) void * realloc (void * pcent unsigned int new_size)

New_size is the modified number of bytes, and p is the original memory header address.

Because these functions are opened up in the heap and need to be released after use, you need the free function.

(4) void free (void * p)

P is the first address of the space that needs to be freed.

The first three functions all require free, which may cause memory leaks if you don't free. You need to set it to NULL after the free is released. We must have a principle before using the pointer, that is, determine whether it is empty before using it and set it to NULL after using it.

Next, let's realize the dynamic growth address book.

The code is as follows:

# define _ CRT_SECURE_NO_WARNINGS 1#ifndef _ CONTACT_H__#define _ CONTACT_H__#include # include enum OP {EXIT,ADD,DEL,MODIFY,SEARCH,DISPLAY,SORT,CLEAR}; # define NAME_MAX 20#define SEX_MAX 5#define TELE_MAX 12#define ADDR_MAX 25#define MAX 1000#define MAX_INIT 2#define MAX_RISE 2typedef struct PerInfo {char name [name _ MAX]; int age;char sexuality [SEX _ MAX]; char Telegraph [Tale _ MAX]; char addr [ADDR _ MAX] } PeoInfo;typedef struct Contact {PeoInfo data [MAX]; int size;int I;} Contact,*Pcon;void init_contact (Pcon pcon); void _ add_contact (Pcon pcon); void _ display_contact (Pcon pcon); void _ del_contact (Pcon pcon); void _ search_contact (Pcon pcon); void _ modify_contact (Pcon pcon); void _ sort_contact (Pcon pcon); void _ clear_contact (Pcon pcon); # endif / / _ CONTACT_H__

Function implementation part:

# define _ CRT_SECURE_NO_WARNINGS 1#include "Contact.h" / git//svn//void init_contact (Pcon pcon) {/ / memset (pcon- > data, 0, MAX*sizeof (PeoInfo)); / / pcon- > size = 0 PeoInfo pcon-> data= (PerInfo *) malloc (MAX_INIT*sizeof (PerInfo)); if (pcon- > data= = NULL) {printf ("out of menory"); exit (EXIT_FAILURE) } pcon- > size=0; pcon- > capacity=MAX_INIT;} void _ add_contact (Pcon pcon) {/ * if (pcon- > size > = MAX) {printf ("phone book full\ n"); return;} * / if (pcon- > size > = pcon- > I) {PerInfo * tmp= (PerInfo *) realloc (pcon- > data, (pcon- > i+MAX_RISE) * sizeof (PerInfo)) / / use realloc to increase if (tmp = = NULL) {printf ("out of menory\ n"); exit (EXIT_FAILURE);} else {pcon- > data=tmp; pcon- > capacity+=MAX_RISE when the actual number of contacts is equal to the initialized capacity } printf ("Please enter name: >"); scanf ("% s", pcon- > data [Pcon-> size] .name); printf ("Please enter age: >"); scanf ("% d", & (pcon- > data [Pcon-> size] .age)); printf ("Please enter gender: >"); scanf ("% s", pcon- > data [Pcon-> size] .sex); printf ("Please enter phone number: >") Scanf ("% s", pcon- > data [Pcon-> size] .tele); printf ("Please enter address: >"); scanf ("% s", pcon- > data [Pcon-> size] .addr); pcon- > size++;printf ("added successfully\ n");} void _ display_contact (Pcon pcon) {int I = 0Tran printf ("% 9s\ t% 3s\ t% 4s\ ts\ ts\ n", "name", "age", "sex", "tele", "addr"); for (I = 0) Isize;i++) {printf ("% 9s\ t% 3D\ t% 4s\ ts\ ts\ n", pcon- > data [I] .name, pcon- > data [I] .age, pcon- > data [I] .sex, pcon- > data [I] .tele, pcon- > data [I] .addr)}} static int find_entry (Pcon pcon, char * name) {if (strcmp (pcon- > data [I] .name, name) = 0) {return i.}} return-1 } void _ del_contact (Pcon pcon) {int pos = 0tint index = 0Tracchar name [name _ MAX]; if (pcon- > size = = 0) {printf ("phone is empty\ n"); return;} printf ("Please enter the person's name to delete: >"); scanf ("% s", name); pos = find_entry (pcon, name); if (pos =-1) {printf ("cannot find the person to delete\ n"); return;} / for (index = pos; index)

< pcon->

Size; index++) {pcon- > data [index] = pcon- > data [index+1];} pcon- > size--;printf ("deleted successfully\ n");} void _ search_contact (Pcon pcon) {char name [name _ MAX] = {0}; int pos = 0Transprintf ("Please enter the name of the person you are looking for: >"); scanf ("% s", name); pos = find_entry (pcon, name); if (pos =-1) {printf ("specified contact does not exist\ n"); return } else {printf ("% 9s\ t% 3s\ t% 4s\ ts\ ts\ n", "name", "age", "sex", "tele", "addr"); printf ("% 9s\ t% 3D\ t% 4s\ ts\ ts\ n", pcon- > datapos] .name, pcon- > datapos .age, pcon- > datapos .sex, pcon- > data [pos] .tele, pcon- > data[ pos] .addr) }} void _ modify_contact (Pcon pcon) {char name [name _ MAX] = {0}; int pos = 0politics printf ("Please enter the name of the person to be modified: >"); scanf ("% s", name); pos = find_entry (pcon, name); if (pos = =-1) {printf ("specified contact does not exist\ n"); return;} else {printf ("Please enter name: >"); scanf ("% s", pcon- > data [pos] .name) Printf ("Please enter age: >"); scanf ("% d", & (pcon- > data [pos] .age)); printf ("Please enter gender: >"); scanf ("% s", pcon- > datapos [sex] .sex); printf ("Please enter phone number: >"); scanf ("% s", pcon- > datapos] .tele); printf ("Please enter address: >"); scanf ("% s", pcon- > datapos] .addr) }} / / volatilevoid _ sort_contact (Pcon pcon) {PeoInfo tmp I = 0 pcon- j = 0 pcon- (I = 0; pcon- [j]) / / controls the number of sorted trips {for (j = 0; jsize-1-i; jacks +) {if (strcmp (pcon- > data [j] .name, pcon- > data [j + 1] .name) > 0) {PeoInfo tmp = {0}; tmp = pcon- > data [j]; pcon- > data [j] = pcon- > data [jacks 1]; pcon- > data [pcon-] = tmp } void _ clear_contact (Pcon pcon) {pcon- > size = 0;} above is all the content of the article "how to implement java address Book". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report