⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 huffmandlg.cpp

📁 vc编写的基于哈夫曼的文件压缩程序,解压后即可运行,
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                 ptr=tree; 
                 index=0; 
             }  
        }  
    }  
    //printf("\n"); 
    free(code);   
}  

/*译码 */ 
void decode(linktree tree,char code[],char *filename) 
{ 
   int i=0,j=0,k=0,h=0; 
   char char0[2],cc[10]; 
   CFile tf;
	if(!tf.Open(filename,CFile::modeCreate|CFile::modeWrite))
	{
		char dfn[250];
		strcpy(dfn,filename);
		strcat(dfn,"文件不存在");
		AfxMessageBox(dfn);
		return;
	}
	//AfxMessageBox(code);
	//tf.Write(code,strlen(code));
   linktree ptr=tree; 
 //  printf("哈夫曼编码-----相应字符\n\n"); 
   i=strlen(code);
//   code[i]='1';
//   code[i+1]='\0';
j=0;
   for(i=0;code[i]!='\0';i++) 
   { 
        if(code[i]=='0'&&ptr->lchild) 
		{
           ptr=ptr->lchild;  
		   cc[j++]='0';
		}
        else if(code[i]=='1'&&ptr->rchild) 
		{
           ptr=ptr->rchild; 
		   cc[j++]='1';
		}
		if(!ptr->lchild&&!ptr->rchild)
		{
			char0[0]=ptr->ch;
			char0[1]='\0';
			tf.Write(char0,1);
			cc[j]='\0';
			//AfxMessageBox(cc);
			ptr=tree;
			j=0;
		}
	}
   cc[j]='\0';
//	   AfxMessageBox(cc);
//	tf.Write(EOF,1);
	tf.Close();
} 

/*用递归的方法释放哈夫曼树所占用的空间*/ 
void deletenode(linktree tree) 
{ 
   linktree ptr=tree; 
   if(ptr) 
   { 
       deletenode(ptr->lchild); 
       deletenode(ptr->rchild); 
       free(ptr); 
   }  
} 


void CHuffmanDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CFile sf,tf;
	UpdateData(TRUE);
		// open mapping table
	char filename[250];
	char text[2],*sfp,hcode[256][100],*tempp;
	int sfilelength;
	strcpy(filename,m_spath);
	if(!sf.Open(filename,CFile::modeRead))
	{
		char dfn[250];
		strcpy(dfn,filename);
		strcat(dfn,"文件不存在");
		AfxMessageBox(dfn);
		return;
	}
	strcpy(filename,m_tpath);
	if(!tf.Open(filename,CFile::modeCreate|CFile::modeWrite))
	{
		char dfn[250];
		strcpy(dfn,filename);
		strcat(dfn,"文件不存在");
		AfxMessageBox(dfn);
		return;
	}
	sfilelength=sf.GetLength();
	sfp=(char*)malloc(sfilelength+2);
	int i=0;
	text[1]='\0';
    while(sf.Read(text,1))
	{
		sfp[i]=text[0];
		i++;
		//AfxMessageBox(text);
		//tf.Write(text,1);
	}
	sfp[i]='\0';
	sfilelength=i;
	linktree temp,ht,htree,ptr=NULL,hht; 
	htree=NULL;
	temp=initialize(sfp,sfilelength);     //初始化链表
    ht=Order(temp);                //对链表进行排序
	if(ht->next->weight==sfilelength)       //判断是否是非法格式
    {
          AfxMessageBox("信号只有一个字符,无法编码\n");
          exit(-1);
    }
	int j=0,k=0,sl;
	short loc=3;
	char cha=0;
	hht=ht->next;
	tf.Write("000",3);//用于记录树的存放位置及最后一个字节的有效位数
	struct tree
	{
		char ch;
		short weight;
	}tt;
	while(hht!=NULL)
	{
		tt.ch=hht->ch;
		tt.weight=hht->weight;
		tf.Write(&tt,sizeof(tt));
		hht=hht->next;
		loc=loc+sizeof(tt);
	}

    htree=createHftree(ht);     //对排序好的链表建立哈夫曼表
    Huffmancoding(htree,hcode); //对哈夫曼表再进行处理,打印输出
//char cc[9];
//	AfxMessageBox(sfp);
	cha=0;
	for(i=0;i<sfilelength;i++)
	{
		j=0;
		while(sfp[i]!=hcode[j][0]) 
		{
			j++;
		}
		//text[0]=sfp[i];
		//text[1]='\0';
		//AfxMessageBox(text);
		tempp=&hcode[j][1];
		//AfxMessageBox(tempp);
//		tf.Write(tempp,strlen(tempp));
		sl=strlen(tempp);
		for(j=0;j<sl;j++)
		{
			
			cha=cha<<1;
			cha=cha|(tempp[j]-'0');
			//if(sfp[i+1]=='\0')
			//{
			//	text[0]=cha+'0';
			//	text[1]='\0';
			//	AfxMessageBox(text);
			//}
			k++;
			if(k>=8)
			{
				text[0]=cha;
				text[1]='\0';
				//ll=cha;
				//itoa(ll,&cc[0],8);
				//AfxMessageBox(cc);
				//tf.Write(cc,8);
				tf.Write(text,1);
				k=0;
				cha=0;
			}
		}
	}
	//text[0]=sfp[i-1];
	//text[1]='\0';
	//AfxMessageBox(text);
	if(k<8)
	{
//		text[0]=k+'0';
//	text[1]='\0';
//	AfxMessageBox(text);
		j=k;
		while(j<8)
		{
			cha=cha<<1;
			//cha=cha+1;
			j++;
		}
		text[0]=cha;
		text[1]='\0';
//		AfxMessageBox(text);
		tf.Write(text,1);
	}
	tf.SeekToBegin();
	//itoa(loc,text,10);
	//AfxMessageBox(text);
	tf.Write(&loc,2);
	text[0]=k;
	text[1]='\0';
	tf.Write(text,1);
	free(sfp);
	deletenode(htree);
	sf.Close();
	tf.Close();
