In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how the C language simple address book function, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The details are as follows
1. Store contact information
two。 Information: name + age + gender + telephone + address
3. Add contacts
4. Delete contact
5. Find contact
6. Modify contact
7. Sort
The test.c source file code is as follows:
# define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" void menu () {printf ("# #\ n"); printf ("# 1. Add 2. Del #\ n"); printf ("# 3. Search 4. Modify #\ n") Printf ("# 5. Show 6. Sort #\ n"); printf ("# 0. Exit #\ n "); printf (" # #\ n ");} / / create an enumerated type corresponding to the menu options enum Option {EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT,}; int main () {int input = 0; / / create an address book struct Contact con / / initialize address book InitContact (& con); / / put up to 3 contact information / / add do {menu () when there is not enough space; / / enter the loop and print menu printf ("Please choose: >"); scanf ("% d", & input); / / user select action 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 SORT: SortContact (& con); break; case EXIT: SaveContact (& con); / / destroy address book DestroyContact (& con); printf ("exit\ n"); break; default: printf ("wrong choice\ n"); break;} while (input); return 0;}
The contact.c source file code is as follows:
# define _ CRT_SECURE_NO_WARNINGS 1#include "contact.h" / / check address book capacity void CheckCapacity (struct Contact* pc) {/ / assert assert (pc); if (pc- > sz = = pc- > capacity) / / if the valid contact equals the maximum capacity struct PeoInfo* ptr = realloc (pc- > data, (pc- > capacity + 2) * sizeof (struct PeoInfo)) If (ptr! = NULL) / / if it is not empty, it indicates that the compatibility was successfully increased {pc- > data = ptr; pc- > capacity + = 2; printf ("compatibility succeeded\ n");} else {perror ("AddContact::realloc"); return;} / load valid contact information void LoadContact (struct Contact* pc) {assert (pc); struct PeoInfo tmp = {0} / / temporary space FILE* pfRead = fopen ("contact.txt", "rb"); / / Open the file and read the data if (NULL = = pfRead) {perror ("open file for reading"); return;} / / load the data / / read one contact information at a time until the frea return value is less than 1, indicating that the reading is complete, and the loop ends with while (& tmp, sizeof (struct PeoInfo), 1, pfRead) {CheckCapacity (pc). Pc- > data [PC-> sz] = tmp; pc- > sz++;} / / close the file fclose (pfRead); pfRead = NULL;// is left empty to prevent it from becoming a wild pointer} / / initialize the address book void InitContact (struct Contact* pc) {assert (pc); pc- > sz = 0; pc- > capacity = DEFAULT; pc- > data = (struct PeoInfo*) malloc (DEFAULT * sizeof (struct PeoInfo)); / / dynamic application space if (pc- > data = NULL) {perror ("InitContact::malloc") Return;} LoadContact (pc); / / load address book information} / / add contacts void AddContact (struct Contact* pc) {assert (pc); CheckCapacity (pc); / / check whether the capacity is full. If yes, increase the capacity / / enter the specified contact information printf ("Please enter name: >"); scanf ("% s", pc- > data[ PC-> sz] .name); printf ("Please enter age: >") Scanf ("% d", & (pc- > data [PC-> sz] .age); printf ("Please enter gender: >"); scanf ("% s", pc- > data [PC-> sz] .sex); printf ("Please enter phone number: >"); scanf ("% s", pc- > Data [PC-> sz] .tele); printf ("Please enter address: >"); scanf ("% s", pc- > data [PC-> sz] .addr); printf ("add successfully\ n") Pc- > sz++;// valid contacts add 1} / destroy address book void DestroyContact (struct Contact* pc) {assert (pc); free (pc- > data); / / release the dynamic memory space of the application pc- > data = NULL;// set to null pointer pc- > capacity = 0 pc- / capacity set 0 pc- > sz = 0 lead / valid contact set 0} / / display address book information void ShowContact (const struct Contact* pc) {assert (pc); int I = 0 Printf ("% 20s\ t% 5s\ ts\ ts\ t% 50s\ n\ n", "name", "age", "sex", "tele", "addr"); for (I = 0; I
< pc->Sz; iTunes +) {/ / print each data printf ("% 20s\ t% 5d\ ts\ ts\ t% 50s\ n", pc- > data.name, pc- > data.age, pc- > data.sex, pc- > data.tele, pc- > data.addr);}} / / find the specified contact int FindContactByName (const struct Contact* pc, const char* name) {assert (pc & & name) Int I = 0; for (I = 0; I
< pc->Sz; iTunes +) {if (strcmp (pc- > data [I] .name, name) = 0) {return I;} / cannot find return-1;} / / Delete the specified contact void DelContact (struct Contact* pc) {assert (pc); if (pc- > sz = = 0) {printf ("address book is empty and cannot be deleted\ n"); return;} char name [name _ name] = {0} Printf ("Please enter the name of the contact to be deleted: >"); scanf ("% s", name); / / find the deleted person int pos = FindContactByName (pc, name); if (pos =-1) printf ("specified contact does not exist\ n"); else {/ / delete int I = 0; for (I = pos; I)
< pc->Sz-1; iTunes +) {pc- > data [I] = pc- > data [I + 1];} pc- > sz--; printf ("deleted successfully\ n");} / / find the specified contact void SearchContact (const struct Contact* pc) {assert (pc); char name [name _ MAX] = {0}; if (pc- > sz = = 0) {printf ("address book is empty, cannot be found\ n"); return } printf ("Please enter the name of the contact to be found: >"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (- 1 = = pos) printf ("find no such person\ n"); else {printf ("s\ t% 5s\ t% 8s\ ts\ t% 30s\ n", "name", "age", "sex", "tele", "addr") Printf ("s\ t% 5d\ t% 8s\ ts\ t% 30s\ n", pc- > data [pos] .name, pc- > data [pos] .age, pc- > data [pos] .sex, pc- > data [pos] .tele, pc- > data [pos] .addr);} / / modify the specified contact void ModifyContact (struct Contact* pc) {assert (pc); char name [name _ MAX] = {0} If (pc- > sz = = 0) {printf ("address book is empty and cannot be modified\ n"); return;} printf ("Please enter the contact name to be modified: >"); scanf ("% s", name); int pos = FindContactByName (pc, name); if (- 1 = = pos) printf ("the person to be modified does not exist\ n"); else {printf ("Please enter a new name: >") Scanf ("% s", pc- > data [pos] .name); printf ("Please enter a new age: >"); scanf ("% d", & (pc- > data [pos] .age)); printf ("Please enter a new gender: >"); scanf ("% s", pc- > datapos] .sex); printf ("Please enter a new phone: >"); scanf ("% s", pc- > data [pos] .tele) Printf ("Please enter a new address: >"); scanf ("% s", pc- > data [pos] .addr);}} / / sort by name-> ascending order int cmp_by_name (const void* E1, const void* e2) {return strcmp (struct PeoInfo*) E1)-> name), ((struct PeoInfo*) e2)-> name) Sort by age-> ascending int cmp_by_age (const void* E1, const void* e2) {return ((struct PeoInfo*) E1)-> age-((struct PeoInfo*) e2)-> age;} / / sort address book void SortContact (struct Contact* pc) {assert (pc); int input = 0; if (pc- > sz = = 0) {printf ("address book is empty, cannot be sorted\ n"); return } printf ("enter 1 by name, 2: >"); scanf ("% d", & input); if (1 = = input) {/ / name sort qsort (pc- > data, pc- > sz, sizeof (struct PeoInfo), cmp_by_name);} else {/ / Age sort qsort (pc- > data, pc- > sz, sizeof (struct PeoInfo), cmp_by_age) }} / / Save address book void SaveContact (struct Contact* pc) {assert (pc); int I = 0; FILE* pfWrite = fopen ("contact.txt", "wb"); write valid contacts of the current program to file if (NULL = = pfWrite) / / determine whether the pointer {perror ("open file for writting") is null; return;} / / write data for (I = 0; I
< pc->Sz; iTunes +) {fwrite (pc- > data+i, sizeof (struct PeoInfo), 1, pfWrite); / / write} fclose (pfWrite); / / close file pfWrite = NULL;// set to null pointer}
The contact.h header file code is as follows:
# pragma once#include # define DEFAULT 3#define NAME_MAX 20#define SEX_MAX 10#define TELE_MAX 12#define ADDR_MAX 50#define MAX 1000swap / contact information struct PeoInfo {char name [name _ MAX]; int age; char sexuality [SEX _ MAX]; char Telegraph [Tale _ MAX]; char addr [ADDR _ MAX]; / / address book-dynamic growth version struct Contact {struct PeoInfo* data; int sz / / current maximum capacity of int capacity;// address book of current valid elements in address book}; / / increase void CheckCapacity (struct Contact* pc); / / initialize address book void InitContact (struct Contact* pc); / / destroy address book void DestroyContact (struct Contact* pc); / / add contact void AddContact (struct Contact* pc); / / display address book void ShowContact (const struct Contact* pc); / / delete specified contact void DelContact (struct Contact* pc) / / find the specified contact void SearchContact (const struct Contact* pc); / / modify the specified contact void ModifyContact (struct Contact* pc); / / sort the address book void SortContact (struct Contact* pc); / / save the address book to the file void SaveContact (struct Contact* pc); / / load the address book void LoadContact (struct Contact* pc)
The program runs:
Rerun the program and display
The previously added contacts will be stored in the specified file, and the program will be loaded again. The successful capacity increase is due to the default capacity of 3 contacts and 4 contacts to be loaded, so the capacity will be increased automatically.
Thank you for reading this article carefully. I hope the article "how the C language is simple address book function" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.