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

📄 lzwview.cpp

📁 用可视化环境,实现LZW压缩算法 编程环境:VC6.0
💻 CPP
字号:
// LZWView.cpp : implementation of the CLZWView class
//

#include "stdafx.h"
#include "LZW.h"

#include "LZWDoc.h"
#include "LZWView.h"
#include<fstream>
#include"HashTable.h"
#include"stdlib.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CLZWView

IMPLEMENT_DYNCREATE(CLZWView, CFormView)

BEGIN_MESSAGE_MAP(CLZWView, CFormView)
	//{{AFX_MSG_MAP(CLZWView)
	ON_BN_CLICKED(IDC_LIULAN, OnLiulan)
	ON_BN_CLICKED(IDC_JIEYA, OnJieya)
	ON_BN_CLICKED(IDC_YASUO, OnYasuo)
	ON_BN_CLICKED(IDC_LIULAN2, OnLiulan2)
	ON_WM_CREATE()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLZWView construction/destruction

CLZWView::CLZWView()
	: codes(4096),CFormView(CLZWView::IDD)
{
	//{{AFX_DATA_INIT(CLZWView)
	m_s = _T("");
	m_s2 = _T("");
	m_s4 = _T("");
	m_s3 = _T("");
	m_2 = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	status=0;
	alpha=256;
	s=new unsigned char[codes];
 ht=new ele[codes];
 
//	codes=4096;

}

CLZWView::~CLZWView()
{
	delete ht;
	delete s;
}

void CLZWView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLZWView)
	DDX_Control(pDX, IDC_PROGRESS2, m_jindu2);
	DDX_Control(pDX, IDC_PROGRESS1, m_jindu1);
	DDX_Text(pDX, IDC_EDIT1, m_s);
	DDX_Text(pDX, IDC_EDIT2, m_s2);
	DDX_Text(pDX, IDC_EDIT4, m_s4);
	DDX_Text(pDX, IDC_EDIT3, m_s3);
	DDX_Text(pDX, IDC_S2, m_2);
	//}}AFX_DATA_MAP
}

BOOL CLZWView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CLZWView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	m_jindu1.SetRange(1,(short)50000);
 m_jindu1.SetStep(1);
 m_jindu1.SetPos(1);
  m_jindu2.SetRange(1,(short)50000);
 m_jindu2.SetStep(1);
 m_jindu2.SetPos(1);
 GetDlgItem(IDC_YASUO)->EnableWindow(FALSE);
  GetDlgItem(IDC_JIEYA)->EnableWindow(FALSE);
//	m_p1.SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));
//	this->Invalidate();
}

/////////////////////////////////////////////////////////////////////////////
// CLZWView printing

BOOL CLZWView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CLZWView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CLZWView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CLZWView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CLZWView diagnostics

#ifdef _DEBUG
void CLZWView::AssertValid() const
{
	CFormView::AssertValid();
}

