?? huffman.cpp
字號:
ptr=tree; //指針復位
index=0;
}
}
if(ptr->rchild&&ptr->rchild->mark==0)
{
ptr=ptr->rchild;
code[index++]='1'; //右支路編碼為1
}
if(!ptr->lchild&&!ptr->rchild) //如果沒有左右孩子,即葉子結點
{
ptr->mark=1;
code[index++]='\0';
codedictionary[k].ch=ptr->ch; //給字典賦字符值
for(index=0;code[index]!='\0';index++)
codedictionary[k].code[index]=code[index];//給字典賦碼值
codedictionary[k].code[index]='\0';
k++;
ptr=tree;
index=0;
}
if(ptr->lchild->mark==1&&ptr->rchild->mark==1)//如果左右孩子都已標記
{
ptr->mark=1;
ptr=tree;
index=0;
}
}
}
printf("\n");
}
/*
*解碼,即將huffman編碼轉化為字符串
*/
void decodeHTree(LinkTree tree , char *code , char *filestring)
{
int i=0,j=0,k=0;
char *char0_1;
LinkTree ptr=tree;
char0_1=(char *)malloc(MAX_SINGLECODE_LEN); //此數組用于統計輸入的0-1序列
printf("解碼后的字符串如下:\n");
for(j=0,ptr=tree;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j=0,ptr=tree)
{
for(j=0;code[i]!='\0'&&ptr->lchild&&ptr->rchild;j++,i++)
{
if(code[i]=='0')
{
ptr=ptr->lchild;
char0_1[j]='0';
}
if(code[i]=='1')
{
ptr=ptr->rchild;
char0_1[j]='1';
}
}
if(!ptr->lchild&&!ptr->rchild)
{
printf("%c",ptr->ch); //顯示解壓后的字符
filestring[k++]=ptr->ch; //將字符逐一保存到字符串里
}
if(code[i]=='\0'&&ptr->lchild&&ptr->rchild)
{
char0_1[j]='\0';
printf("\n沒有與最后的幾個0-1序列:%s相匹配的字符!\n",char0_1);
return;
}
}
printf("\n\n");
filestring[k]='\0';
free(char0_1);
}
/*
*釋放哈夫曼樹所占用的空間
*/
void deleteNode(LinkTree tree)
{
LinkTree ptr=tree;
if(ptr)
{
deleteNode(ptr->lchild);
deleteNode(ptr->rchild);
free(ptr);
}
}
/*
*將整個字符串轉化為0-1的字符串
*/
void compressString(char *string,CodeDictionary *codedictionary,char *codestring)
{
int i=0,j=0,k=0,m;
while(string[i]) //整個文件字符串沒結束時
{
while(string[i]!=codedictionary[j].ch&&j<MAX_WORDS)
//找與對應字符相同的字符
j++;
if(string[i]==codedictionary[j].ch) //如果找到與對應字符相同的字符
for(m=0;codedictionary[j].code[m];m++,k++)
codestring[k]=codedictionary[j].code[m];
j=0; //字典復位
i++;
}
codestring[k]='\0';
}
/*
*把指定文件讀到字符串中
*/
void readFile(char *string)
{
FILE *fp;
int i;
char ch; //記錄讀入的字符
char path[PATH_LEN]; //文本文件的讀路徑
printf("請輸入要進行編碼的文件:");
gets(path);
while((fp=fopen(strcat(path,".txt"),"r"))==NULL)//用字符串拼接函數自動添加擴展名
{
printf("\n錯誤!!!無法讀取源文件!\n");
printf("\n請重新輸入要進行編碼的文件:");
gets(path);
}
ch=fgetc(fp);
for(i=0;ch!=EOF;i++)
{
string[i]=ch;
ch=fgetc(fp);
}
string[i]='\0';
fclose(fp);
}
/*
*保存編碼后的解碼樹和字符串
*/
void writeCode(LinkTree tree,char *string)
{
FILE *fp;
int i;
int weight; //記錄寫入的權值
char ch; //記錄寫入的字符
LinkTree p;
char path[PATH_LEN]; //0-1碼文件的寫路徑
printf("請輸入保存編碼的文件:");
gets(path);
if((fp=fopen(strcat(path,".txt"),"w+"))==NULL)
{
printf("\n錯誤!!!寫文件出錯!\n");
getch();
return;
}
p=tree->next;
/*解碼樹部分寫入文件前部分*/
do
{
ch=p->ch;
weight=p->weight;
fprintf(fp,"%c%d",ch,weight);
p=p->next;
}while(p);
fprintf(fp,"%c%d",'$',END_TREE);
fseek(fp,sizeof(char),1); //空出區分位
/*字符串的Huffman編碼寫入文件后部分*/
for(i=0;string[i];i++)
{
ch=string[i];
fputc(ch,fp);
}
printf("\n文件編碼成功!任意鍵返回\n");
getch();
fclose(fp);
}
/*
*讀取編碼后的0-1字符串
*/
void readCode(LinkTree tree,char *string)
{
FILE *fp;
int i;
int weight; //記錄讀入的權值
char ch; //記錄讀入的字符
LinkTree ptr,beforeptr;
char path[PATH_LEN]; //0-1碼文件的讀路徑
printf("請輸入要進行解碼的文件:");
gets(path);
while((fp=fopen(strcat(path,".txt"),"r"))==NULL)//用字符串拼接函數自動添加擴展名
{
printf("\n錯誤!!!無法讀取源文件!\n");
printf("\n請重新輸入要進行解碼的文件:");
gets(path);
}
beforeptr=tree;
/*從文件前部分讀出解碼樹*/
fscanf(fp,"%c%d",&ch,&weight);
while(weight!=END_TREE)
{
if((ptr=(LinkTree)malloc(sizeof(HTNode)))==NULL)
{
printf("內存分配失敗!");
getch();
exit(1); //錯誤出口
}
ptr->ch=ch;
ptr->weight=weight;
ptr->lchild=NULL;
ptr->rchild=NULL;
ptr->parent=NULL;
ptr->mark=0;
beforeptr->next=ptr;
beforeptr=ptr;
fscanf(fp,"%c%d",&ch,&weight);
}
beforeptr->next=NULL;
fseek(fp,sizeof(char),1); //文件指針定位
/*從文件后部分讀出0-1碼*/
ch=fgetc(fp);
for(i=0;ch!=EOF;i++)
{
string[i]=ch;
ch=fgetc(fp);
}
string[i]='\0';
fclose(fp);
}
/*
*保存解碼后的文件
*/
void writeFile(char *string)
{
FILE *fp;
char ch; //記錄寫入的字符
int i;
char path[PATH_LEN]; //文本文件的寫路徑
printf("請輸入保存以上字符串的文件:");
gets(path);
if((fp=fopen(strcat(path,".txt"),"w+"))==NULL)//用字符串拼接函數自動添加擴展名
{
printf("\n錯誤!!!文件寫入失敗!\n");
getch();
return;
}
for(i=0;string[i];i++)
{
ch=string[i];
fputc(ch,fp);
}
printf("\n文件解碼成功!任意鍵返回\n");
getch();
fclose(fp);
}
/*
*顯示主菜單
*/
void menu()
{
printf("\n\n\n\n\n\n\n");
printf("\n\t\t\t\t請選擇運行方式:\n");
printf("\n\t\t\t\t (c)文件編碼\n");
printf("\n\t\t\t\t (d)文件解碼\n");
printf("\n\t\t\t\t (q)退 出\n");
printf("\n\n\n\n\n\n\n\n\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -