当前位置:网站首页>Address book (file version)
Address book (file version)
2022-07-22 14:08:00 【A collection of old dreams 186】
author : A collection of old dreams 186
special column :C Language programming ---- Little bit's Growth Diary
Related links : File operation (C Language )
Daily inspirational :
Life will never fail anyone , Make yourself busy when you feel sad , The world has not become smaller , We must make ourselves more and more lovely to live , Don't waste your life , But enjoy life .
Catalog
Preface :
We have finished the previous two chapters on file operation , Today, in order to consolidate the knowledge of file operation , We specially practiced the address book of document class .
The design requirements :
The address book we designed must meet the following requirements :
1. Use static memory to open up the address book , The initial default size of the address book is 1000 Contact type .
Use dynamic memory to open up the address book , The initial default size of the address book is 3 Contact types .
2. Contact information includes : full name 、 Gender 、 Age 、 Telephone 、 address .
3. The address book has the function of adding contacts .
4. The address book has the function of deleting contacts .
5. The address book has the function of finding contacts by name and outputting all the information of this contact .
6. The address book has the function of modifying contact information .
7. The address book has the function of sorting by age .
8. The address book has all contact information cleared 、 Functions of memory .
9. The address book has the function of displaying the address book .
preparation :
Static address book and dynamic address book , The layout required is basically the same
use , In a modular way, we need to create test.c,contact.c and contact.c file
among test.c Is the entry of the main function ,contact.c It is the realization of function ,contact.c It's a function declaration , Use of structure declarations and header files .
File address book
The reason why I write file address book , It's because whether it's a static address book or a dynamic address book , The information entered each time cannot be saved , After exiting the address book, the message disappears , Have to re-enter . The emergence of file address book can solve this problem .
The file version address book is improved on the basis of the dynamic version address book , We need to save the information before exiting the address book , And we need to be able to load files after each initialization , Let the address book have information . This is the biggest difference !
test.c
#define _CRT_SECURE_NO_WARNINGS #include "contact.h" /* * Add contact information Delete the specified contact information Find the specified contact information Modify designated contact information Show all contact information Clear all contacts Sort all contacts by name */ enum option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, CLEAR, SORTNAME }; void menu() { printf("*******************************************\n"); printf("****** 1.add 2.del ******\n"); printf("****** 3.search 4.modify****\n"); printf("****** 5.show 6.clear ****\n"); printf("****** 7.sortName 0.exit *****\n"); printf("*******************************************\n"); } int main() { int input = 0; Contact con; InitContact(&con); do { menu(); printf(" Please select :>"); scanf("%d", &input); switch (input) { case ADD: AddContact(&con); break; case DEL: DelContact(&con); break; case SEARCH: SearchContact(&con); break; case MODIFY: ModifyContact(&con); break; case SHOW: ShowContact(&con); break; case CLEAR: ClearContact(&con); break; case SORTNAME: SortContact(&con); break; case EXIT: // Save information to file SaveContact(&con); // Destroy the address book DestroyContact(&con); printf(" Exit address book \n"); break; default: printf(" Wrong choice \n"); break; } } while (input); return 0; }
contact.h
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <assert.h> #include <stdlib.h> #define DEFAULT_SZ 3 #define INC_SZ 2 #define MAX 1000 #define MAX_NAME 20 #define MAX_SEX 10 #define MAX_TELE 12 #define MAX_ADDR 30 typedef struct PeoInfo { char name[MAX_NAME]; int age; char sex[MAX_SEX]; char tele[MAX_TELE]; char addr[MAX_ADDR]; }PeoInfo; //typedef struct Contact //{ // PeoInfo data[MAX]; // int count; //}Contact; // Dynamic version typedef struct Contact { PeoInfo* data; int count; int capacity; }Contact; // initialization int InitContact(Contact* pc); // add to void AddContact(Contact* pc); // Print void ShowContact(const Contact* pc); // Delete void DelContact(Contact* pc); // lookup void SearchContact(Contact* pc); // modify void ModifyContact(Contact* pc); // Empty void ClearContact(Contact* pc); // Sort void SortContact(Contact* pc); // The destruction void DestroyContact(Contact* pc); // Save address book information to a file void SaveContact(Contact* pc); // Load the contents of the file into the address book void LoadContact(Contact* pc); // void CheckCapacity(Contact* pc);
contact.c
#define _CRT_SECURE_NO_WARNINGS #include "contact.h" void LoadContact(Contact* pc) { FILE* pf = fopen("contact.dat", "r"); if (pf == NULL) { perror("LoadContact"); return; } // Reading documents PeoInfo tmp = { 0 }; while (fread(&tmp, sizeof(PeoInfo), 1, pf)) { // Whether to increase capacity CheckCapacity(pc); pc->data[pc->count] = tmp; pc->count++; } // Close file fclose(pf); pf = NULL; } int InitContact(Contact* pc) { assert(pc); pc->count = 0; pc->data = (PeoInfo*)calloc(3, sizeof(PeoInfo)); if (pc->data == NULL) { printf("InitContact:%s\n", strerror(errno)); return 1; } pc->capacity = DEFAULT_SZ; // Load the file LoadContact(pc); return 0; } void CheckCapacity(Contact* pc) { if (pc->count == pc->capacity) { PeoInfo* ptr = realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo)); if (ptr == NULL) { printf("ADDContact:%s\n", strerror(errno)); return; } else { pc->data = ptr; pc->capacity += INC_SZ; printf(" Successful expansion \n"); } } } // Dynamic version void AddContact(Contact* pc) { assert(pc); CheckCapacity(pc); printf(" Please enter a name :"); scanf("%s", pc->data[pc->count].name); printf(" Please enter age :"); scanf("%d", &pc->data[pc->count].age); printf(" Please enter gender :"); scanf("%s", pc->data[pc->count].sex); printf(" Please input the phone number :"); scanf("%s", pc->data[pc->count].tele); printf(" Please enter the address :"); scanf("%s", pc->data[pc->count].addr); pc->count++; printf(" Increase success \n"); } void ShowContact(const Contact* pc) { assert(pc); int i = 0; printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " name ", " Age ", " Gender ", " Telephone ", " Address "); for (i = 0; i < pc->count; i++) { printf("%-20s\t%-3d\t%-5s\t%-12s\t%-30s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } } static int FindByName(Contact* pc, char name[]) { assert(pc); int i = 0; for (i = 0; i < pc->count; i++) { if (0 == strcmp(pc->data[i].name, name)) { return i; } } return -1; } void DelContact(Contact* pc) { char name[MAX_NAME] = { 0 }; assert(pc); int i = 0; if (pc->count == 0) { printf(" Address book is empty , No information can be deleted \n"); return; } printf(" Please enter the name of the person you want to delete :>"); scanf("%s", name); // lookup int pos = FindByName(pc, name); if (pos == -1) { printf(" The person to delete does not exist \n"); return; } // Delete for (i = pos; i < pc->count - 1; i++) { pc->data[i] = pc->data[i + 1]; } pc->count--; printf(" Delete successful \n"); } void SearchContact(Contact* pc) { char name[MAX_NAME] = { 0 }; assert(pc); printf(" Please enter the name of the person you want to search for :>"); scanf("%s", name); int pos = FindByName(pc, name); if (pos == -1) { printf(" The person you're looking for doesn't exist \n"); return; } else { printf("%-20s\t%-5s\t%-5s\t%-12s\t%-30s\n", " name ", " Age ", " Gender ", " Telephone ", " Address "); printf("%-20s\t%-3d\t%-5s\t%-12s\t%-30s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } } void ModifyContact(Contact* pc) { char name[MAX_NAME] = { 0 }; assert(pc); printf(" Please enter the name of the person you want to modify :>"); scanf("%s", name); int pos = FindByName(pc, name); if (pos == -1) { printf(" The person to modify does not exist \n"); return; } else { printf(" Please enter a name :"); scanf("%s", pc->data[pos].name); printf(" Please enter age :"); scanf("%d", &pc->data[pos].age); printf(" Please enter gender :"); scanf("%s", pc->data[pos].sex); printf(" Please input the phone number :"); scanf("%s", pc->data[pos].tele); printf(" Please enter the address :"); scanf("%s", pc->data[pos].addr); printf(" Modification successful \n"); } } void ClearContact(Contact* pc) { assert(pc); pc->count = 0; memset(pc->data, 0, sizeof(pc->data)); printf(" Empty successfully !\n"); } int cmp_by_name(const void* e1, const void* e2) { return strcmp(((PeoInfo*)e1)->name, ((PeoInfo*)e2)->name); } void SortContact(Contact* pc) { assert(pc); for (int i = 0; i < pc->count - 1; i++) { qsort(pc->data, pc->count, sizeof(pc->data[0]), cmp_by_name); } printf(" Sort success !\n"); } void DestroyContact(Contact* pc) { assert(pc); free(pc->data); pc->data = NULL; } void SaveContact(Contact* pc) { FILE*pf = fopen("Contact.dat", "w"); if (pf == NULL) { perror("SaveContact"); return; } // Writing documents int i = 0; for (i = 0; i < pc->count; i++) { fwrite(pc->data + i, sizeof(PeoInfo), 1, pf); } // Close file fclose(pf); pf = NULL; }
Conclusion :
Everyone's growth is ability and what he wants , Continuous matching process , When your talent doesn't match your desire , You should calm down and study , If Xiaobian's summary can help you , I hope you guys will pay more attention for three consecutive days , Your support is the biggest motivation for Xiaobian's creation .
边栏推荐
- 9. ZABBIX SNMP monitoring
- 【C】 General template of information management system / address book (introduce static, dynamic and file versions)
- Android interview question: tell me the difference between pendingintent and intent
- PADS画板框
- Shengxin often draws analytical graphics -- various types of heat maps! Have you learned?
- Notes and Thoughts on the red dust of the curtain of Heaven (II) you know what others know and what you know
- 2.zabbix概念
- 美团二面: Redis 5 种基础数据结构?
- Explore the implementation of hash table through redis source code
- 基于SSM+JSP+MYSQL+H-UI 实现的火锅店点餐系统
猜你喜欢
Network counting -- network layer
【C】 General template of information management system / address book (introduce static, dynamic and file versions)
In the software testing interview, the interviewer asks you some "difficult" questions. How will you answer them
Do you have to follow flush privileges after MySQL grant?
MMD pmx 导入unity 相关资料
最终一致性性分布式事务 TCC
【带你上手云原生体系】第四部分:Kubernetes从入门到精通
MySQL高级篇(C)
来get一个超实用的小页面——todolist
What should we pay attention to in writing automated test resumes? What are the skills?
随机推荐
The undeclared identifier "MainWindow" is used in the QT code of Kirin system“
JUC-线程池
Win10 registry of the nth reinstallation of the system
Use of split function in MATLAB
7.zabbix自动发现.自动注册
JUC no lock
异常检测 and 自编码器(2)
Do you have to follow flush privileges after MySQL grant?
22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
Pytoch (III) -- fashionmnist fashion classification
JUC concurrent contracting
来get一个超实用的小页面——todolist
Take you to brush (niuke.com) C language hundred questions (day 3)
NVIDIA programmable inference accelerator tensorrt learning notes (I) -- start
ABAP grammar foundation 2
寻找斐波那契数
C language hierarchical understanding (C language branches and circular statements)
kube-scheduler的调度上下文
9.zabbix-SNMP监控
微服务实战|集中配置中心Config对称加密实战