Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<ctype.h>
- #define MAX_SIZE 100
- typedef struct
- {
- char firstname[40];
- char lastname[40];
- char mobileNumber[20];
- } contact;
- typedef enum {FIRST_NAME,LAST_NAME,MOBILE_NUMBER} search_t;
- contact phonebook[MAX_SIZE];
- int currentSize=0;
- /*Function prototype*/
- void addEntry();
- void display (int index);
- void search(char *key,search_t type);
- void saveToFile();
- void readFromFile();
- void displaAll();
- void removeEntry(char *mobileNumber);
- void strtolower(char *str);
- void clear();
- int main()
- {
- system("Color A");
- readFromFile();
- int i;
- char selection[41];
- int choice;
- while(1)
- {
- puts(" Phonebook Application main menu");
- puts(" 1.Insert a new Entry");
- puts(" 2.Display All Entry");
- puts(" 3.Search by First Name");
- puts(" 4..Search by Last name");
- puts(" 5.Search by mobile Number");
- puts(" 6.Delete a contact(by Mobile number) ");
- puts(" 7.Save Database");
- puts(" 8.Exit");
- do
- {
- printf("Enter your choice:");
- gets(selection);
- choice=atoi(selection);
- }
- while(choice<1 || choice>8);
- switch(choice)
- {
- case 1:
- addEntry();
- clear();
- break;
- case 2:
- displayAll();
- clear();
- break;
- case 3:
- printf("Enter first name to search:");
- gets(selection);
- search(selection,FIRST_NAME);
- clear();
- break;
- case 4:
- printf("Enter last name to search:");
- gets(selection);
- search(selection,LAST_NAME);
- clear();
- break;
- case 5:
- printf("enter mobile number to search:");
- gets(selection);
- search(selection,MOBILE_NUMBER);
- clear();
- break;
- case 6:
- printf("enter mobile number to delete :");
- gets(selection);
- removeEntry(selection);
- clear();
- break;
- case 7:
- saveToFile();
- break;
- case 8:
- exit(0);
- break;
- default:
- break;
- }
- }
- return 0;
- }
- void clear()
- {
- system("PAUSE");
- system("CLS");
- }
- void addEntry()
- {
- if(currentSize==MAX_SIZE)
- {
- puts("Error:phonebook is full!");
- return ;
- }
- contact c;
- printf("Enter first name:");
- gets(c.firstname);
- printf("Enter Last name:");
- gets(c.lastname);
- printf("Enter mobile number:");
- gets(c.mobileNumber);
- phonebook[currentSize]=c;
- ++currentSize;
- printf("Contact Added.\n\n");
- }
- void display(int index)
- {
- if(index < 0 ||index>=currentSize)
- {
- puts("Error:invalid index!");
- return ;
- }
- contact c=phonebook[index];
- printf("First Name: %s\n",c.firstname);
- printf("Last Name: %s\n",c.lastname);
- printf("Mobile no: %s\n",c.mobileNumber);
- }
- void displayAll()
- {
- if(currentSize==0)
- {
- puts("Phonebook is empty!");
- return ;
- }
- int i;
- for(i=0; i<currentSize; i++)
- {
- display(i);
- puts(" ");
- }
- }
- void strtolower(char *str)
- {
- int len=strlen(str);
- int i;
- for(i=0; i<len; i++)
- {
- str[i]=tolower(str[i]);
- }
- }
- void search(char *key,search_t type)
- {
- int found =0;
- int i;
- strtolower(key);
- char content[41];
- if(type==FIRST_NAME)//search by first name
- {
- for(i=0; i<currentSize; i++)
- {
- strcpy(content,phonebook[i].firstname);
- strtolower(content);
- if(strcasecmp(content,key)==0)
- {
- display(i);
- found=1;
- }
- }
- }
- else if(type==LAST_NAME)
- {
- for(i=0; i<currentSize; i++)
- {
- strcpy(content,phonebook[i].lastname);
- strtolower(content);
- if(strcasecmp(content,key)==0)
- {
- display(i);
- found=1;
- }
- }
- }
- else if (type==MOBILE_NUMBER)
- {
- for(i=0; i<currentSize; i++)
- {
- strcpy(content,phonebook[i].mobileNumber);
- strtolower(content);
- if(strcasecmp(content,key)==0)
- {
- display(i);
- found=1;
- }
- }
- }
- else
- {
- puts("Error! Invalid search type!");
- return;
- }
- if(!found)
- {
- puts("Not found in the phonebook");
- }
- }
- void readFromFile()
- {
- FILE *fp;
- if((fp=fopen("phonebook.db", "rb"))==NULL)
- {
- puts("Error:can't load the database file");
- return;
- }
- if(fread(¤tSize,sizeof(currentSize),1,fp)!=1)
- {
- puts("Error:can't read data!");
- return ;
- }
- if(fread(phonebook,sizeof(phonebook),1,fp)!=1)
- {
- puts("Error:can't read data!");
- return ;
- }
- }
- void removeEntry(char *mobileNumber)
- {
- if(currentSize==0)
- {
- puts("Phnebook is empty ! Nothing to delete");
- return ;
- }
- int i,j;
- int count=0;
- for(i=0; i<currentSize; i++)
- {
- if(strcmp(phonebook[i].mobileNumber,mobileNumber)==0)
- {
- for(j=0; j<currentSize-1; j++)
- {
- phonebook[j]=phonebook[j+1];
- }
- strcpy(phonebook[j].firstname,"");
- strcpy(phonebook[j].lastname,"");
- strcpy(phonebook[j].mobileNumber,"");
- currentSize-=1;
- ++count;
- }
- }
- if(count==0)
- {
- puts("No entry deleted");
- }
- else
- {
- printf("%d entries deleted\n ",count);
- }
- }
- void saveToFile()
- {
- FILE *fp;
- if( (fp=fopen("phonebook.db","wb"))==NULL)
- {
- puts("Error:can't create a database file!");
- return ;
- }
- if( fwrite(¤tSize,sizeof(currentSize),1,fp)!=1)
- {
- puts("Error:can't save data!");
- return ;
- }
- if(fwrite(phonebook,sizeof(phonebook),1,fp)!=1)
- {
- puts("Error:can't save data!");
- return ;
- }
- puts("Phonebook save to file successfully!\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment