Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- FILE *fr,*fw;
- int position = 0;
- int arr[10];
- char c;
- int n;
- int i=0,count=0;
- typedef struct birth //defining birthday date
- {
- int day;
- int month;
- int year;
- }birth;
- birth readbirthday() //function for reading birthday from file (used in readcontact() function)
- {
- birth birthday;
- fscanf(fr,",%d",&birthday.day);
- fscanf(fr,"/%d",&birthday.month);
- fscanf(fr,"/%d",&birthday.year);
- return birthday;
- }
- typedef struct contact //defining contacts and its fields
- {
- char firstname[20];
- char lastname[20];
- birth birthday;
- char address[50];
- char email[50];
- int phonenumber;
- }contact;
- contact contacts[100];
- void loadcontacts()
- {
- FILE * fr = fopen("book.txt","r");
- fseek(fr,position,0);
- fscanf(fr,"%[^,]",contacts[i].firstname);
- fscanf(fr,",%[^,]",contacts[i].lastname);
- fscanf(fr,",%d",&contacts[i].birthday.day);
- fscanf(fr,"/%d",&contacts[i].birthday.month);
- fscanf(fr,"/%d",&contacts[i].birthday.year);
- fscanf(fr,",%[^,]",contacts[i].address);
- fscanf(fr,",%[^,]",contacts[i].email);
- fscanf(fr,",%d\n",&contacts[i].phonenumber);
- position = ftell(fr);
- fclose(fr);
- i++;
- }
- int count_contacts()
- {
- int x = 0;//local counter in this function
- fr = fopen("book.txt","r");
- while(!feof(fr))
- {
- if((c=fgetc(fr))=='\n')
- x++;
- }
- fclose(fr);
- return x;
- }
- void printcontact(contact contacts)
- {
- printf("\nFirst name is: %s\n",contacts.firstname);
- printf("Last name is: %s\n",contacts.lastname);
- printf("Birthday: %d/%d/%d\n",contacts.birthday.day,contacts.birthday.month,contacts.birthday.year);
- printf("Address: %s\n", contacts.address);
- printf("E-mail: %s\n",contacts.email);
- printf("Phone number: 0%d\n",contacts.phonenumber);
- }
- void printsorted()
- {
- int x;
- sort();
- for(x=0;x<count;x++)
- {
- printcontact(contacts[x]);
- }
- }
- void add_contact()
- {
- printf("Enter the contact's first name\t:");
- scanf("%s",contacts[count].firstname);
- printf("Enter the contact's last name\t:");
- scanf("%s",contacts[count].lastname);
- printf("Enter the birth date\n");
- printf("enter the day:\t");
- scanf("%d",&contacts[count].birthday.day);
- printf("enter the month\t:");
- scanf("%d",&contacts[count].birthday.month);
- printf("enter the year\t:");
- scanf("%d",&contacts[count].birthday.year);
- printf("Enter the address: ");
- scanf(" %199[^\n]",contacts[count].address);
- printf("Enter the E-mail: ");
- scanf("%s",contacts[count].email);
- printf("Enter phone number\t:");
- scanf("%d",&contacts[count].phonenumber);
- count++;
- display_menu();
- switch_function();
- }
- int* searchlastname()
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring(contacts[x].lastname,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- {//for(j=0;j<n;j++)
- //printcontact(contacts[arr[j]]);
- return p;
- }
- }
- void searchmenulastname()
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring(contacts[x].lastname,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- { for(j=0;j<n;j++)
- printcontact(contacts[arr[j]]);
- }
- }
- void modify()
- {
- int x,y;
- int*p = searchlastname();
- for(x=0;x<n;x++)
- {
- printf("Contact number %d:\n",x+1);
- printcontact(contacts[*(p+x)]);
- }
- printf("Choose the contact's number to modify:");
- scanf("%d",&y);
- printcontact(contacts[*(p+y-1)]);
- printf("Enter the contact's first name\t:");
- scanf("%s",contacts[*(p+y-1)].firstname);
- printf("Enter the contact's last name\t:");
- scanf("%s",contacts[*(p+y-1)].lastname);
- printf("Enter the birth date\n");
- printf("enter the day:\t");
- scanf("%d",&contacts[*(p+y-1)].birthday.day);
- printf("enter the month\t:");
- scanf("%d",&contacts[*(p+y-1)].birthday.month);
- printf("enter the year\t:");
- scanf("%d",&contacts[*(p+y-1)].birthday.year);
- printf("Enter the address: ");
- scanf(" %199[^\n]",contacts[*(p+y-1)].address);
- printf("Enter the E-mail: ");
- scanf("%s",contacts[*(p+y-1)].email);
- printf("Enter phone number\t:");
- scanf("%d",&contacts[*(p+y-1)].phonenumber);
- printf("Contact is modified.\n");
- display_menu();
- switch_function();
- }
- void deletecontact()
- {
- int x,y,flag=0;
- char firstname[20],lastname[20],key[5];
- printf("Enter first name: ");
- scanf("%s",firstname);
- printf("Enter last name: ");
- scanf("%s",lastname);
- for(x=0;x<count;x++)
- {
- if(strcmp(contacts[x].firstname,firstname)==0&&strcmp(contacts[x].lastname,lastname)==0)
- {
- flag =1;
- printcontact(contacts[x]);
- printf("Are you sure? (yes/no)");
- scanf("%s",key);
- if (strcmp(key,"yes")==0||strcmp(key,"Yes")==0)
- {
- for(y=x;y<count-1;y++)
- contacts[y]=contacts[y+1];
- count--;
- display_menu();
- switch_function();
- }
- else
- {
- display_menu();
- switch_function();
- }
- }
- }
- if(flag==0)
- {
- printf("Search not found\nDo you want to search again?(yes/no)");
- scanf("%s",key);
- if (strcmp(key,"yes")==0||strcmp(key,"Yes")==0)
- deletecontact();
- else{
- display_menu();
- switch_function();
- }
- }
- }
- void save()
- {
- fw = fopen("book.txt","w");
- int x;
- for(x=0;x<count;x++)
- {
- fprintf(fw,"%s,",contacts[x].firstname);
- fprintf(fw,"%s,",contacts[x].lastname);
- fprintf(fw,"%d/",contacts[x].birthday.day);
- fprintf(fw,"%d/",contacts[x].birthday.month);
- fprintf(fw,"%d,",contacts[x].birthday.year);
- fprintf(fw,"%s,",contacts[x].address);
- fprintf(fw,"%s,",contacts[x].email);
- fprintf(fw,"%d\n",contacts[x].phonenumber);
- }
- fclose(fw);
- }
- void searchmenufirstname()
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring(contacts[x].firstname,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- { for(j=0;j<n;j++)
- printcontact(contacts[arr[j]]);
- }
- }
- int* searchfirstname()
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring(contacts[x].firstname,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- { //for(j=0;j<n;j++)
- //printcontact(contacts[arr[j]]);
- return p;
- }
- }
- int* searchphonenumber()//needs to be rechecked
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring((char)contacts[x].phonenumber,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- { //for(j=0;j<n;j++)
- //printcontact(contacts[arr[j]]);
- return p;
- }
- }
- void searchmenuphonenumber()//needs to be rechecked
- {
- int x,j=0;
- char key[30],confirm[5];
- int *p=arr;
- printf("Enter the search key: ");
- scanf("%s",key);
- for(x=0;x<count;x++)
- {
- if(substring((char)contacts[x].phonenumber,key)==1)
- {
- *(p+j)=x;
- j++;
- n=j;
- }
- }
- if(j==0)
- {printf("Search not found\n");
- printf("Do you want to search again? (yes/no)");
- scanf("%s",confirm);
- if(confirm=="yes" || confirm=="YES")
- searchlastname();
- else display_menu();
- }
- if(j!=0)
- { for(j=0;j<n;j++)
- printcontact(contacts[arr[j]]);
- }
- }
- int substring(char name[],char key[])
- {
- int x,j,flag;
- if(strlen(key)>strlen(name)){
- flag = 0;
- return flag;
- }
- for(x=0;x<=strlen(name)-strlen(key);x++)
- {
- for(j=0;j<strlen(key);j++)
- {
- flag = 1;
- if(key[j] != name[j+x])
- {
- flag = 0;
- break;
- }
- }
- if (flag ==1)
- break;
- }
- return flag;
- }
- void search_menu()
- {
- int y;
- printf("Search by:\n1-First name\n2-Last name\n3-Phone number\n");
- scanf("%d",&y);
- switch(y)
- {
- case(1): searchmenufirstname();break;
- case(2): searchmenulastname();break;
- case(3): searchmenuphonenumber();break;
- default: printf("Invalid input\n"); search_menu();//break;
- }
- }
- void quit()
- {
- char confirm[5];
- printf("Are you sure you want to exit? (yes/no)\n");
- scanf("%s",&confirm);
- if (strcmp(confirm,"yes")==0||strcmp(confirm,"Yes")==0)
- exit(0);
- else{
- display_menu();
- switch_function();
- }
- }
- void sort()
- {
- int pass,x;
- contact temp;
- for (pass=1; pass<count; pass++)
- {
- for(x=0; x<count-1; x++)
- {
- if (strcmp(contacts[x].lastname,contacts[x+1].lastname)>0)
- {
- temp=contacts[x];
- contacts[x]=contacts[x+1];
- contacts[x+1]=temp;
- }
- else if(strcmp(contacts[x].lastname,contacts[x+1].lastname)==0)
- {
- if (strcmp(contacts[x].firstname,contacts[x+1].firstname)>0)
- {
- temp=contacts[x];
- contacts[x]=contacts[x+1];
- contacts[x+1]=temp;
- }
- }
- }
- }
- }
- void display_menu()
- {
- printf("\nChoose an option:\n1-Add contact\n2-Delete\n3-Modify\n4-Search\n5-Print\n6-Save\n7-Quit\n");
- }
- void switch_function()
- {
- printf("Please enter a number: ");
- int x;
- scanf("%d",&x);
- switch(x)
- {
- case(1): add_contact();break;
- case(2): deletecontact();break;
- case(3): modify();break;
- case(4): search_menu();break;
- case(5): printsorted();break;
- case(6): save();break;
- case(7): quit();break;
- default: printf("Invalid input\n"); switch_function();//break;
- }
- // if (x>7||x<1)
- // {
- // switch_function();
- // }
- }
- int main()
- {
- printf("***********\n*Phonebook*\n***********\n");
- count = count_contacts();
- int x;
- for(x=0;x<count;x++)
- loadcontacts();
- display_menu();
- switch_function();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement