s[len++]=ch[0];
tmp_index=find_lemma(s);
//the processor is much too time-consuming
if(tmp_index!=0){
//matching
index=tmp_index;
continue;
}
// tmp_index==0: no matching
insert_dictionary(s);
fwrite(&index,1,sizeof(unsigned short),fp_out);
fwrite(ch,1,sizeof(char),fp_out);
index=0;
len=0;
for(int k=0;k<30;k++)
s[k]='\0';
}
}
if(index){
//表示能匹配下去但输入缓冲中已无字符了
s[len-1]='\0';
index=find_lemma(s);
fwrite(&index,1,sizeof(unsigned short),fp_out);
fwrite(ch,1,sizeof(char),fp_out);
}
rewind(fp_out);
fwrite(&p,1,sizeof(unsigned short),fp_out);
fclose(fp_out);
fclose(fp_in);
printf("The LZ78 algorithm has done the compression successfully!\n\n");
scanf("%d",&len); //in order to let the outwindow pause!
return 0;
}
int insert_dictionary(char *lemma){
unsigned short length=strlen(lemma);
struct indexes_node *pnext;
unsigned short i,j;
if(p
dictionary[p][j]='\0';
strcpy(dictionary[p],lemma);
p++;
}
else //字典已满而待压缩数据还没有结束的情况,此时直接输出码字而不插入字典
return -1; //indicating the dictionary is full
//TODO: update the index_level1[],find the index i for c
i=lemma[0];
if(i<0 || i>255){
printf("Error!\nsome Chinese words or marks in the following
txt:\n==================================================\n%s\n",buffer_in);
exit(0);
}
if(index_level1[i]==NULL){
index_level1[i]=(struct indexes_node *)malloc(sizeof(indexes_node));
index_level1[i]->initNode();
index_level1[i]->indexes[0]=p-1;
}
else
{
pnext=index_level1[i];
while(pnext->next)pnext=pnext->next;
j=0;
while(j<8){
if(pnext->indexes[j]==0)
break;
j++;
}
}
if(j==8){ //the node is full
pnext->next=(struct indexes_node *)malloc(sizeof(indexes_node));
pnext->next->initNode();
pnext->next->indexes[0]=p-1;
else{
pnext->indexes[j]=p-1;
}
}
}
return 1; //insert successfully
unsigned short find_lemma(char * tmp_lemma){
unsigned short length=strlen(tmp_lemma);
struct indexes_node *pnext;
unsigned short i,j;
if(length>0){
i=tmp_lemma[0];
if(i<0 || i>255){
printf("Error!\nsome Chinese words or marks in the following
txt:\n===============================================\n%s\n",buffer_in);
exit(0);
}
pnext=index_level1[i];
while(pnext){
j=0;
while(j<8){
if(pnext->indexes[j]){
if(strcmp(dictionary[pnext->indexes[j]],tmp_lemma)==0)
return pnext->indexes[j];
}
else
break;
j++;
pnext=pnext->next;
}
}
}
return 0;
//not matching
}