📄 textquery.cpp
字号:
# include "TextQuery.h"
void TextQuery::BuildMap(std::ifstream& in, std::string file_name)
{
m.clear();
strall.clear();
huffumancode.clear();
str="";
sum=0;
in.open(file_name.c_str());
if(!in)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
string strread;
while(getline(in,strread))
{
strall+=strread;
if(!in.eof())
strall+='\n';
}
for(size_t i=0;i<strall.size();i++)
{
m[strall[i]]++;
}
tree=HuffumanTree();
in.close();
in.clear();
}
ExtBinTree<char>* TextQuery::HuffumanTree()
{
ExtBinTree<char>* newtree=NULL;
ExtBinTree<char> first,second;
Heap<ExtBinTree<char>> hp;
size_t n=m.size();
vector<ExtBinTree<char>> fr;
ExtBinTree<char> Node[hp.DefaultSize];
if(n>hp.DefaultSize)
{
cerr<<"Out of space!"<<endl;
return NULL;
exit(1);
}
map<char,int>::iterator it=m.begin();
int i=0;
while(it!=m.end())
{
Node[i].root->c.data=it->first;
Node[i].root->c.key=it->second;
Node[i].root->SetLeft(NULL);
Node[i].root->SetRight(NULL);
fr.push_back(Node[i]);
i++;
it++;
}
hp.MakeHeap(fr);
for(size_t i=0;i<n-1;i++)
{
hp.RemoveMin(first);
hp.RemoveMin(second);
newtree=new ExtBinTree<char>(first,second);
hp.Insert(*newtree);
}
return newtree;
}
void TextQuery::Encode()
{
Element<char>* current;
Element<char>* currentpre;
size_t length=m.size();
string substr;
pair<char,string> p;
for(size_t i=0;i<length;i++)
{
current=currentpre=tree->root;
substr="";
while(1)
{
if(current->GetLeft()->GetMark()==0)
{
substr+='0';
if(current->GetLeft()->GetLeft()==NULL)
{
p=make_pair(current->GetLeft()->c.data,substr);
current->GetLeft()->SetMark(1);
this->huffumancode.insert(p);
break;
}
currentpre=current;
current=current->GetLeft();
}
else
{
substr+='1';
if(current->GetRight()->GetLeft()==NULL)
{
p=make_pair(current->GetRight()->c.data,substr);
current->GetRight()->SetMark(1);
current->SetMark(1);
huffumancode.insert(p);
while(currentpre->GetLeft()->GetMark()==1&¤tpre->GetRight()->GetMark()==1)
{
currentpre->SetMark(1);
if(currentpre==tree->root)
break;
else if(currentpre->GetParent()!=NULL)
currentpre=currentpre->GetParent();
else
break;
}
break;
}
currentpre=current;
current=current->GetRight();
}
}
}
}
int TextQuery::Decode(char& ch,string& str,string file_name)
{
ofstream on;
on.open(file_name.c_str());
if(!on)
{
cerr<<"The file cannot open!"<<endl;
return 0;
}
Element<char>* current=tree->root;
for(size_t i=0;i<str.size();i++)
{
if(str[i]=='0')
current=current->GetLeft();
else
current=current->GetRight();
if(current->GetLeft()==NULL)
{
ch=current->c.data;
current=tree->root;
on<<ch;
cout<<ch;
}
}
cout<<endl;
on.clear();
on.close();
return 1;
}
void TextQuery::Compress(ifstream& in,string source_file,ofstream& on,string destination_file)
{
on.open("TempCompress.txt",ios::out);
if(!on)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
cout<<strall<<endl;
for(size_t i=0;i<strall.size();i++)
str+=huffumancode[strall[i]];
on<<str;
strpre=str;
cout<<str<<endl;
sum=str.size();
on.close();
on.clear();
in.close();
in.clear();
in.open("TempCompress.txt",ios::in);
if(!in)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
on.open(destination_file.c_str(),ios::binary);
if(!on)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
bitset<8> b;
last=sum%8;
size_t count=0;
while(in>>b)
{
if(count<sum/8)
{
on.write((char*)&b,1);
cout<<b;
}
else
{
on<<str.substr(str.size()-last,str.size());
cout<<str.substr(str.size()-last,str.size());
break;
}
count++;
}
cout<<endl;
in.close();
in.clear();
on.close();
on.clear();
}
void TextQuery::Depress(std::ifstream &in, std::string source_file, std::ofstream &on, std::string destination_file)
{
in.open(source_file.c_str(),ios::binary);
if(!in)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
on.open("TempDepress.txt",ios::out);
if(!on)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
string substr;
bitset<8> b;
size_t count=0;
int l=sum%8;
while(in.read((char*)&b,1))
{
if(count<sum/8)
{
on<<b;
cout<<b;
}
else
{
if(l!=0)
{
int i=0;
i=-l;
in.seekg(i,ios::end);
in>>substr;
on<<substr;
cout<<substr;
}
else
break;
}
count++;
}
cout<<endl;
on.clear();
on.close();
in.clear();
in.close();
in.open("TempDepress.txt",ios::in);
if(!in)
{
cerr<<"The file does not exsit!"<<endl;
return;
}
str="";
char ch;
while(in>>ch)
str+=ch;
in.clear();
in.close();
if(strpre==str)
{
Decode(ch,str,destination_file);
}
else
cerr<<"Not the same!"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -