user-0 提问: linux 下自编简易命令行字典

回复:
user: user-1
content: 厉害！学以致用！

user: user-2
content: 英汉汉英字典C源代码 支持模糊查询，支持生成生词本。 http://download.csdn.net/detail/zhao4zhong1/4140015

user: user-0
content: 上面是dict.h因为一次发不完，我就分开了 接下来的是源文件dict.c #include"dict.h" int main(int argc,char *argv[]) {    //only one parameter    if(argc==1)    {             lookup(argv,argc);    }    else if(argc==2&&*argv[1]=='-')   //two parameter wtih the second one startt with '-'    {          if(!strcmp(argv[1],"-A"))         {             if(addword(0)) 	         printf("word add successfully!\n");         }         else if(!strcmp(argv[1],"-c"))         {              addword(2);         }         else if(!strcmp(argv[1],"--help"))         {              loadhelp();         }         else if(!strcmp(argv[1],"-s"))         {               printf("you must add at lest a word behind -s.\n");         }         else if(!strcmp(argv[1],"-a"))         {               printf("you must add at lest a word behind -a.\n");         }         else          {               printf("Parameter %s is not legle!for more  help please type\"dict --help\"\n",argv[1]); 	}    }    else     {         lookup(argv,argc);    }    return 0; } 帮助文件help #about the dict   version 1.0   	-s       Briefly display the meaning of the words you look up 	-a       Dispaly the meaning of the words you look up in detail 	-c       copy words and add them in the dictionary.the format is                   "word phonetic meaning" or "word  meaning"and every word                  should be estinguished by [Enter]         -A       add one word in the dictionary with directions         --help   statement of parameters. 源文件CreateDict.c  (我觉得可要可不要) #include"dicthelp.h" int main() {        if(CreateDict())     {        printf("successfully create file.\n");        return 0;     }     printf("error:failed to create file DictList or Meaning!\n");     return 1; }

