// xujianfeng.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "memory.h"
typedef struct word
{char cnword[18];
char enword[128];
struct word *next;
}*Ptrword,word;


FILE *fp;
char filename[20];
Ptrword New;

void Open_word();
void Create_word(word **T);
void Search_word(Ptrword T);
void Scan_word(Ptrword T);
void Add_word(Ptrword T);
void Modify_word(Ptrword T);
void Delete_word(Ptrword T);
void Output_word(Ptrword T);
void free_word(Ptrword T);
void my_strcpy(char *s, char *t);
void New_word(Ptrword T);


int main(int argc, char* argv[])

{
Ptrword T;
word a;
int i,t=1;
T = &a;
while(1)
{
   if( t == 1)
   {
    printf("\n\n\t----------------操作界面-----------------\n\n");
    printf("\n\t打开英语词典,请按1\n");
    printf("\t创建链表文件,请按2\n");
    printf("\t英语词典查询,请按3\n");
    printf("\t英语词典浏览,请按4\n");
    printf("\t英语词典添加,请按5\n");
    printf("\t修改单词释义,请按6\n");
    printf("\t删词某个单词,请按7\n");
    printf("\t退出,请按8");
    t = 0;
   }
   printf("\n请输入你所要进行的操作:");
   scanf("%d",&i);
   switch(i)
   {
   case 1:Open_word();
    break;
   case 2:Create_word(&T);
    break;
   case 3:Search_word(T);
    break;
   case 4:Scan_word(T);
    break;
   case 5:Add_word(T);
    break;
   case 6:Modify_word(T);
    break;
   case 7:Delete_word(T);
    break;
   case 8:printf("欢迎您下次继续使用!\n");
    Output_word(T);
    free_word(T);
    return 0;
   default:printf("输入错误!");
    printf("请选择是否查看操作界面,是请按1:");
    scanf("%d",&t);
    break;
   }
}
return 0;
}



void Open_word()
{
if((fp = fopen("English-Chinese.txt", "r+")) ==NULL)
{
   printf("\n\n不能建立词库!");
   exit(0);
}
else
   printf("文件已打开!");
fclose(fp);
}



void Create_word(word **T)
{
assert(*T!=NULL);
FILE *fp_word;
Ptrword p,q;
int i = 0;
*T = NULL;
if((fp_word = fopen("English-Chinese.txt", "r+")) ==NULL)
{
   printf("\n\n不能打开词库!");
   exit(0);
}
while(!feof(fp_word))
{
   p = (Ptrword)malloc(sizeof(word));
   fgets(p->cnword,18,fp_word);
   for(i = 0;p->cnword[i] != '\n';i++)
    ;
   p->cnword[i] = '\0';
   fgets(p->enword,128,fp_word);
   if(*T==NULL)
    *T = p;
   else
    q->next = p;
   q = p;
}
if(*T != NULL)
   q->next = NULL;
printf("词库链表建立完毕!\n");
fclose(fp_word);
}



void Search_word(Ptrword T)
{
assert(T!=NULL);
Ptrword p = T;
char s[20];
int i ;
printf("输入你要查询的单词:");
scanf("%s",s);
while( p && ((i = strcmp(p->cnword , s)) != 0))
{
   //printf("%s %d",p->cnword, i);
   p = p->next;
}
if(p != NULL)
   printf("存在!该单词的释义:%s",p->enword);
else
   printf("该单词不存在!");
}



void Scan_word(Ptrword T)
{
assert(T != NULL);
char ch,c;
Ptrword p = T, q;
while(1)
{
   printf("请输入您要浏览的单词的首字母:");
   getchar();
   scanf("%c",&ch);
   while( p && (p->cnword[0] != ch))
    q = p,p = p->next;
   if(!p)
    printf("没有以该字母为首字母的单词\n");
   else
   {

    printf("\n-----------浏览操作-------------\n");
    printf("\t向上浏览,请按a\n");
    printf("\t向下浏览,请按l\n");
    printf("\t退出浏览,请按x\n");
    while(1)
    {
     printf("当前单词%s\t%s",p->cnword,p->enword);
     printf("请输入操作命令:");
     getchar();
     scanf("%c",&c);
     switch(c){
     case 'a': 
      if(p == T)
      {
       printf("这是第一个单词,不能向上了!\n");
       break;
      }
      else
      {
       p = q;
       printf("\n%s\t%s",p->cnword, p->enword);
       New_word(p);
       break;
      }
     case 'l':
      if(p->next == NULL)
      {
       printf("这是最后一个单词,不能向下了!\n");
       break;
      }else
      {
       p = p->next;
       q = p;
       printf("\n%s\t%s",p->cnword, p->enword);
       New_word(p);
       break;
      }
     case 'x':printf("\n已退出。。。。\n");
      Output_word(New);
      free_word(New);
      break;
     default:printf("\n输入错误!");
      break;
     }
    }
   }
}
}



