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

📄 stringinttree.cpp

📁 这是一个有关哈夫曼编/译码器的课程设计
💻 CPP
字号:
#include "stdafx.h"
#include "StringIntTree.h"

// ---------------------- CStringIntTreeNode ---------------------
CStringIntTreeNode::CStringIntTreeNode(LPCTSTR str,int i)
{
    mString = new TCHAR[strlen(str)+1];
	strcpy(mString,str);

	mInt = i;
}

CStringIntTreeNode::~CStringIntTreeNode()
{
	delete [] mString;
}

// ---------------------- CStringIntTree --------------------------

int CStringIntTree::Compare(CBinTreeNode* p1,CBinTreeNode* p2) const
{
	return strcmp(
		     ((CStringIntTreeNode*)p1)->GetString(),
			 ((CStringIntTreeNode*)p2)->GetString()
			 );
}

void CStringIntTree::Add(LPCTSTR str,int i)
{
	Insert( new CStringIntTreeNode(str,i) );
}

BOOL CStringIntTree::DeleteByString(LPCTSTR str)
{
  CStringIntTreeNode searchNode(str,0); // The int part of the node is
                                        // ignored in comparison

  return RemoveNode(&searchNode);
}

CStringIntTreeNode* CStringIntTree::FindByString(LPCTSTR str)
{
  CStringIntTreeNode searchNode(str,0); // The int part of the node is
                                        // ignored in comparison

  CStringIntTreeNode* pRes = (CStringIntTreeNode*) Find(&searchNode);
  return pRes;
}


// tcb_Clear : TraverseCallBack. Delete the node
void tcb_Clear(CBinTreeNode* p, void* pParam)
{
  delete (CStringIntTreeNode*)p;
}


void CStringIntTree::Clear()
{
	// ParentLast, so child is deleted prior its parent
	Traverse(ParentLast,tcb_Clear,this);
	CBinTree::Clear();
}

void tcb_Save(CBinTreeNode* p, void* pParam)
{
	CFile* pFile = (CFile*) pParam;
	CStringIntTreeNode* pNode = (CStringIntTreeNode*) p;

	int i = pNode->GetInt();
	pFile->Write(pNode->GetString(),strlen(pNode->GetString())+1);
	pFile->Write(&i,sizeof(int));
}

void CStringIntTree::Save(LPCTSTR fileName)
{
  CFile f;
  VERIFY(f.Open(fileName,CFile::modeCreate | CFile::modeWrite));

  // ParentFirst, so that when we read the file, the same tree
  // structure will be built.
  Traverse(ParentFirst,tcb_Save,&f);

  f.Close();

}

void CStringIntTree::Load(LPCTSTR fileName)
{
  CFile f;
  VERIFY(f.Open(fileName,CFile::modeRead));

  Clear();

  int pos = 0;
  int size = f.GetLength();

  LPCTSTR str;
  int* pInt;

  char* buf = new char[size];

  f.Read(buf,size);  
  f.Close();

  while (pos<size)
  {
	  str = &buf[pos];
	  pInt= (int*)&buf[pos + strlen(str)+1];

	  Add(str,*pInt);

	  pos+=strlen(str)+sizeof(int)+1;
  }

  delete [] buf;


}



⌨️ 快捷键说明

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