/*	Al Azhar University
	Faculty of Enginering
	Computer & Systems Department
	
	Eng \ Ahmed Samieh

	Index Useing TC++
	1 - 7 - 2003
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define max 100
typedef struct
{
 int id;
 char name[30];
 char phon[15];
}tell;
tell rec[max]={0};
int n=0; //number of recordes have been entered
int i,j;
void addrec();
void updaterec();
void delrec();
void sortidrec();
void sortnmrec();
void listrec();
void openfile();
void searchrec();
void savefile();
void main()
{
 char chose;
 int true=1;  // being in program
 while (true ==1)
 {
 textcolor(26);
  clrscr();
  puts("                               \" Phone Index\"");
  puts("                               **************");
  puts("\n Press   A   To Add New Record");
  puts(" Press   D   To Delete Recorde");
  puts(" Press   U   To Update Recorde");
  puts(" press   I   To Sort By Id Tabel");
  puts(" press   N   To Sort By Name Tabel");
  puts(" Press   F   To Find Name");
  puts(" Press   L   To List Tabel");
  puts(" Press   O   To Open Tabel From File");
  puts(" press   S   To Save Tabel In File");
  puts(" Press  Esc  To Exit Program ");
  gotoxy(52,4);textcolor(244);cprintf("Eng \\ Ahmed Samieh.");textcolor(26);
  gotoxy(80,25);
ask:chose = getch();
  switch (chose)
  {
   case 'a':
   case 'A': addrec(); break;
   case '\x1b': clrscr();
   gotoxy(30,12);
   textcolor(255);
   cprintf("Eng \\ Ahmed Samieh.");
   textcolor(7);
   gotoxy(80,25);
   getch();
   return;
   case 'd':
   case 'D': delrec(); break;
   case 'l':
   case 'L': listrec();break;
   case 'f':
   case 'F': searchrec();break;
   case 'O':
   case 'o': openfile();break;
   case 'i':
   case 'I': sortidrec();break;
   case 'S':
   case 's': savefile();break;
   case 'n':
   case 'N': sortnmrec();break;
   case 'u':
   case 'U': updaterec();break;
   default: goto ask;
  } //switch
 } //while
} //main
void addrec()
{
char idin[10];
 if (n>=max) return;  //chek for max size
 clrscr();
 puts("                               \" Appind Record \"");
 puts("                               *****************");
 printf("\n %d)\n ===",n+1);
 printf("\n Enter ID : ");
 gets(idin);
 rec[n].id=atof(idin);
 printf(" Enter Name :");
 gets(rec[n].name);
 printf(" Enter Phon N. :");
 gets(rec[n].phon);
 n++;
}
void delrec()
{
 if(n==0) return; //chek for mine size
 long k;
 char a,idin[10];
 clrscr();
  puts("                               \" Delete Record \"");
  puts("                               *****************");
  printf(" Enter ID :");
 gets(idin);
 k=atof(idin);
 for (i=0;i<n;i++)
 {
  if (rec[i].id==k)
  {
  printf("\n\n %d",rec[i].id);
  printf("\t %s",rec[i].name);
  printf("\t %s",rec[i].phon);
  textcolor(255);
  printf("\n\n ");
  cprintf("WARNING ");
   textcolor(7);
   printf("The Record Will Be DELETED Are you OK ?(y/n)\n\a");
   a=getch();
   switch (a)
   {
    case 'y':
    case 'Y':
    for (j=i;j<n-1;j++)
    rec[j]=rec[j+1];
    printf("\n\n The Record Was DELETED");
    n--;
    gotoxy(80,25);
    getch();
    return;
    case 'n':
    case 'N': return;
    default: a=getch();
   }//switch
  }  //if
 } //for
printf("\n ID IS NOT In THE TABEL");gotoxy(80,25); getch(); //case the ID you type not in tabel
}//function
void listrec()
{
if (n==0) return;
 clrscr();
  puts("                               \" The List \"");
  puts("                               ************");
 for (i=0;i<n;i++)
 {
  gotoxy(2,4+i);
  printf("%d",rec[i].id);
  gotoxy(16,4+i);
  printf("%s",rec[i].name);
  gotoxy(52,4+i);
  printf("%s",rec[i].phon);
 }
 gotoxy(80,25);
 getch();
}
void sortidrec()
{
 tell sw;
 int adress;
 long x = 0,min ;
 for (i=0;i<n;i++)
 if (rec[i].id>=x) x=rec[i].id;
 for (i=0;i<n;i++)
 {
  min=x;
  for(j=i;j<n;j++)
  {
   if (rec[j].id<=min)
   {
    adress=j;
    min=rec[j].id;
   }
  }
  sw=rec[i];
  rec[i]=rec[adress];
  rec[adress]=sw;
 }
}
void updaterec()
{
 if (n==0) return;
 clrscr();
 long k;  //the Id Will You Enter to Update
 char idin[10];
  puts("                               \" Update Record \"");
  puts("                               *****************");
 printf(" Enter The ID :");
 gets(idin);
 k=atof(idin);
 for (i=0;i<n;i++)
 {
  if (rec[i].id==k)
  {
   printf("\n %d",rec[i].id);
   printf("\t %s",rec[i].name);
   printf("\t %s",rec[i].phon);
   printf("\n Enter The New ID :");
   gets(idin);
   rec[i].id=atof(idin);
   printf("\n Enter The New Name :");
   gets(rec[i].name);
   printf("\n Enter The New Phon N. :");
   gets(rec[i].phon);
   return;
  }
 }
printf("\n The ID Was Not Found");
gotoxy(80,25);
getch();
}
void searchrec()
{
if (n==0) return;
clrscr();
char namein[30],thename[30];
int j=1,x;
 puts("                                \" Find Name \"");
 puts("                                *************");
 printf(" Enter Name :");
 gets(namein);
 x=strlen(namein);
 for (i=0;i<n;i++)
 {
 strncpy(thename,rec[i].name,x);
 thename[x]='\0';
 if ((strcmp(namein,thename)==0))
 {
 j=0;
 printf("\n %d",rec[i].id);
 printf("\t %s",rec[i].name);
 printf("\t %s",rec[i].phon);
}
}
if (j==1) printf("\n The Name Was Not Found");
gotoxy(80,25);
getch();
}
void sortnmrec()
{
if (n==0)return;
 tell sw;
 int adress;
 char ss[30] = "";
 char min[30];
 for (i=0;i<n;i++)
 if ((strcmp(rec[i].name,ss))>=0) strcpy(ss,rec[i].name);
 for (i=0;i<n;i++)
 {
  strcpy(min,ss);
  for(j=i;j<n;j++)
  {
   if ((strcmp(rec[j].name,min))<=0)
   {
    adress=j;
    strcpy(min,rec[j].name);
   }
  }
  sw=rec[i];
  rec[i]=rec[adress];
  rec[adress]=sw;
 }
}
void savefile()
{
if (n==0)return;
clrscr();
FILE *target;      // pointer to The File
char fname[20]="";
char file[8];
 puts("                               \" Save As File  \"");
 puts("                               *****************");
printf("\n Enter The Name Of The File : ");
gets(file);
strcat(fname,file);   //to add the string file to string fname
strcat(fname,".sam"); // to add the string ".sam" to string fname
target = fopen(fname,"wb"); //to open The File
fwrite(rec,sizeof(rec[0]),n,target); // to save The Record In His Line
fclose(target);
}
void openfile()
{
clrscr();
j=1;
FILE *target;      // pointer to The File
char fname[20]="";

char file[8];
puts("                               \" Open File  \"");
puts("                               *****************");
printf("\n Enter The Name Of The File : ");
gets(file);
strcat(fname,file);   //to add the string file to string fname
strcat(fname,".sam"); // to add the string ".sam" to string fname
if ((target = fopen(fname,"rb"))==NULL) //to open The File
{
printf("\n File Not Found");
gotoxy(80,25);
getch();
return;
}
n=0;
while (fread(&rec[n],sizeof(rec[n]),1,target)==1) n++;
fclose(target);
}