Académique Documents
Professionnel Documents
Culture Documents
;phone}
;phone *start
******************************************************************************//
;(void load(void
;(void modify(void
;(void removeall(void
;(void delone(const char *s
;(void global_adition(void
;(void addrecord(void
;(void writetofile(void
;(void DisplayList(void
;(void DisplayPhone(phone* ptr
;([[phone* findbyfirstname(const char pname
;(void main_menu(void
;(void load (void
******************************************************************************//
((void main
;int ok {
;[char pname[10
;phone *pointer
;start=NULL
;((load
{do
;((main_menu
;(fflush(stdin
;((ok =getchar
;(fflush(stdin
(switch (ok
{
;((case '1': global_adition
;break
((void main_menu
{
;("***printf("\n\t\t\t*** PHONE - BOOK
;("printf("\n\t\t\t*** MAIN MENU ***\n
;("printf("\n\t*** please select one of the foloing options***\n\n
;("printf("1.Add a new record\n
;("printf("2.Display the Phone Book\n
;("printf("3.Delete all records\n
;("printf("4.Delete file\n
;("printf("5.Delete one record\n
;("printf("6.Serch by first name\n
;("printf("7.Modify\n
;("printf("8.Save\n
;("printf("Esc to exit\n
;("printf ("\nEnter your choice:\n
}
******************************************************************************//
this fanction is saving data to the file it tell as if it can open//
or cant writ from or to the file//
(void writetofile(void
{
;FILE *fp
;phone *p=start
(if((fp=fopen(FILENAME,"wb"((==NULL
{
;(printf("ERROR OPENING FILE %s\n",FILENAME
;(getch((; fflush(stdin
;(exit(2
}
(while(p
{
(if(fwrite(p,sizeof(phone(,1,fp(!=1
{
;(printf("ERROR WRITING TO FILE %s\n",FILENAME
;(getch((; fflush(stdin
;(exit(2
}
;p=p->next
}
;(fclose(fp
}
******************************************************************************//
this fanction job is to call addrecord fanction and ask the user if he//
wonts to add another one//
(void global_adition(void
{
;int ok
{do
;((addrecord
;("printf("\n\tMORE?->(yes/no(\n
;(((ok=tolower(getch
;(fflush(stdin
;('while(ok!='n}
}
******************************************************************************//
this fanction allow us to add some person to the phone book//
((void addrecord
{
;phone *ptr,*s,*f
;((ptr=(phone*(malloc(sizeof(phone
;(" :printf("\nEnter FirstName
;(gets(ptr->name.FirstName
;(" :printf("Enter LastName
;(gets(ptr->name.LastName
;(" :printf("Enter Street
;(gets(ptr->address.Street
;(printf("Enter %s 's street number: ",ptr->name.FirstName
;(gets(ptr->address.number
;(" :printf("Enter City
;(gets(ptr->address.city
;(printf("Enter %s 's phone number: ",ptr->name.FirstName
;(gets(ptr->tel.phone
;s=start
;f=start
(while(f != NULL && strcmp(f->name.FirstName,ptr->name.FirstName(<0
{
;s=f
;f=f->next
}
(if(s==start
{
;ptr-> next =start
; start=ptr
}
else
{
;ptr->next =s ->next
;s->next=ptr
}
}
******************************************************************************//
this fanction give us the option to show the user the person exist//
on his phone book//
(void DisplayList(void
{
;phone *p=start
(while(p
{
;((clrscr
;(DisplayPhone(p
;p=p->next
;("printf("\n\n\tPress the enter key\n\n
; ((getch
;((getchar
;(fflush(stdin
}
}
((void removeall
{
;phone* temp=start,*ptr
(if(start
{
(while(temp
{
;ptr=temp
;temp=temp->next
;(free(ptr
}
(if(ptr
;(free(ptr
;start=ptr=NULL
}
}
******************************************************************************//
this fanction is deleting only one record at a time it searches the name//
by first name//
(void delone(const char *s
{
;phone *ptr,*temp=start,*temp1
;[char pname[10
;(printf("%s",s
;(gets(pname
ptr=findbyfirstname(pname(;//leyvar she mohkim
(if(!ptr
;("printf("There no such person.\n
else
{
(while(temp!=ptr
{
;temp1=temp
;temp=temp->next
}
;temp1->next=ptr->next
;(free(ptr
}
}
******************************************************************************//
this fanction help the user find his persons in the phone book//
it also needed in the delone fanction and modify fanction//
((void load
{
;FILE* fp
;phone *ptr, *ptr1
(if((fp=fopen(FILENAME,"r"((==NULL
{
;(printf("Unable opening. File %s is empty.\n",FILENAME
;("printf("\t\tPress enter to continue.\a
;((getch
;((getchar
;(fflush(stdin
}
else
{
((if(!feof(fp
{
;((start=ptr=(phone*(malloc(sizeof(phone
;(fread((void*(ptr,sizeof(phone(,1,fp
;ptr->next=0
((while(!feof(fp
{
;((ptr1=(phone*(malloc(sizeof(phone
;(fread((void*(ptr1,sizeof(phone(,1,fp
;if(feof(fp(( break
;ptr1->next=0
;ptr->next=ptr1
;ptr=ptr1
}
;(fclose(fp
}
}
}