void New_word(Ptrword T)
{
assert(T != NULL);
Ptrword p,q = New;
int i;
printf("该单词是不是生词(是请按1), 是不是已熟记该单词(是请按2),否则继续浏览:");
getchar();
scanf("%d",&i);
if(i == 1)
{
   p = (Ptrword)malloc(sizeof(word));
   my_strcpy(p->cnword ,T->cnword);
   my_strcpy(p->enword, T->enword);
   if(New == NULL)
    New = p;
   else
   {
    while(q->next)
     q = q->next;
    q->next = p;
   }
   p->next = NULL;
}
else if(i == 2)
{
   while(q)
   {
    while(q && ((strcmp(T->cnword ,q->cnword)) != 0))
     p = q,q = q->next;
    if(q)
    {
     printf("改单词在生词库中,是否要删除(是请按1):");
     scanf("%d",&i);
     if(i == 1)
     {
      if(q == New)
      { 
       if(q->next != NULL)
       {
        New = New->next;
        free(q);
        break;
       }
       else
       {
        free(q),New = NULL;
        break;
       }
      }
      else if(q->next == NULL)
      {
       p->next = NULL,
        free(q);
       break;
      }
     }
     else
      break;
    }
    else
     break;
   }

}
else
   printf("请继续浏览!\n");
}



void my_strcpy(char *s, char *t)
{
while(*s++ = *t++)
   ;
*s = '\0';
}



void Add_word(Ptrword T)
{
assert(T != NULL);
Ptrword p = T,q,t;
int i;
char s[20],b[128];
printf("请输入您要添加的新单词:");
getchar();
gets(s);
printf("请输入您要添加的新单词的释义:");
scanf("%s",b);
while(p && ((i = strcmp(p->cnword, s)) < 0))
   t = p,p = p->next;
if(i == 0)
   printf("此单词已存在,无需添加!");
else if( i > 0 )
{
   q = (Ptrword)malloc(sizeof(word));
   my_strcpy(q->cnword , s);
   my_strcpy(q->enword , b);
   q->enword[strlen(q->enword)+1] = '\0';
   q->enword[strlen(q->enword)] = '\n';
   t->next = q;
   q->next = p;
}
else 
{
   q = (Ptrword)malloc(sizeof(word));
   my_strcpy(q->cnword , s);
   my_strcpy(q->enword , b);
   t->next = q;
   q->next = NULL;
}
}



void Modify_word(Ptrword T)
{
assert(T != NULL);
Ptrword p = T;
int i= 1;
char s[20]; 
while(i==1)
{
   printf("输入要修改的单词:");
   getchar();
   scanf("%s",s);
   i = 0;
   while(p && (strcmp(p->cnword, s) != 0))
    p = p->next;
   if(p)
   {
    memset(p->enword, 128 ,0);
    printf("\n输入新的释义:");
    getchar();
    scanf("%s",p->enword);
    p->enword[strlen(p->enword)+1] = '\0';
    p->enword[strlen(p->enword)] = '\n';
   }
   else
   {
    printf("没有此单词!是否从新输入(是请按1,否请按0)");
    getchar();
    scanf("%d",&i);
   }
}
}



void Delete_word(Ptrword T)
{
assert(T != NULL);
Ptrword p = T,q;
char s[20];
int i = 1;
while(i == 1)
{
   printf("请输入要删除的单词:");
   getchar();
   scanf("%s", s);
   while(p && (strcmp(p->cnword, s) != 0))
    q = p,p = p->next;
   if(p)
   {
    q->next = p->next;
    free(p);
    printf("%s:删除成功!\n",s);
    printf("是否继续(是请按1,否请按0)");
    scanf("%d",&i);
   }
   else
   {
    printf("没有此单词!是否从新输入(是请按1,否请按0)");
    getchar();
    scanf("%d",&i);
   }
}
}



void Output_word(Ptrword T)
{
assert(T != NULL);
Ptrword p = T;
printf("请输入词库名:");
getchar();
scanf("%s",filename);
if((fp = fopen(filename, "w")) ==NULL)
{
   printf("\n\n不能建立词库!");
   exit(0);
}
while(p)
{
   fputs(p->cnword, fp);
   fputc('\n',fp);
   fputs(p->enword, fp);
   fputc('\n', fp);
   p = p->next;
}
fclose(fp);
}



void free_word(Ptrword T)
{
assert(T != NULL);
Ptrword p , q;
for(p = T; p; )
{
   q = p;
   p = p->next;
   free(q);
}
}