首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

!分词程序的最后一个bug,实在是找不出来了,要不小弟我也不来这里

2012-02-17 
求助!!!分词程序的最后一个bug,实在是找不出来了,要不我也不来这里。简单的中文分词程序,可以运行,现在还只

求助!!!分词程序的最后一个bug,实在是找不出来了,要不我也不来这里。
简单的中文分词程序,可以运行,现在还只能分单句文本,
问题是:一旦句子过长,就会错误退出。
我找了半天实在是找不出错误,大家帮忙啊
小弟在这里多谢了!
哈希表的建立应该问题不大


#include <stdio.h>
#include <malloc.h>
#include <string.h>  


//#ifndef   _HashSep_H_

#define   MinTableSize   128
typedef   char   *ElementType;     //自定义Key数据类型  
struct   ListNode;         //节点的结构体  
typedef   struct   ListNode   *Position;     //指向节点的指针  
typedef   Position   List;     //指向头节点的指针  
struct   HashTbl;         //哈希表的结构体  
typedef   struct   HashTbl   *HashTable;

HashTable   InitializeTable(int   TableSize);       //基本函数声明  
void   DestroyTable(HashTable   H);
Position   Find(ElementType   Key,HashTable   H);
void   Insert(ElementType   Key,HashTable   H);
ElementType   Retrieve(Position   P);

//#endif


////////////////////////////////////////////具体的结构体定义  
struct   ListNode{        
        ElementType   *Element;
        Position   Next;
        };

struct   HashTbl{
        int   TableSize;
        List   *TheLists;
        };


////////////////////////////////////////////////////////函数定义  
char   *MyDel(char   *p,int   n){           //清除字符串P的前n个字符,返回字符串  
        char   q[64];  
        strcpy(q,p+n);
        strset(p, '\0 ');
        strcpy(p,q);
        strset(q, '\0 ');
        return(p);  
        }

Error(){
        exit(1);
        getchar();
        }

FatalError(){
        exit(1);
        getchar();
        }

int   Prime(int   i){               /*判断是否素数*/
        int   j;
       
        for(j=2;j <i;j++){
                if(i%j==0){
                        //printf( "no ");
                        return(0);
                        }
                }
        return(1);
        }

int   NextPrime(int   i){                       /*求大于i得最小素数*/
        i++;
        while(1){
                if(Prime(i)==1   )return(i);
                i++;
                }
        }



int   IndexHash2(unsigned   char   *Key,int   TableSize){
        unsigned   int   HashVal=0;
        while(*Key!= '\0 ')
                HashVal=(HashVal < <5)+*Key++;
        return   HashVal%TableSize;
        }

int   IndexHash(unsigned   char   *Key,int   TableSize){
        unsigned   int   HashVal;
        HashVal=(*Key)*1000+*(Key+1);        
        return   HashVal%TableSize;
}

HashTable   InitializeTable(int   TableSize){
        HashTable   H;
        int   i;
       
        if(TableSize <MinTableSize){
                Error( "TableSize   is   too   small ");
                return   NULL;
                }        
        H=malloc(   sizeof(   struct   HashTbl)   );
        if(   H==NULL   )
                FatalError( "out   of   space!!! ");
       
        H-> TableSize=NextPrime(TableSize);         //求大于TableSize的最小素数
        H-> TheLists=malloc(   sizeof(List)*H-> TableSize   );
        if(H-> TheLists==NULL)
                FatalError( "TheLists ");
        for(i=0;i <H-> TableSize;i++){
                H-> TheLists[i]=malloc(   sizeof(struct   ListNode)   );
               
                if(H-> TheLists[i]==NULL)
                               
                                FatalError( "fuck ");
                                   
                else
                                H-> TheLists[i]-> Next=NULL;                                
                }
                return   H;
}

Position   Find(ElementType   Key,HashTable   H){
        Position   P;
        List   L;         //这里List和Position好像没什么区别  
        L=H-> TheLists[IndexHash(Key,H-> TableSize)];
        P=L-> Next;
        while(P!=NULL   &&   strcmp(P-> Element,Key)!=0   )
                P=P-> Next;


        return   P;
}

void   Insert(ElementType   Key,HashTable   H){
        Position   Pos,NewCell;
        List   L;         //L是头节点  
        Pos=Find(Key,H);
        if(Pos==NULL   ){
                NewCell=malloc(sizeof(struct   ListNode)   );
                if(NewCell==NULL)
                                FatalError( "为新节点分配空间失败 ");
                else{
                        L=H-> TheLists[IndexHash(   Key,H-> TableSize   )   ];
                        NewCell-> Next=L-> Next;
                        L-> Next=NewCell;
                        NewCell-> Element=Key;
                        }
                }
               
}


////////////////////////////////////////////main()函数  
main(){

int   a=0,b=0,i,j,k,IfFind,m;
unsigned   char   Buffer[32],*p,*q,*NoFind,*Temp_Sent,*BiaoDian,Temp_C;
HashTable   H;
FILE   *Dictionary,*Doc;
List   Tmp;


Dictionary=fopen( "dictionary.txt ", "r ");//打开字典文件  
Doc=fopen( "doc.txt ", "r ");

H=InitializeTable(65536);//初始化哈希表  
printf( "哈希初始化完毕\n\n\n ");


while(!feof(Dictionary)   ){        
        p=malloc(sizeof(Buffer)   );
        p=fgets(p,32,Dictionary);
        p[(strlen(p)-1)]= '\0 ';         //去掉回车  
        Insert(p,H);
        }  
printf( "哈希表建立完毕\n\n\n\n ");
printf( "begin\n ");

///////////////////////////////////////////////////开始匹配  
Temp_Sent=malloc(sizeof(char)*1024);
strset(Temp_Sent, '\0 ');  
strcat(Temp_Sent, "测试软件轻松实现从现有的电脑里件轻松实 ");   //写进测试文本
NoFind=malloc(sizeof(char)*64);
strset(NoFind, '\0 ');


while(*Temp_Sent!= '\0 '){             //检验是否匹配到  
        i=0;j=0;
        IfFind=0;
        Tmp=H-> TheLists[IndexHash(Temp_Sent,H-> TableSize)];
        Tmp=Tmp-> Next;
        if(*NoFind){         //显示上次没匹配到的内容  
                        printf( "%s\n ",NoFind);
                        strset(NoFind, '\0 ');
                        }
        while(   Tmp!=NULL   ){
                if(   strstr(Temp_Sent,Tmp-> Element)==Temp_Sent   ){


                        IfFind=1;                             //IfFind是是否匹配到的标记  
                        break;  
                        }
                Tmp=Tmp-> Next;  
               
                }
               
        if(IfFind==1){                 //如果匹配到,  
                         
                printf( "%s\n ",Tmp-> Element);                
                MyDel(Temp_Sent,strlen(Tmp-> Element)   );
                continue;  
                }  
               
        if(IfFind==0){                                 //如果没匹配到,判断当前字是否汉字,当前字入串  
                if(Temp_Sent[0] <128){
                        strncat(NoFind,Temp_Sent,1);
                        MyDel(Temp_Sent,1);
                        continue;
                        }
                if(Temp_Sent[0]> =128){
                        strncat(NoFind,Temp_Sent,2);
                        MyDel(Temp_Sent,2);
                        continue;
                        }
                }    
        }  
if(!*Temp_Sent&&*NoFind){                           //句子结束,显示nofind里的残余  
                printf( "%s\n ",NoFind);  
                }


printf( "done ");
       
getchar();

}

[解决办法]
p=malloc(sizeof(Buffer) );
p=fgets(p,32,Dictionary);
p[(strlen(p)-1)]= '\0 '; //去掉回车

你这几句不是表明了你的单个字符串只能是32,而你的table定义是128,最大就是32*128
[解决办法]
int main()
{
ifstream infile;
string filename;
cout < < "Please enter the file name: ";
cin > > filename;



infile.open(filename.c_str());
string line;
getline(infile, line, '\n ');
infile.close();

vector <string> wordsOfLine;
string::size_type pos = 0, prev_pos =0;
string word;
while ((pos = line.find_first_of( ' ', pos)) != string::npos)
{
word = line.substr(prev_pos, pos - prev_pos);
prev_pos = ++pos;
wordsOfLine.push_back(word);
}
wordsOfLine.push_back(line.substr(prev_pos, pos - prev_pos));

size_t numOfLine = wordsOfLine.size();
cout < < numOfLine < < "words " < < endl;
}

热点排行