address book
- 1 Introduction
- 2. Requirements
- 3. Realize
-
- 3.1 Preparation
- 3.2 Menu
- 3.3 Selection
- 3.4 Contact information
- 3.5 Initialize address book
- 3.6 Add contacts
- 3.7 Display all contact information
- 3.8 Find the specified contact person
- 3.8 Delete specified contact information
- 3.9 Modify the specified contact information
- 3.9 Sorting contact information
- 3.10 Release the space for the application
- 4. Optimization
- 5. Summary
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
- Implement an address book to store contact information, including the contact's name, age, gender, address, phone number
- Add contact information
- Delete the information of the specified contact
- Find information about a specific contact
- Modify the information of the specified contact
- Show information for all contacts
- Sort contacts (by age/name)
- Dynamic growth space
- Save the information to a file when exiting the address book
- 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
3.2 Menu
- Offer menu, offer choices
- The do while loop executes once regardless of the judgment condition, and can provide a choice (whether to continue or exit)
- input is used as the judgment condition. When it is 0, the loop stops, which just corresponds to exiting the address book
注意
There is a mistake here, the menu function must be before the selection.
3.3 Selection
- According to different choices, different functions are provided
- Using enumeration, the menu function corresponding to different selections
注意
must be selected before selection, which is wrong here.
3.4 Contact information
- 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
- 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.
3.5 Initialize address book
疑惑
(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.
(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)
疑惑
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
增加联系人
(1) Determine whether the maximum capacity is reached
(2) Adding the contact
con->num can just be used as an "array subscript"
3.7 Display all contact information
Implement this function first, and see the effect after adding contacts
显示联系人
疑惑
In printing, the negative sign means to align to the left, and the number limits the printing range.
3.8 Find the specified contact person
First find and then delete, so implement this function first
查找联系人
3.8 Delete specified contact information
删除联系人
3.9 Modify the specified contact information
修改联系人
3.9 Sorting contact information
排序联系人信息
qsort is a quick sort function, I mentioned qsort earlier .
3.10 Release the space for the application
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
4.2 Load the information in the file into the address book when the address book is opened
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!