Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- FILE *pb;
- int counter=0;
- typedef struct
- {
- char fname[20];
- char lname[20];
- char address[50];
- char city[20];
- char number[16];
- } phonebook;
- phonebook person[100];
- void Load();
- void add();
- void delete_();
- void modify();
- void save();
- void print();
- void main()
- {
- char x[20];
- int op,p;
- printf("|-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-|\n\n");
- printf("choose:\n");
- printf("1)Load contacts.\n2)show all contacts\n3)Add contact.\n4)Modify.\n5)delete contact.\n6)Search.\n7)save\n8)save & exit.\n9)exit without saving.\n\n");
- scanf(" %s",&x);
- op=atoi(x);
- while(op!=9)
- {
- system("cls");
- if(op<1||op>9)
- printf("enter a vaild number.\n\n");
- else
- {
- switch (op)
- {
- case 1:
- Load();
- break;
- case 2:
- print();
- break;
- case 3:
- add();
- break;
- case 4:
- modify();
- break;
- case 5:
- delete_();
- break;
- case 6:
- printf("enter the last name of contatct you search for:\n");
- search_(p);
- break;
- case 7:
- save1();
- break;
- case 8:
- save();
- break;
- }
- }
- printf("\n-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-\n\n");
- printf("choose:\n");
- printf("1)Load contacts.\n2)show all contacts\n3)Add contact.\n4)Modify.\n5)delete contact.\n6)Search.\n7)save\n8)save & exit.\n9)exit without saving.\n\n");
- scanf(" %s",&x);
- op=atoi(x);
- }
- }
- void Load()
- {
- int i=0;
- pb = fopen("phonebook.txt","r");
- if(pb==NULL)
- printf("no such file");
- else
- {
- while(!feof(pb))
- {
- fscanf(pb,"%[^,],%[^,],%[^,],%[^,],%s\n",person[counter].fname,person[counter].lname,person[counter].address,person[counter].city,person[counter].number);
- counter++;
- }
- }
- fclose(pb);
- printf("contacts have been loaded");
- }
- void add()
- {
- int i;
- if(counter ==99)
- {
- system("cls");
- printf("the memory is full, you cant add another contact.\n\n");
- }
- else
- {
- printf("first name: ");
- getchar();
- scanf("%[^\n]s",person[counter].fname);
- printf("last name: ");
- scanf(" %[^\n]s",person[counter].lname);
- printf("address: ");
- scanf(" %[^\n]s",person[counter].address);
- printf("city: ");
- scanf(" %[^\n]s",person[counter].city);
- printf("Number: ");
- scanf(" %[^\n]s",person[counter].number);
- counter++;
- printf("\n\nA new contact has been added.");
- }
- }
- void delete_()
- {
- int i,n,p,x;
- char z[10];
- printf("enter the last name of the contact you want to delete:\n");
- x=search_(p);
- if(x==0);
- else{
- printf("enter number of the contact you want to modify:\n");
- scanf("%s",z);
- n=atoi(z);
- if(n<1||n>counter)
- {
- system("cls");
- printf("not a valid choise");
- }
- else
- {
- n--;
- for(i=n; i<=counter; i++)
- {
- strcpy(person[i].fname,person[i+1].fname);
- strcpy(person[i].lname,person[i+1].lname);
- strcpy(person[i].address,person[i+1].address);
- strcpy(person[i].city,person[i+1].city);
- strcpy(person[i].number,person[i+1].number);
- }
- counter--;
- system("cls");
- printf("\nA contact has been deleted.");
- }
- }
- }
- void modify()
- {
- char names[20];
- int i,n,p,x;
- char m,z[10];
- printf("enter the last name of contatct you want to modify:\n");
- x=search_(p);
- if(x==0);
- else{
- printf("\nenter number of the contact you want to modify:\n");
- scanf(" %s",z);
- n=atoi(z);
- if(n<1||n>counter)
- {
- system("cls");
- printf("not a valid number");
- }
- else
- {
- n--;
- system("cls");
- printf("%d)\nfirst name: %s\nlast name: %s\naddress: %s\nCity: %s\nNumber: %s\n",n+1,person[n].fname,person[n].lname,person[n].address,person[n].city,person[n].number);
- printf("\n\nyou want to modify:\n");
- printf("1-first name\n2-last name\n3-number\n4-address\n5-city\n6-Done.\n\n");
- scanf(" %c",&m);
- while(m!='6')
- {
- if(m!='1'&&m!='2'&&m!='3'&&m!='4'&&m!='5')
- {
- printf("enter a valid number.\n\n");
- }
- else if(m=='1')
- {
- printf("Enter the new first name:\n");
- scanf("%s",person[n].fname);
- }
- else if(m=='2')
- {
- printf("Enter the new last name:\n");
- scanf("%s",person[n].lname);
- }
- else if(m=='3')
- {
- printf("Enter the new number:\n");
- scanf("%s",person[n].number);
- }
- else if(m=='4')
- {
- printf("Enter the new Address:\n");
- scanf(" %[^\n]s",person[n].address);
- }
- else if(m=='5')
- {
- printf("Enter the new City:\n");
- scanf("%s",person[n].city);
- }
- printf("\nenter the number of contact you want to modify: ");
- printf("\n1-first name\n2-last name\n3-number\n4-address\n5-city\n6-DONE\n\n");
- scanf(" %c",&m);
- }
- if(m=='6')
- {
- system("cls");
- printf("\n\nthe contact after modification:\n");
- printf("first name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n",person[n].fname,person[n].lname,person[n].address,person[n].city,person[n].number);
- }
- printf("\n\nA contact has been modified.");
- }
- }
- }
- void search_(int p)
- {
- int i;
- p=0;
- char name[20];
- scanf("%s",name);
- system("cls");
- printf("the results:\n");
- for(i=0; i<counter ; i++)
- {
- if(strcmpi(name,person[i].lname)==0)
- {
- printf("%d)\nfirst name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n\n\n",i+1,person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
- p++;
- }
- }
- if(p==0)
- {
- printf("the name doesn't exist.\n");
- }
- return p;
- }
- void save1()
- {
- int i;
- pb=fopen("phonebook.txt","w");
- for(i=0; i<counter ; i++)
- {
- fprintf(pb,"%s,%s,%s,%s,%s",person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
- if(i!=counter-1)
- fprintf(pb,"\n");
- }
- fclose(pb);
- printf("\n\nyour changes saved.\n\n");
- }
- void save()
- {
- int i;
- pb=fopen("phonebook.txt","w");
- for(i=0; i<counter ; i++)
- {
- fprintf(pb,"%s,%s,%s,%s,%s",person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
- if(i!=counter-1)
- fprintf(pb,"\n");
- }
- fclose(pb);
- printf("\n\nyour changes saved.\n\n");
- exit(0);
- }
- void print()
- {
- int i,j,k,l;
- phonebook swap_;
- for (i=0 ; i < counter; i++)
- {
- for (j=0; j < counter-1; j++)
- {
- if (strcmpi(person[j].lname,person[j+1].lname)>0)
- {
- swap_=person[j];
- person[j]=person[j+1];
- person[j+1]=swap_;
- }
- else if(strcmpi(person[j].lname,person[j+1].lname)==0)
- {
- if (strcmpi(person[j].fname,person[j+1].fname)>0)
- {
- swap_=person[j];
- person[j]=person[j+1];
- person[j+1]=swap_;
- }
- else if(strcmpi(person[j].fname,person[j+1].fname)==0)
- {
- if(strcmp(person[j].number,person[j+1].number)>0)
- {
- swap_=person[j];
- person[j]=person[j+1];
- person[j+1]=swap_;
- }
- }
- }
- else
- continue;
- }
- }
- system("cls");
- for (i=0 ; i <counter; i++)
- printf("%d)\nfirst name: %s\nlast name: %s\nAddress: %s\nCity: %s\nNumber: %s\n",i+1,person[i].fname,person[i].lname,person[i].address,person[i].city,person[i].number);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement