Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- FILE *fw,*fr;
- int position=0;
- int arr[10]; //array for results of search
- int i=0;
- char c;
- int n;
- int 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);
- }
- int count_contacts()
- {
- int x = 0; // x is a local counter
- 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 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++;
- }
- 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);
- }
- int* searchlastname()
- {
- int x,j=0;
- char key[30];
- 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 or No)");
- char confirm [5];
- 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* searchfirstname()
- {
- int x,j=0;
- char key[30];
- 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 or No)");
- char confirm [5];
- scanf("%s", confirm);
- if (confirm=="yes"||confirm=="Yes")
- searchfirstname();
- else
- display_menu();
- }
- if(j!=0)
- { //for(j=0;j<n;j++)
- //printcontact(contacts[arr[j]]);
- return p;
- }
- }
- int* searchphonenumber()
- {
- int x,j=0;
- char key[30];
- 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 or No)");
- char confirm [5];
- scanf("%s", confirm);
- if (confirm=="yes"||confirm=="Yes")
- searchphonenumber();
- else
- display_menu();
- }
- if(j!=0)
- { //for(j=0;j<n;j++)
- //printcontact(contacts[arr[j]]);
- return p;
- }
- }
- 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()
- {
- printf("Search by: \n1 - First name.\n2 - Last name.\n3 - Phone number.");
- int y;
- scanf("%d",&y);
- switch (y)
- {
- case (1): searchfirstname(); break;
- case (2): searchlastname(); break;
- case (3): searchphonenumber(); break;
- default: printf("Invalid input!");search_menu();break;
- }
- }
- void quit()
- {
- printf("Are you sure you want to exit?: (Yes or No)");
- char confirm [10];
- scanf("%s", &confirm);
- if (confirm=="yes"||confirm=="Yes")
- exit(0);
- else
- {
- display_menu();
- switch_function();
- }
- }
- void display_menu()
- {
- printf("***********\n*Phonebook*\n***********\n\nChoose an option:\n1-Add contact\n2-Delete\n3-Modify\n4-Search\n5-Print\n6-Save\n7-Quit\n");
- }
- void switch_function()
- {
- int x; //action
- printf("Please choose a number: ");
- scanf("%d",&x);
- switch (x)
- {
- case(1): add_contact();break;
- case(2): printf("add");break;
- case(3): printf("add");break;
- case(4): search_menu();break;
- case(5): printf("add");break;
- case(6): save();break;
- case(7): quit();break;
- default: printf("Invalid input!");switch_function();//break;
- }
- }
- int main ()
- {
- count = count_contacts();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement