تبليغاتX
خانه ای اینترنتی برای برنامه نویسان
خانه | آرشیو | پست الکترونیک
سورس کامل برنامه دیکشنری به زبان ++c

#include
#include
#include
#include
#include
struct dictionary {
   char english[30] ;
   char farsi[30] ;
   struct dictionary *next ;
   struct dictionary *prior ;
} list_entry ;

struct dictionary *start ;
struct dictionary *last ;

void enter() , display() , search() ;
void save()  , load()    , list() , del();
void display(struct dictionary *info, int *row);

struct dictionary *find(char *);
int menu_select();
struct dictionary *store(struct dictionary *, struct dictionary *);

int main ()
{
   start = last = NULL ;
   for(;;) {
   switch(menu_select()) {
    case 1:  enter();  break ;
    case 2 : del();    break ;
    case 3:  list() ;  break ;
    case 4:  search(); break ;
    case 5:  save();   break ;
    case 6:  load();   break ;
    case 7:  exit(0) ;
   }
   }
}
//****************
int menu_select()
{
  char s[5];
  clrscr() ;
  gotoxy(25, 4) ;
  printf("1. add a word ");
  gotoxy(25, 6) ;
  printf("2. delete a word  ") ;
  gotoxy(25, 8) ;
  printf("3. list all files ") ;
  gotoxy(25, 10) ;
  printf("4. search a  word ") ;
  gotoxy(25, 12) ;
  printf("5. save the file  ") ;
  gotoxy(25, 14) ;
  printf("6. load the file  ") ;
  gotoxy(25, 16) ;
  printf("7. quit ") ;
  do {
   gotoxy(20, 18) ;
   printf("enter your select(1-7):");
   gets(s);
  } while (atoi(s) < 0 || atoi(s) > 7) ;
  return atoi(s) ;
}
//*********************
void enter ()
{
 struct dictionary *info ;
 int i ;
 char ch ;
 clrscr() ;
 gotoxy(3, 2) ;
 printf("   english        farsi     ");
 gotoxy(3, 3) ;
 printf(" ------------ -------- ");
 i = 4 ;
 for (;;) {
   info = (struct dictionary *)malloc(sizeof(list_entry)) ;
   if(!info) {
 printf("\n out of memory. press a key ") ;
 getch();
 return ;
   }
   gotoxy(3, i) ;
   gets(info -> english) ;
   if (!info -> english[0]) {
   gotoxy(15, i + 1) ;
   printf("press a key to continue");
   getch() ;
   break ;
   }
   gotoxy(18, i);
   gets(info -> farsi) ;
   i++ ;
   start = store(info, start) ;
   }
 }