//	m_tpath=m_spath;
//	UpdateData(FALSE);
}

void CHuffmanDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fileDlg(TRUE, "*.txt", "*.txt", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||", NULL);
	//fileDlg.m_ofn.lpstrInitialDir=datasource0;
	if(fileDlg.DoModal()==IDOK)
	{
		// 得到文件路径
		m_spath.Insert(0,fileDlg.GetPathName());
		UpdateData(FALSE);		
		// 新建文档 
		PostMessage(WM_COMMAND,ID_FILE_NEW,0);
	}	
}

void CHuffmanDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fileDlg(TRUE, "*.txt", "*.txt", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
	"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||", NULL);
	fileDlg.m_ofn.lpstrInitialDir=m_spath;
	UpdateData(FALSE);
	if(fileDlg.DoModal()==IDOK)
	{
		// 得到文件路径
		m_tpath.Insert(0,fileDlg.GetPathName());
		UpdateData(FALSE);		
		// 新建文档 
		PostMessage(WM_COMMAND,ID_FILE_NEW,0);
	}	
}

void CHuffmanDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	CFile sf,ttf;
	UpdateData(TRUE);
		// open mapping table
	char filename[250],*sfp,*cfp;
	char text[3];
	int sfilelength;
	strcpy(filename,m_spath);
	if(!sf.Open(filename,CFile::modeRead))
	{
		char dfn[250];
		strcpy(dfn,filename);
		strcat(dfn,"文件不存在");
		AfxMessageBox(dfn);
		return;
	}
	strcpy(filename,m_tpath);
	sfilelength=sf.GetLength();
	int j=0,k=0;
	short loc;
	char cha=0,sl;
	sf.Read(&loc,2);
	sf.Read(&sl,1);
	//text[0]=sl+'0';
	//text[1]='\0';
	//AfxMessageBox(text);
	linktree node,ht,htree,ptr=NULL; 
	htree=(linktree)malloc(sizeof(Hftree));
	htree->next=NULL;
	struct tree
	{
		char ch;
		short weight;
	}tt;
	for(j=3;j<loc;j=j+sizeof(tt))
	{
		sf.Read(&tt,sizeof(tt));
		node=(linktree)malloc(sizeof(Hftree)); 
		if(!node) exit(-1); 
		node->next=NULL; 
		node->parent=NULL; 
		node->lchild=NULL; 
		node->rchild=NULL; 
		node->mark=0; 
		node->ch=tt.ch; 
		node->weight=tt.weight; 
		if(j==3)
		{
			htree->next=node;
			ptr=node;
		}
		else
		{
			ptr->next=node;
			ptr=node;
		}
	}
	sfp=(char*)malloc(sfilelength-loc+2);
	int i=0;
//	text[1]='\0';
    while(sf.Read(text,1))
	{
		sfp[i]=text[0];
		i++;
		//tf.Write(text,1);
	}
	sfp[i]='\0';
	//text[0]=sfp[i-1]-0x80+'0';
	//text[1]='\0';
	//AfxMessageBox(text);	
	//AfxMessageBox("2");
	loc=i;
	ht=htree;
    htree=createHftree(ht);     //对排序好的链表建立哈夫曼树
	
	cfp=(char*)malloc(loc*8);
	char l,tl[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
	//tl=6;
	//tl=tl<<0;
	//AfxMessageBox("4");
	for(i=0,j=0;i<loc;i++)
	{
		//tl=sfp[i];
		if(i<loc-1)
			k=8;
		else
		{
			k=sl;
//			text[0]=k+'0';//sfp[i]-0x80+'1';
//				text[1]='\0';
//				AfxMessageBox(text);
		}

		for(l=0;l<k;l++)
		{
			if((sfp[i]&tl[l])==tl[l])
				cfp[j]='1';
			else
				cfp[j]='0';
			j++;
		}
	}
//	cfp[j]='\0';
	//j=j+sl-8;
//	itoa(sl,text,10);
//	AfxMessageBox(text);
	cfp[j]='\0';
//	strcpy(filename,m_tpath);
//	ttf.Open(filename,CFile::modeCreate|CFile::modeWrite);
//	ttf.Write(cfp,j);
//	ttf.Close();
//	sf.Close();
//	exit(0);
//AfxMessageBox(cfp);
	decode(htree,cfp,filename);
	free(sfp);
	free(cfp);
	deletenode(htree);
	sf.Close();
//	tf.Close();
//	m_tpath=m_spath;
//	UpdateData(FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -