phone book بلغة السى
يعمل أندر دوس
والأهم إنه أوبين سورس
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <io.h>
#include <conio.h>
#define PHONE_MIN 7
struct strc{
int id;
char name[20];
char address[25];
char phone[20];
}info;
void getentry();
void putentry(int);
void delentry(int);
int countentry();
int readentry(int,int);
void searchentry();
int main(void)
{
register char ch;
register int sz;
int id;
char intr[]="############################################################\n"
"## Insert Entry (I) || call by ID (C) || See All (A) #\n"
"## Edit Entry (T) || Delete (D) || search (S) || Exit (X) #\n"
"############################################################";
puts(intr);
do{
ch=toupper(ch=getch());
fflush(stdin);
switch(ch)
{
case 'I':
printf("Inserting Entry Number: %d\n", (countentry()+1));
getentry();
putentry(0);
puts("Done");
puts(intr);
break;
case 'C':
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
printf("You have %d Entries\n", countentry());
puts("Enter the ID of your entry");
do{
scanf("%d", &id);
fflush(stdin);
if(id>sz) printf("You have only %d entries\ntry again\n", sz);
}while(id>sz);
puts("+---+----------------------+---------------------------+----------------------+");
printf("|ID | %-20s| %-25s| %-20s|\n", " NAME"," ADDRESS"," PHONE #");
readentry(id,1);
puts("+---+----------------------+---------------------------+----------------------+");
puts("Done");
puts(intr);
break;
case 'A':
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
puts("Displaying All Entries");
puts("+---+----------------------+---------------------------+----------------------+");
printf("|ID | %-20s| %-25s| %-20s|\n", " NAME"," ADDRESS"," PHONE #");
for(id=1; id<=sz; id++)
{
readentry(id,1);
if(id==20)
{
puts("+---+----------------------+---------------------------+----------------------+");
puts("Press any key to continue..");
getch();
}
}
puts("+---+----------------------+---------------------------+----------------------+");
puts("Done");
puts(intr);
break;
case 'T':
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
puts("Editing Entry");
puts("Enter ID of your entry");
do{
scanf("%d", &id);
fflush(stdin);
if(id>sz) printf("You have only %d entries\ntry again\n", sz);
}while(id>sz);
puts("+---+----------------------+---------------------------+----------------------+");
printf("|ID | %-20s| %-25s| %-20s|\n", " NAME"," ADDRESS"," PHONE #");
readentry(id,1);
puts("+---+----------------------+---------------------------+----------------------+");
getentry();
putentry(id);
puts("Done");
puts(intr);
break;
case 'D':
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
puts("Deleting Entry");
puts("Enter ID of your entry");
do{
scanf("%d", &id);
fflush(stdin);
if(id>sz) printf("You have only %d entries\ntry again\n", sz);
}while(id>sz);
puts("+---+----------------------+---------------------------+----------------------+");
printf("|ID | %-20s| %-25s| %-20s|\n", " NAME"," ADDRESS"," PHONE #");
readentry(id,1);
puts("+---+----------------------+---------------------------+----------------------+");
delentry(id);
puts("Done");
puts(intr);
break;
case 'S':
if(!(sz=countentry()))
{
puts("Couldn't find the entry file");
puts(intr);
break;
}
searchentry();
puts(intr);
break;
case 'X':
printf("%c\n",ch);
printf("Ending...\n\n");
default:
;
}
}while(ch!='X');
return 0;
}
void getentry()
{
register int i;
puts("Full Name:");
fgets(info.name, 20, stdin);
fflush(stdin);
if(info.name[0]=='\n') info.name[0]='\0';
else strtok(info.name, "\n");
puts("address:");
fgets(info.address, 25, stdin);
fflush(stdin);
if(info.address[0]=='\n') info.address[0]='\0';
else strtok(info.address, "\n");
do{
puts("phone #:");
fgets(info.phone, 20, stdin);
fflush(stdin);
if(info.phone[0]=='\n') puts("You should enter a phone #");
else {
strtok(info.phone,"\n");
for(i=0;info.phone;i++)
if(!isdigit(info.phone))
{
puts("You can only enter digits");
info.phone[0]='\n';
break;
};
if(i<PHONE_MIN && info.phone[0]!='\n')
{
puts("The Phone # should contain more than 6 digits");
info.phone[0]='\n';
}
}
}while(info.phone[0]=='\n');
if(info.id=countentry()) info.id++;
else info.id=1;
}
void putentry(register int id)
{
FILE* fp;
register int sz;
if(!(sz=countentry()))
{
fp=fopen("list.phn", "a");
fclose(fp);
id=1;
}
if(!(fp=fopen("list.phn", "r+b")))
puts("Couldn't find the entry file");
else
{
if(id)
{
info.id=id;
fseek(fp, --id*sizeof(struct strc), SEEK_SET);
}else fseek(fp, sz*sizeof(struct strc), SEEK_SET);
fwrite(&info, sizeof(struct strc), 1, fp);
fclose(fp);
}
}
int countentry()
{
FILE* fp;
register int sz;
if(!(fp=fopen("list.phn", "rb")))
return 0;
else{
sz=filelength(fileno(fp));
sz=sz/sizeof(struct strc);
fclose(fp);
return sz;
}
}
int readentry(register int id,register int display)
{
FILE* fp;
if(!(fp=fopen("list.phn", "rb")))
return 0;
else{
if(id>1) fseek(fp, --id*sizeof(struct strc), SEEK_SET);
fread(&info, sizeof(struct strc), 1, fp);
if(display)
{
puts("+---+----------------------+---------------------------+----------------------+");
printf("|%-3d| %-20s| %-25s| %-20s|\n", info.id,info.name,info.address,info.phone);
}
fclose(fp);
return id;
}
}
void delentry(register int id)
{
FILE* tmpfp;
register int sz=countentry(), i, j;
char in;
puts("are you sure you want to delete?");
in=toupper(in=getchar());
fflush(stdin);
if(in=='N')
{
puts("Entry wasn't removed");
return;
}
puts("YES");
tmpfp=tmpfile();
for(i=1,j=1; i<=sz;i++)
{
readentry(i,0);
if(i!=id)
{
info.id=j;
fwrite(&info, sizeof(struct strc), 1, tmpfp);
j++;
}
}
rewind(tmpfp);
remove("list.phn");
for(id=1;id<j;id++)
{
fread(&info, sizeof(struct strc), 1, tmpfp);
putentry(id);
}
puts("Entry deleted successfully");
_rmtmp();
}
void searchentry()
{
register char ch;
char key[25], *result, search_str[100];
register int i, id, sz=countentry();
puts("Do you like to search by NAME:(N),or ADDRESS:(A),or PHONE#(P)?");
do{
ch=toupper(ch=getchar());
fflush(stdin);
}while(ch!='N' && ch!='A' && ch!='P');
puts("Enter your search:");
strtok(fgets(search_str, 100, stdin),"\n");
fflush(stdin);
for(id=0, i=0; id<=sz; id++)
{
readentry(id,0);
switch (ch)
{
case 'N': strcpy(key,info.name); break;
case 'A': strcpy(key,info.address); break;
case 'P': strcpy(key,info.phone); break;
default :;
}
result=strstr(strlwr(key), strlwr(search_str));
if(result)
{
if(i==0)
{
printf("The Entries containing \"%s\" are :\n", search_str);
puts("+---+----------------------+---------------------------+----------------------+");
printf("|ID | %-20s| %-25s| %-20s|\n", " NAME"," ADDRESS"," PHONE #");
}
readentry(id,1);
i++;
}
}
if(i)
{
puts("+---+----------------------+---------------------------+----------------------+");
printf("Found %d Result\n", i);
}else
puts("No Results found");
}