Address Book (c)
First define the header file
# Ifndef _ CONTACT_H __# define NAME_LEN 10 # define SEX_LEN 6 # define TELE_LEN 12 # define ADDR_LEN 20 # define limit 1000 typedef struct PEO {char name [NAME_LEN ]; int age; char sex [SEX_LEN]; char tele [TELE_LEN]; char addr [ADDR_LEN];} Peo, * PPEO; typedef struct Contact {Peo nb [MAX_PEO]; int count; // Number of valid records} PCON, * Pcon; void add_peo (Pcon pcon); void del_peo (Pcon pcon); void search (Pcon pcon); void modify (Pcon pcon ); void show (Pcon pcon); void clear (Pcon pcon); # endif
Create contact. c and write the Function
# Include
# Include
# Include "contact. h "void add_peo (Pcon pcon) {if (pcon-> count <= MAX_PEO) {printf (" Enter name: "); scanf (" % s ", pcon-> nb [pcon-> count]. name); printf ("Enter age:"); scanf ("% d", & pcon-> nb [pcon-> count]. age); printf ("Enter Gender:"); scanf ("% s", pcon-> nb [pcon-> count]. sex); printf ("Enter the phone number:"); scanf ("% s", pcon-> nb [pcon-> count]. tele); printf ("Enter the address:"); scanf ("% s", pcon-> nb [pcon-> count]. addr); pcon-> count ++; printf ("added successfully! \ N ");} elseprintf (" the phone book is full! ");} Void show (Pcon pcon) {int I = 0; printf (" name, age, and Gender phone address \ n "); for (I = 0; I
Count; I ++) {printf ("% s % 8d % 8 s % 15 s % 18s \ n", pcon-> nb [I]. name, pcon-> nb [I]. age, pcon-> nb [I]. sex, pcon-> nb [I]. tele, pcon-> nb [I]. addr) ;}} int find (char * c, Pcon pcon) {int I = 0; for (; I
Count; I ++) {if (strcmp (c, pcon-> nb [I]. name) = 0) return I;} return-1;} void del_peo (Pcon pcon) {char nam [NAME_LEN]; int index = 0; printf ("Enter the person you want to delete:"); scanf ("% s", nam); index = find (nam, pcon-> nb [index]. name); for (; index <= pcon-> count-1; index ++) {pcon-> nb [index] = pcon-> nb [index + 1];} pcon-> count --; printf ("deleted successfully! \ N ") ;}void search (Pcon pcon) {char nam [NAME_LEN]; int I = 0; printf (" Enter the person you want to search :"); scanf ("% s", nam); I = find (nam, pcon-> nb [I]. name); printf ("name, age, gender, telephone address \ n"); printf ("% s % 8d % 8 s % 15 s % 20s \ n ", pcon-> nb [I]. name, pcon-> nb [I]. age, pcon-> nb [I]. sex, pcon-> nb [I]. tele, pcon-> nb [I]. addr);} void modify (Pcon pcon) {char nam [NAME_LEN]; int I = 0; printf ("Enter the person you want to modify :"); scanf ("% s", nam); I = find (nam, pcon-> nb [I]. name); pcon-> nb [I] = pcon-> Nb [-1]; printf ("Enter name:"); scanf ("% s", pcon-> nb [I]. name); printf ("Enter age:"); scanf ("% d", & pcon-> nb [I]. age); printf ("Enter Gender:"); scanf ("% s", pcon-> nb [I]. sex); printf ("Enter the phone number:"); scanf ("% s", pcon-> nb [I]. tele); printf ("Enter the address:"); scanf ("% s", pcon-> nb [I]. addr); printf ("added successfully! \ N ");} void clear (Pcon pcon) {pcon-> count = 0 ;}
Finally, write the function body
# Include
# Include "contact. h "# define _ CRT_SECURE_NO_WARINGS_void print_menu () {printf ("**************************** \ n "); printf ("*** 1> add 2> del *** \ n"); printf ("*** 3> show 4> search *** \ n "); printf ("*** 5> modify 6> clear *** \ n "); printf ("**************************** \ n ");} int main () {PCON con; int input = 1; con. count = 0; while (input) {print_menu (); printf ("select:"); scanf ("% d", & input ); // system ("cls"); switch (input) {case 1: add_peo (& con); break; case 2: del_peo (& con); break; case 3: show (& con); break; case 4: search (& con); break; case 5: modify (& con); break; case 6: clear (& con ); break; default: break;} return 0 ;}
Run example for address book