void CLZWView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CLZWDoc* CLZWView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLZWDoc)));
	return (CLZWDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CLZWView message handlers

void CLZWView::OnLiulan() 
{
	CFileDialog dlg(TRUE);
	if(dlg.DoModal()==IDOK){
		m_s=dlg.GetPathName();
		//UpdateData(TRUE);
			Input=(LPCTSTR)m_s;


		in.open(Input,ios::binary);
		if(in.fail())
			MessageBox("打开错误!");
		 GetDlgItem(IDC_YASUO)->EnableWindow(TRUE);
	}
	
	
	UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CLZWView::OnJieya() 
{

		
	
		
		CFileDialog dlg(FALSE);
		int b=dlg.DoModal();
		if(b==IDOK){
		


		
		m_s4=dlg.GetPathName();
		//UpdateData(TRUE);
		Output=(LPCTSTR)m_s4;
		
			UpdateData(FALSE);
			out1.open(Output,ios::binary);
			if(out1.fail())
				MessageBox("out打开错误!");
	
	
		if(MessageBox("确认?","提示框",MB_OKCANCEL)==IDOK)
			Decompress();
		
		
		
	}
	// TODO: Add your control notification handler code here
	
}

void CLZWView::Compress()
{	HashTable<element,unsigned long> h(4099);
	element e;
	int step=1;
	for(int i=0;i<alpha;i++){
		e.key=i;
		e.code=i;
		h.Insert(e);
	}
	int used=alpha;
	unsigned char c;
	char a;
	in.get(a);
	c=a;

	unsigned long pcode=c;
	if(!in.eof()){
		
		do{
				if(step<45000){
			m_jindu1.StepIt();
			step++;
			}
			in.get(a);
			c=a;
			if(in.eof()) break;
			 if(pcode==0||c==0)
			{
				Output1(pcode);		
				pcode=c;			
				continue;
			} 
		
			unsigned long k=(pcode<<8)+c;
			if(h.Search(k,e)) pcode=e.code;
		

			else {
				Output1(pcode);
				if(used<4096)
				{
					e.code=used++;
					e.key=(pcode<<8)|c;
				
					h.Insert(e);
					}
				pcode=c;
			}
		
		}while(true);
		Output1(pcode);
		if(status){
			c=LeftOver<<4;
			out.put(c);
		//	cout<<c+0<<" ";
		}
	}
	for(;step<=50000;step++)
		m_jindu1.StepIt();
	m_jindu1.SetPos(50000);
	MessageBox("over!");
	m_jindu1.SetPos(1);
	out.close();
	in.close();
	status=0;
	alpha=256;

}

void CLZWView::Output1(unsigned long pcode)
{
		unsigned char c,d;


	if(status){
		d=pcode&255;
		c=(LeftOver<<4)|(pcode>>8);
	
		out.put(c);

	
		out.put(d);

		status=0;
	}
	else{
		LeftOver=pcode&15;
		c=pcode>>4;

		out.put(c);
	
		status=1;
	}

}

void CLZWView::Decompress()
{
int used=alpha;
	int pcode;
	int ccode;
	int step=1;
	if(GetCode(pcode)){
		s[0]=pcode;
		out1.put(s[0]);

		size=0;
		
		while(GetCode(ccode)){
				if(step<45000)
			{
				step++;
				m_jindu2.StepIt();
				/*m_2.Format("%d '%'",step/50);
				UpdateData(FALSE);*/
			}
			if(pcode==0||ccode==0)
			{
              
				Output2(ccode);
		
				pcode=ccode;
				continue;
			}
			if(ccode<used){
				Output2(ccode);
		
					if(used<codes){
						ht[used].prefix=pcode;
						ht[used++].suffix=s[size];
				
					}
			}
			else{
		
				ht[used].prefix=pcode;
				ht[used++].suffix=s[size];
			
		
				Output2(ccode);
			}
			pcode=ccode;

		
		}
	}
	in1.close();
	out1.close();
	for(;step<=50000;step++)
		m_jindu2.StepIt();
	m_jindu2.SetPos(50000);
	MessageBox("over!!");
	m_jindu2.SetPos(1);
}

bool CLZWView::GetCode(int &code)
{char a;
	unsigned char c,d;
	in1.get(a);
	c=a;
	//cout<<c+0<<" ";

	if(in1.eof()) return false;
	if(status) code=(LeftOver<<8)|c;
	else {
		in1.get(a);
		d=a;
	
	//	cout<<d+0<<" ";
		LeftOver=d&15;	
		code=(c<<4)|(d>>4);

	}
	//cout<<code<<"**"<<endl;
	status=1-status;
	return true;

}

void CLZWView::Output2(int code)
{
		size=-1;
	while(code>=alpha){
	s[++size]=ht[code].suffix;

		code=ht[code].prefix;
	}
	s[++size]=code;
	for(int i=size;i>=0;i--){
		out1.put(s[i]);

	}
}

void CLZWView::OnYasuo() 
{
	

	// TODO: Add your control notification handler code here
		CFileDialog dlg(FALSE);
		int b=dlg.DoModal();
		
		
		if(b==IDOK){
		
			m_s2=dlg.GetPathName();
			UpdateData(FALSE);
	
			Output=(LPCTSTR)m_s2;
			int k=strlen(Output);
			if(!(Output[k-1]=='z'&&Output[k-2]=='z'&&Output[k-3]=='z'&&Output[k-4]=='.'))
			MessageBox("扩展名不正确,本程序支持“.zzz”的扩展名!");
		else{
		
			out.open(Output,ios::binary);


			if(out.fail())
				MessageBox("打开错误!");
				UpdateData(FALSE);
			if(MessageBox("确认?","提示框",MB_OKCANCEL)==IDOK)
		//	Sleep(100);
				Compress();	
		

			
		}
		

		
	}
}

void CLZWView::OnLiulan2() 
{
CFileDialog dlg(TRUE);
	if(dlg.DoModal()==IDOK){
		m_s3=dlg.GetPathName();
		//UpdateData(TRUE);
		Input=(LPCTSTR)m_s3;
			int k=strlen(Input);
			if(!(Input[k-1]=='z'&&Input[k-2]=='z'&&Input[k-3]=='z'&&Input[k-4]=='.'))
				MessageBox("扩展名不正确,本程序解压扩展名“.zzz”的文件!");
			else{
					in1.open(Input,ios::binary);
				if(in1.fail())
					MessageBox("in打开错误!");
				else
					GetDlgItem(IDC_JIEYA)->EnableWindow(TRUE);
			}
	
	}

	UpdateData(FALSE);	
}

void CLZWView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	pDC=GetDC();
	pDC->BitBlt( 263, 16, 530,400, &m_ZDC, 0,0, SRCCOPY );
	
}


int CLZWView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;
	CDC* pDC=GetDC();
	m_Z.LoadBitmap(IDB_Z);

   	m_ZDC.CreateCompatibleDC(pDC);
    m_ZDC.SelectObject(&m_Z);
//	pDC->DeleteDC();
	return 0;
}

⌨️ 快捷键说明

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