C Language Practice-Address book, the address book mainly to achieve maximum storage of personal information, the personal information in the Address Book can be added, deleted, check, change and other basic functions, by using C language, Ability to exercise programming to a great extent. The program mainly uses the knowledge of the array to create A static array of size, in extreme cases, fewer elements in the address book, less space usage, resulting in wasted space. The program can be improved again by dynamically opening up arrays, reducing the waste of space, can also be achieved through the linked list.
--The following program is mainly using static arrays, the program needs to be improved.
#define _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> #include <string.h># Include <stdlib.h> #define MAX 1000#define MAX_NAME 10#define MAX_SEX 3#define max_telephone 20#define max_adr 20typedef struct people //people Information { char name[max_name]; char sex[MAX_SEX]; int age; CHAR&NBSP;TELE[MAX_TELEPHONE];&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;ADR[MAX_ADR];} Storage for people;typedef struct txl //Address Book { people elem[max]; int count;} Pdh, *por;void init (POR&NBSP;L) //Initialize Address Book { l->count = 0;} Main Menu Void menu () {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRintf ("\t\t************************************\n"); printf ("\t\t***** 1. Add contact information *****\n "); printf (" \t\t* 2. Delete the specified contact information *****\n "); printf (" \t\t***** 3. Find the specified contact information *****\n "); printf (" \t\t***** 4. Modify the specified contact information *****\n "); printf (" \t\t***** 5. Show all contact information *****\n "); printf (" \t\t***** 6. Clear all contact information *****\n "); printf (" \t\t***** 7. Sort all contacts by name *****\n "); printf (" \t\t***** 0. Exit Contacts *****\n "); printf (" \t\t**** ****************\ n "); printf (" \n\t Please select: ");} Add Contact Void _add (por l) { if (L->count >= max) { printf ("Phone book is full, cannot be added!") "); return; } printf (>> Please enter the contact information you want to add!) \ n "); printf (" \n name: "); fflush (stdin); gets (L->elem[l->count].name); printf ("\ n sex: "); fflush (stdin); gets (L->elem[ L->count].sex); printf ("\n Age:"); &NBSP;&NBSP;SCANF ("%d", &l->elem[l->count].age); &nbsP;&NBSP;&NBSP;PRINTF ("\n Tel:"); fflush (stdin); gets (L->elem[l->count].tele); printf ("\n Address:"); fflush (stdin); gets (L->ELEM[L->COUNT].ADR); l->count++; printf ("Add complete! \ n ");} Find contact Static int search (por l, const char * dos) { int i = 0; for (i = 0; i < l->count; i++) { if (strcmp (dos, l->elem[i].name) == 0) { return i; &nBsp; } } return -1;} Delete the specified contact information void _delete (por l) { char arr[max_name]; int i = 0; int ret = 0; printf ("Please enter the name of the contact you want to delete: \ n"); fflush (stdin); gets (arr); ret = search (L, arr); if (ret == -1) { printf ("There is no contact in the Address Book!") \ n "); return; } else { for (i = ret; i < l->count - 1; i++) &NBSP;&Nbsp; { L->elem[i] = L->elem[i + 1]; } L->count--; printf ("Delete succeeded! \ n "); }}//find the specified contact information void _find (por l) { char ptr[max_name]; int ret = 0; printf ("Please enter the name of the contact you are looking for: \ n"); fflush (stdin); Gets (PTR); ret = search (l, ptr); if (ret == -1) { printf ("Find failed, no this contact! \ n "); return; } else { printf ("Find success! \ n "); printf (" name sex Age phone address \n "); printf ("%s %s %d %s %s \n ", l->elem[ret].name, l->elem[ret]. sex, l->elem[ret].age, l->elem[ret].tele, l->elem[ RET].ADR) }}//Modify the specified contact information void _alter (por l) { char ptr[max_name]; int ret = 0; int ter = 0; char a = 0; printf ("Please enter the name of the contact you want to modify: \ n"); fflush ( stdin); gets (PTR); ret = search (L, &NBSP;PTR); if (ret == -1) { printf ("No contact in the Address Book!") \ n "); return; } else { printf ("1. Change name \ n"); printf ("2. Modify sex \ n "); printf (" 3. Age of modification \ n "); printf ("4. Modify phone \ n"); &Nbsp; printf ("5. Change address \ n"); printf ("\ n Please select Modify item:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ( "%d", &ter); switch (ter) { case 1: &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;PRINTF ("Modified by Name:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", l->elem[ret].name); break; case 2: printf ("Modify gender as:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", L->elem[ret] . Sex); break; case 3: printf ("Modify Age:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", L->elem[ret] . Age); break; case 4: printf ("Modify phone as:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", l->elem[ret].tele); Break; case 5: printf ("Modify Address to:"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s", l->elem[ret].adr); break; default: break; } printf ("Modified successfully! \ n "); printf (" Continue typing? y/n "); if ((A = getchar ()) == ' Y ') { _alter (L); } else if ((A = getchar ()) == ' N ') { return; } }}//Show All Contacts Void _show (por l) { int i = 0; if (l->count == 0) { printf ("No Contacts in Contacts!") \ n "); return; } else { for (i = 0; i < l->count; i++) { printf ("\n name Sex Age Phone address \n "); printf (" %s %s %d %s %s\n ", l->elem[i].name, l->elem[i].sex, l->elem[i].age , l->elem[i].tele, l->elem[i].adr); } }}//Delete all contacts Void _delete_all (POR&NBSP;L) { l->count = 0; printf ("Clear success! \ n ");} To bubble sort contacts by name Void _sort (por l) { int i = 0; int j = 0; for (i = 0; i < l->count - 1; i++) { for (j = 0; j < l->count - i - 1; j++) { if (strcmp (l->elem[j].name, l->elem[j + 1].name) > 0) { people temp = L->elem[j]; l- >elem[j] = L->elem[j + 1]; L->elem[j + 1] = temp; } } }}int main () { pdh l; int num = 1; init (&l); while (num) { menu (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d", &num); switch (num) { case 0: exit (EXIT_SUCCESS); break; case 1: &nbSp; _add (&l); break; case 2: _delete (&l); break; case 3: _find (&l); break; case 4: _alter (&l); break; case 5: _show (&l); break; case 6: _delete_all (&l); break; case 7: &nbSp; _sort (&l); break; default: break; } } system ("pause"); return 0;}
This article from "Unintentional persistent" blog, declined reprint!
C Language Writing-contacts (small items)