"C Language" address Book (a file implementation)
#include <stdio.h> #include <string.h> #define NAME_MAX 20#define sex_max 5#define tel_max 11#define Addr_max 20#define person_max 1000typedef struct Person{char name[name_max];char sex[sex_max];int Age;char Tel[tel_max];char Addr[addr_max];} *pperson;typedef struct contact{struct person telbook[person_max];int count;} *pcon;void print () {printf ("************************************************************************\n");p rintf ( " || || \ n ");p rintf (" | | 1. Add a contact 2. Delete a contact 3. Find a Contact | | \ n ");p rintf (" | | 4. Modify the Contact 5. Show contacts 6. Clear Contacts | | \ n ");p rintf (" | | | | \ n ");p rintf (" ************************************************************************\n ");} int find (PCon Pcon,char *name) {int i=0;for (i=0;i<pcon->count;i++) {if (strcmp ((Pcon->telbook[i]). Name,name) ==0) return i;} return-1;} void add_peo (PCon pCon) {if (pcon->count) ==person_max) printf ("The Address Book is full, please delete it and add it again.") \ n ");p rintf (" Name: "); scanf ("%s ", (Pcon->telbook[pcon->count]). Name);p rintf (" Gender: "); scanf ("%s ", pcon-> Telbook[pcon->count]). Sex);p rintf ("Age:"), scanf ("%d",& (Pcon->telbook[pcon->count]). Age);p rintf (" Tel: "); scanf ("%s ", (Pcon->telbook[pcon->count]). Tel);p rintf (" Home Address: "); scanf ("%s ", pcon->telbook[pcon- >count]). addr);p con->count++;} void Del_peo (PCon pCon) {int Ret=0;char Peoplename[name_max];p rintf ("Please enter the contact you want to delete:"); scanf ("%s", peoplename); ret=find (pcon,peoplename); if (ret==-1) {printf ("The contact does not exist. \ n ");} Else{int j;for (j=ret;j<pcon->count-1;j++) {pcon->telbook[j]=pcon->telbook[j+1];} printf ("Delete contact succeeded.") \ n ");} pcon->count--;} void Search_peo (PCon pCon) {int Ret=0;char Peoplename[name_max];p rintf ("Please enter the contact you are looking for:"); scanf ("%s", peoplename); ret= Find (Pcon,peoplename); if (ret==-1) {printf ("The contact does not exist. \ n ");} else{printf ("%s\t", (Pcon->telbook[ret]). Name);p rintf ("%s\t", (Pcon->telbook[ret]). Sex);p rintf ("%d\t",(Pcon->telbook[ret]). Age);p rintf ("%s\t", (Pcon->telbook[ret]). Tel);p rintf ("%s\n", (Pcon->telbook[ret]). addr);}} void Modil_peo (PCon pCon) {int Ret=0;char Peoplename[name_max];p rintf ("Please enter the contact you want to modify:"); scanf ("%s", peoplename); ret= Find (Pcon,peoplename); if (ret==-1) {printf ("The contact does not exist. ");} else{printf ("Name:"); scanf ("%s", (Pcon->telbook[ret]). Name);p rintf ("Gender:"); scanf ("%s", (Pcon->telbook[ret]). Sex);p rintf ("Age:"); scanf ("%d",& (Pcon->telbook[ret]);p rintf ("Phone:"); scanf ("%s", Pcon->telbook[ret ]. Tel);p rintf ("Home Address:"); scanf ("%s", (Pcon->telbook[ret]). addr);}} void Show_peo (PCon pCon) {int i;for (i=0;i<pcon->count;i++) {printf ("%d.%s\t", I+1, (Pcon->telbook[i]). Name); printf ("%s\t", (Pcon->telbook[i]). Sex);p rintf ("%d\t", (Pcon->telbook[i]). Age);p rintf ("%s\t", pcon-> Telbook[i]). Tel);p rintf ("%s\n", (Pcon->telbook[i]). addr);}} void Clear_peo (PCon pCon) {pcon->count=0;printf ("The contacts have been emptied. \ n ");} int main () {struct contact my_contact;int input;my_contact.count=0;while (1) {PrinT ();p rintf ("Please select operation:"), scanf ("%d", &input), switch (input) {case 1:ADD_PEO (&my_contact); Break;case 2:del_peo ( &my_contact); break;case 3:search_peo (&my_contact); Break;case 4:modil_peo (&my_contact); Break;case 5: Show_peo (&my_contact); Break;case 6:clear_peo (&my_contact); break;default:printf ("Operation error \ n"); break;}} return 0;}
C Language Address Book code (implemented under one file)