[C language] How to implement an address book


1 Introduction

Through the advanced learning of C language, we have learned about structure, enumeration, dynamic memory allocation and file operation, and now use this knowledge to realize an address book and achieve a summary of recent learning.


2. Requirements

  1. Implement an address book to store contact information, including the contact's name, age, gender, address, phone number
  2. Add contact information
  3. Delete the information of the specified contact
  4. Find information about a specific contact
  5. Modify the information of the specified contact
  6. Show information for all contacts
  7. Sort contacts (by age/name)
  8. Dynamic growth space
  9. Save the information to a file when exiting the address book
  10. When the address book is opened, the information in the file can be loaded into the address book

Items 9 and 10 can be ignored first, and can be modified on the basis of the first 8 items.


3. Realize

3.1 Preparation

Create a module (contact.h and contact.c) and a test file (test.c), contact.h implements the declaration of the function, contact.c implements the address book, and test.c tests the address book
insert image description here

3.2 Menu

  1. Offer menu, offer choices
  2. The do while loop executes once regardless of the judgment condition, and can provide a choice (whether to continue or exit)
  3. input is used as the judgment condition. When it is 0, the loop stops, which just corresponds to exiting the address book
    insert image description here

注意
There is a mistake here, the menu function must be before the selection.

3.3 Selection

  1. According to different choices, different functions are provided
  2. Using enumeration, the menu function corresponding to different selections
    insert image description here
    注意
    must be selected before selection, which is wrong here.

3.4 Contact information

  1. The structure will be used in test.c and contact.c. For convenience, the structure is directly defined in the header file, and then the same structure can be used by referencing the header file in the two source files. Therefore, the header file of the library function can be directly included in contact.h

insert image description here
insert image description here

  1. When we add, delete, and search these operations, we need to know the number of people in the current address book, so we create a structure again, which includes the number of people in the current address book, pointers of people type and the maximum capacity of the current address book.
    疑惑
    (1) Why do we need a pointer to people? Wouldn't it be simpler not to create an array directly to store it in?
    One is that people is very large, not to mention the array composed of it. If there are 100 contacts, an array of 100 elements needs to be created. What if it is larger? Therefore, including a pointer with only 4 bytes or 8 bytes can save space;
    the second is to use an array to record the contact information. The number of elements we create is 100, but if there are only 15 contacts, wouldn’t it be a waste? Space, if there are 200 contacts, the space is not enough. So only use dynamic memory allocation to allocate space, expand when it is not enough, and shrink when it is too large. Use dynamic memory allocation to return the start address of the allocated space, so use a pointer to receive it.
    (2) Why include the maximum capacity of the current address book?
    When the number of contacts in the address book reaches the current maximum capacity, it can be expanded.
    insert image description here

3.5 Initialize address book

insert image description here

疑惑
(1) Why pass the address?
Still in order to save space, we know that the structure has memory alignment, which means that some bytes of space will be wasted, so we simply pass its pointer, and the function parameter only occupies 4/8 bytes.
(2) Why not initialize directly?
Other operations may be performed when the address book is initialized.

初始化通讯录
(1) We define the initial maximum capacity as 3, and define the increased capacity for each subsequent expansion as 2.

insert image description here

(2) Remember that the function is declared in contact.h and implemented in contact.c . Among them, dynamic memory development may fail, remember to judge whether the return value is a null pointer . (I will not remind you later)
insert image description here

疑惑
It seems that it just opened up space and didn't initialize it?
In fact, after calloc opens up space, it will be initialized to 0 at the same time. This is why I use calloc.

3.6 Add contacts

insert image description here
增加联系人
(1) Determine whether the maximum capacity is reached
insert image description here

(2) Adding the contact
con->num can just be used as an "array subscript"
insert image description here

3.7 Display all contact information

Implement this function first, and see the effect after adding contactsinsert image description here
显示联系人
insert image description here

疑惑
In printing, the negative sign means to align to the left, and the number limits the printing range.
insert image description here

3.8 Find the specified contact person

First find and then delete, so implement this function first
insert image description here
查找联系人
insert image description here
insert image description here

3.8 Delete specified contact information

insert image description here
删除联系人
insert image description here
insert image description here

3.9 Modify the specified contact information

insert image description here
修改联系人
insert image description here
insert image description here

3.9 Sorting contact information

insert image description here
排序联系人信息
insert image description here

qsort is a quick sort function, I mentioned qsort earlier .
insert image description here

3.10 Release the space for the application

insert image description here
insert image description here


4. Optimization

Every time we execute the code, we have to re-enter the data, which will disappear when we close the program. Is there any way to solve this problem?

4.1 Save the information to a file when exiting the address book

insert image description here
insert image description here

4.2 Load the information in the file into the address book when the address book is opened

insert image description here

insert image description here

insert image description here

insert image description here


5. Summary

The implementation of the address book uses structures, enumerations, dynamic memory, file writing and reading, and various functions, which help to strengthen the understanding and mastery of advanced knowledge of C language.
Compared with minesweeper and backgammon, the address book is simpler, but there are still deficiencies, and I hope to correct it! Thanks!

Guess you like

Origin blog.csdn.net/Zhuang_N/article/details/128811859