//**************
struct dictionary *store(struct dictionary *i, struct dictionary *top)
{
 struct dictionary *old, *p ;
 if(last == NULL) {
 i -> next = NULL ;
 i -> prior = NULL ;
 start = i;
 last = i ;
 return i ;
 }
 p = top ;
 old = NULL ;
 while (p != NULL) {
   if(strcmp(p -> english, i -> english) < 0) {
   old = p ;
   p = p -> next ;
   }
   else {
    if (p -> prior) {
    p -> prior -> next=i ;
    i -> next=p ;
    i -> prior=p -> prior;
    p -> prior=i ;
    return top ;
    }
    i -> next = p ;
    i -> prior = NULL ;
    p -> prior = i ;
    return i ;
   }
 }
 old -> next = i ;
 i -> next = NULL ;
 i -> prior = old ;
 last = i ;
 return start ;
}
//******************
void del()
{
 struct dictionary *info;
 char english[80];
 gotoxy(20, 20) ;
 printf(" enter word for delete : ") ;
 gets(english) ;
 info = find(english) ;
 if(info == NULL) {
   gotoxy(10, 20) ;
   printf(" word not found! press a key ");
   getch() ;
 }
 if (info)
   if (start == info)
 {
   start = info -> next ;
   if(start)
    start -> prior = NULL ;
   else
    last = NULL ;
 }
   else  {
  info -> prior -> next = info -> next;
  if(info != last)
   info -> next -> prior = info -> prior;
  else
  last = info -> prior ;
   }
   free(info) ;
   gotoxy(10,20) ;
   printf("word deleted, press a key ");
   getch() ;
}
//*******************************
struct dictionary *find(char *english)
{
   struct dictionary *info ;
   info = start ;
   while(info != NULL) {
 if (strcmp(english, info -> english) == 0)
  return info;
 info = info -> next ;
   }
   return NULL ;
}
//*****************
void list ()
{
  struct dictionary *info ;
  int i ;
  info = start ;
  clrscr() ;
  gotoxy(3, 2) ;
  printf("   english       farsi ");
  gotoxy(3, 3) ;
  printf(" ------------ --------  -");
  i = 4 ;
  while(info != NULL) {
 display(info, &i) ;
 info = info -> next ;
  }
  gotoxy(15, i + 2) ;
  printf("press a key ");
  getch() ;
}
//*******************
void display(struct dictionary *info, int *row)
{
   gotoxy(3, *row) ;
   printf("%s", info -> english) ;
   gotoxy(18, *row) ;
   printf("%s", info -> farsi) ;
   *row = *row + 1 ;
}
//**************************
void search()
{
   char english[40];
   int i ;
   struct dictionary  *info;
   gotoxy(20, 20) ;
   printf(" enter word to find : ");
   gets(english) ;
   info  = find(english) ;
   if(info == NULL) {
   gotoxy(10, 20) ;
   printf(" word not found! press a key ");
   getch() ;
   }
    else  {
   clrscr() ;
   gotoxy(3, 2) ;
   printf("   english       farsi   ");
   gotoxy(3, 3) ;
   printf(" ------------ -------");
   i = 4 ;
   display(info ,&i) ;
   gotoxy(15, i + 2) ;
   printf("press a key ");
   getch() ;
   }
}
//*********************
void save()
{
  struct dictionary *info ;
  FILE *fp ;
  if((fp = fopen("d.dat","wb")) == NULL)  {
  printf("\n cannot open file. ") ;
  getch();
  exit(1) ;
  }
  gotoxy(20, 20) ;
  printf("<< saving file >>") ;
  info = start ;
  while(info) {
  fwrite(info, sizeof(struct dictionary), 1, fp);
  info = info -> next ;
  }
  fclose(fp) ;
  gotoxy(15, 22) ;
  printf("file saved press a key") ;
  getch() ;
}
//********************
void load ()
{
  struct dictionary *info , *temp = NULL;
  FILE *fp ;
  fp = fopen("d.dat","rb") ;
  if(fp == NULL)  {
 printf("\n cannot open file.");
 getch();
 exit(1) ;
  }
  while(start) {
  info = start -> next ;
  free(info) ;
  start = info ;
  }
  gotoxy(20,20) ;
  printf(" << loading file  >> ") ;
  start = NULL ;
  while (!feof(fp)) {
   info = (struct dictionary *)
  malloc(sizeof(struct dictionary)) ;
   if(1 != fread(info, sizeof(struct dictionary), 1, fp))
   break ;
   if(start == NULL)  {
   temp = start = info ;
   info -> prior = NULL ;
   info -> next = NULL ;
   }
   else  {
   info -> next = NULL ;
   temp -> next = info ;
   info -> prior = temp ;
   temp = info;
   }
  }
  last = temp ;
  fclose(fp) ;
  gotoxy(15,22) ;
  printf("file loaded press a key ") ;
  getch();
}


 

|+| نوشته شده توسط محمد رضا ابراهیمی در پنجشنبه هجدهم بهمن 1386 و ساعت 6:38 | 
Powered By Blogfa - Designing & Supporting Tools By WebGozar