user: user-0
content: #include"dicthelp.h" #include<stdlib.h> //add word to the dict //successfully excuted return the num of added words. //failed return 0 int addword(int mode) {    FILE *fp;    int   i;    List  list;    DictWord dw;    if(mode==0)    {        printf("please following the direction to add word\n");        printf("Word:");      scanf("%s",dw.word);         if(Travelsal(dw.word))         {             printf("Word %s is already in the dictionary.\n",dw.word);             return 0;        }        printf("Phonetic:");  scanf("%s",dw.phonetic);        printf("Meaning:");   scanf("%s",dw.meaning);        dw.node=count()+1;        printf("Node:%d\n",dw.node);        printf("dw.word=%s,dw.ph=%s,dw.meaning=%s\n",dw.word,dw.phonetic,dw.meaning);        for(i=0;i<36;i++)            list.word[i]=dw.word[i];        list.node=dw.node;        fp=fopen("DictList","ab");        if(fp==NULL) 	  return 0;        fwrite(&list,sizeof(list),1,fp);        fclose(fp);         fp=fopen("Meaning","ab");        if(fp==NULL)            return 0;        fwrite(&dw,sizeof(dw),1,fp);        fclose(fp);        return 1;    }    if(mode>1&&mode<4)    //copy mode    {           char *buffer,ch[BUFFER_SIZE];         int  size=10,test=0,point=0,p=0,add_num=0,added_num=0;        i=0;        printf("please copy the words you want to add,each group estinguished by [Enter]\n");        if(mode==2)             buffer=(char *)malloc((sizeof(DictWord)+1)*size);        else if(mode==3)        {     	    scanf("%d",&size);             buffer=(char *)malloc((sizeof(DictWord)+1)*size);        }        while(1)        {             *(buffer+i)=getchar();             if(*(buffer+i)=='\n')                  add_num++;             if(i>3)             {                 if(*(buffer+i-4)=='#'&&*(buffer+i-3)=='E'&&*(buffer+i-2)=='N'&&*(buffer+i-1)=='D'&&*(buffer+i)=='\n')                 {                      *(buffer+i-4)='\0';break;                 }             }             i++;        }        add_num--;        while(*(buffer+p)!='\0')        {              	  while(*(buffer+p)!='\0')                     // to fill the structrue!           {                 i=0;                 if(point==1)                      break;                 while(*(buffer+p)==' ')                //delete the space in the begginning                      p++;                 while(*(buffer+p)!='\n')                                {                        if(test==0)                                        {                         dw.word[i]=*(buffer+p);p++;                         if(*(buffer+p)==' '||*(buffer+p)=='\n')          //there is a space                         {                              dw.word[i+1]='\0';                              if(!Travelsal(dw.word))                    //word is not in the dict                              {                                  while(*(buffer+p)==' ')                                      p++;                                  if(*(buffer+p)!='\n')                                      test++;                                  else                                  {                                      p++;                                      printf("Word %s is in wrong format.for more help with \" --help\"\n",dw.word);                                   }                                  break;                              }                                        //word id already in the dict                              else                              {                                     printf("Word %s is already in the dictionnary.\n",dw.word);                                   while(*(buffer+p)!='\n')                                      p++;                                  p++;                                  break;                              }                         }                     }                     else if(test==1)                     {                         ch[i]=*(buffer+p);p++;                         if(*(buffer+p)==' '||*(buffer+p)=='\n')                         {                               ch[i+1]='\0';                               while(*(buffer+p)==' ')                                  p++;                             if(*(buffer+p)!='\n')                             {                                 for(i=0;i<strlen(ch);i++)                                      dw.phonetic[i]=ch[i];                                 dw.phonetic[i]='\0';                                 test++;                             }                             else                              {                                 ch[i+1]='\0';                                 dw.phonetic[0]='N';                                 dw.phonetic[1]='U';                                 dw.phonetic[2]='L';                                 dw.phonetic[3]='L';                                 dw.phonetic[4]='\0';                                 for(i=0;i<strlen(ch);i++)                                     dw.meaning[i]=ch[i];                                 dw.meaning[i]='\0';                                 test=0;                                 p++;                                 point=1;                             }                             break;                         }                      }                      else if(test==2)                      {                             dw.meaning[i]=*(buffer+p); p++;                             if(*(buffer+p)=='\n'||*(buffer+p)==' ')                             {                                  dw.meaning[i+1]='\0';                                while(*(buffer+p)==' ')                                     p++;                                if(*(buffer+p)!='\n')         //more than 3 strings                                {                                     printf("Word %s is in wrong format.for more help with \" --help\"\n",dw.word);                                    while(*(buffer+p)!='\n')                                         p++;                                   p++;                                   test=0;                                }                                else                                {                                   p++;                                   test=0;                                   point=1;                                }                                break;                            }                      }                      i++;                             }            }            if(point==1)            {               dw.node=count()+1;               list.node=dw.node;               for(i=0;i<strlen(dw.word);i++)                   list.word[i]=dw.word[i];               list.word[i]='\0';               fp=fopen("DictList","ab");               if(fp==NULL) 	          return 0;               fwrite(&list,sizeof(list),1,fp);               fclose(fp);                fp=fopen("Meaning","ab");               if(fp==NULL)                  return 0;               fwrite(&dw,sizeof(dw),1,fp);               fclose(fp);               point =0;               added_num++;               printf("Word %s is added in the dict successfully.\n",dw.word);           }       }       free(buffer);       printf("%d total	%d added	%d failed.\n",add_num,added_num,add_num-added_num);        return 1;    }    return 0;    }  //cat word //paramter argc=2 hand up to main to judge if - is exist int lookup(char *argv[],int argc) {    FILE *fp;    List list;    DictWord dw;      if(argc==1)          //list all the words in the Dictlist    {         int total=0;       fp=fopen("DictList","rb");       if(fp==NULL)           return -1;       while(!feof(fp))        {                        if(fread(&list,sizeof(list),1,fp)==1)          {                    printf("%s\n",list.word);               total++;          }        }        fclose(fp);        printf("%d Words total in the dictionary.\n",total);        return 1;           }    else   //the num of command is bigger than  3 or    {              int  test=0,i=1,m=1,num=0;           if(*argv[1]=='-')    //the word to cat start from the third one.        {            i=2; test=1;            if(!strcmp(argv[1],"-a"))                 m=2;        }        for(;i<argc;i++)     //this way cat one word need one file io, it can be esayer.        {             fp=fopen("DictList","rb");           if(fp==NULL)               return -1;           while(!feof(fp))           {               if(fread(&list,sizeof(list),1,fp)==1)                  if(strcmp(list.word,argv[i])==0)                  {      	             fclose(fp);                      fp= fopen("Meaning","rb");                      if(fp==NULL)                           return -1;                      while(!feof(fp))                      {                         if(fread(&dw,sizeof(dw),1,fp)==1)                            if(dw.node==list.node)                            {                                 if(m==1)   //list simply                                     printf("%s      %s\n",dw.word,dw.meaning);                               else     // list all                                     printf("%s      %s      %s\n",dw.word,dw.phonetic,dw.meaning);                               num++;                               test++;                               break;                            }                      }                      fclose(fp);   //close Meaning file  test==i                      break;                      }               }           if(test<i)           {    fclose(fp);                test++;                printf("Word %s is not found in the dictionary.\n",argv[i]);            }       }       return num;    }    return -1; }  //to delete a word in the dict int deleteword() {     return 1; }  //to update a word in the dict int update() {         return 0; }