希望大家帮忙把这个C++程序改成C程序.
//Huffman.cpp
#include<iostream>
#include<cstdio>
#include<string>
#include<fstream>
#include<cassert>
#include<cstdlib>
using namespace std;
struct TN {//HuffmanTree 结点
int weight;
int flag; //权和标志位
char ch; //叶子结点的字符
struct TN *lc, *rc, *parent;//父子结点指针
};
typedef struct leafnode{ //做链表时用到的临时结点,便于排序
int weight;
struct leafnode *next;
TN *present;
}LN;
typedef struct allchar{ //记录所有的可能字符
char ch;
struct TN *pleaf;
}AC;
//Huffman Tree class
class HuffmanTree{
TN *TRoot;
AC *allch;
int size;
protected:
void StrtoBin(char str[]);
void BintoStr(char str[]);
public:
HuffmanTree();
~HuffmanTree();
void Construct();
void ReConstruct();
void MakeEmpty(TN *);
void Thread1();
void Thread2();
LN * InsertSort(LN *phead, LN *pnew);//return head pointer
TN * Findp(char ch);
void visit(TN *proot);
void displayvalidchar();
void DoAll();
};
HuffmanTree::HuffmanTree(){ TRoot=NULL; allch=NULL; size=0; }
HuffmanTree::~HuffmanTree(){
MakeEmpty(TRoot);
TRoot=NULL;
if(size!=0) delete []allch;
return ;
}
void HuffmanTree::Construct(){
ifstream infile;
char filename[256];
cout<<"输入字符序列所在的文件名(提示:infile.txt):";
cin>>filename;
infile.open(filename);
if(! infile.is_open()) { cout<<"Can't open file: "<<filename<<endl; exit(1); }
LN *ptop=NULL;
//input all the informations
int n;
infile>>size;
for(n=size; n>0; n--){
TN *pn=new TN;
pn->flag=0; //leaf
pn->parent=NULL;
infile>> pn->ch >> pn->weight;
if(pn->ch=='+') pn->ch=' ';
pn->lc=pn->rc=NULL;
LN *pln=new LN;
pln->present=pn;
pln->weight=pn->weight;
pln->next=NULL;
ptop=InsertSort(ptop, pln);
assert(ptop!=NULL);
}
infile.close();
LN *tmp;
tmp=ptop;
allch=new AC[size];
assert(allch!=NULL);
int i=size-1;
while(tmp!=NULL){
allch[i].ch=tmp->present->ch;
allch[i].pleaf=tmp->present;
tmp=tmp->next;
i--;
}
//construct the tree**************************************
while(ptop!=NULL && ptop->next!=NULL){
TN *pnew=new TN;
pnew->flag=1;
pnew->weight=ptop->weight + ptop->next->weight;
pnew->lc=ptop->present;
pnew->rc=ptop->next->present;
ptop->present->parent=pnew;
ptop->next->present->parent=pnew;
tmp=ptop;
ptop=ptop->next;
delete tmp;
ptop->weight=pnew->weight;
ptop->present=pnew;
tmp=ptop;
ptop=ptop->next;
tmp->next=NULL;
ptop=InsertSort(ptop, tmp);
}
TRoot=ptop->present;
TRoot->parent=NULL;
delete ptop;
cout<<"HuffmanTree构造成功"<<endl;
}
void HuffmanTree::ReConstruct(){
MakeEmpty(TRoot);
TRoot=NULL; size=0; delete[]allch;
Construct();
}
void HuffmanTree::Thread1()
{
char ch, s[1000];
cout<<"从文件读入?(y/n):";
cin >> ch;
if( ch == 'y' ){
ifstream inf( "char.txt" );
if( !inf.is_open() ){ cout<<"can't read from file: char.txt !"<<endl; exit(1); }
while( !inf.eof() ){ inf.getline(s, 1000 ); cout<<s<<endl; StrtoBin(s); }
inf.close();
}
else {
cout<<"输入字符串:" <<endl;
cin>>ch; cin.putback(ch);//clear the stream
cin.getline(s, 1000);
StrtoBin(s);
}
}
void HuffmanTree::Thread2(){
char ch, s[2000];
cout<<"从文件读入?(y/n):";
cin>>ch;
if( ch=='y' ){
ifstream inf("binary.txt");
if( !inf.is_open() ){ cout<<"Can't read from file: 'binary.txt' !"<<endl; exit(1); }
while( !inf.eof() ){
inf.getline(s, 1000 );
if(s[0]!='\0') BintoStr(s);
}
inf.close();
}
else{
cout<<"输入字符串:" <<endl;
cin>>ch; cin.putback(ch);//clear the stream
cin.getline(s, 1000);
BintoStr(s);
}
}
void HuffmanTree::StrtoBin(char str[]){//编码
ofstream outf;
outf.open("binary.txt", ios::out|ios::app);
if(!outf.is_open() ){
cout<<"Can't open file: 'binary.txt' for writing! "<<endl; exit(1);
}
int len=strlen(str), n=0;
for(n=0; n<len; n++){//对每个字符做出相应的处理
char str1[50];
int k=0;
TN *p=Findp(str[n]);
if(!p){cout<<"输出了不允许的字符!"<<endl; exit(1); }
TN *s=p; p=p->parent;
while(p!=NULL){
if(p->lc==s) str1[k++]='0';
else str1[k++]='1';
s=p; p=p->parent;
}
str1[k]='\0';
int len=strlen(str1);
for(len--; len>=0; len--) { cout<<str1[len]; outf<<str1[len]; }
}
cout<<endl;
outf<<endl;
outf.close();
return;
}
void HuffmanTree::BintoStr(char str[]){//解码
int len=strlen(str), i;
TN *ptmp=TRoot;
cout<<'\'';
for(i=0; i<len; i++) {
if(str[i]=='0') ptmp=ptmp->lc;
else if(str[i]=='1') ptmp=ptmp->rc;
else {cout<<"ERROR!<输入了未定义的字符>"<<endl; return; }
if(ptmp->flag==0){ cout<<ptmp->ch; ptmp=TRoot; }
}
cout<<'\''<<endl;
return;
}
TN * HuffmanTree::Findp(char ch){
int i=0;
while(i<size) {
if(allch[i].ch==ch) return allch[i].pleaf;
i++;
}
return NULL;
}
void HuffmanTree::MakeEmpty(TN *proot){
if(proot==NULL) return;
MakeEmpty(proot->lc);
MakeEmpty(proot->rc);
delete proot;
return;
}
LN * HuffmanTree::InsertSort(LN *phead, LN *pnew){
assert(pnew!=NULL);
if(phead==NULL) { phead=pnew; return phead; }
if(pnew->weight<=phead->weight)
{ pnew->next=phead; phead=pnew; return phead; }
LN *ptmp=phead, *ph=phead;
while(ptmp->next!=NULL) {
if(pnew->weight>ptmp->weight && pnew->weight<=ptmp->next->weight){
pnew->next=ptmp->next;
ptmp->next=pnew;
return ph;
}
else ptmp=ptmp->next;
}
ptmp->next=pnew;
return ph;
}
void HuffmanTree::visit(TN *proot){
if(proot==NULL) return ;
visit(proot->lc);
visit(proot->rc);
cout<<proot->ch<<" ";
}
void HuffmanTree::displayvalidchar(){
int i;
cout<<"可以编码的字符有:"<<endl;
for(i=0; i<size; i++)
cout<<allch[i].ch<<" ";
cout<<endl;
}
void HuffmanTree::DoAll(){
int flag=1;
cout<<"********** Huffman编码,解码 **********"<<endl;
Construct();
while(flag==1){
cout<<"请选择命令: ";
cout<<"退出<q> 编码<c> 解码<t> 可编码字符<d> 重建HTREE<r>"<<endl;
char ch;
cin>>ch;
switch(ch)
{
case 'q':flag=0; break;
case 'c':Thread1();break;
case 't':Thread2();break;
case 'd':displayvalidchar();break;
case 'r':ReConstruct();break;
default :cout<<"未知命令:"<<ch<<endl;break;
}
}
cout<<"************* 结 束 *************"<<endl;
return;
}
int main()
{
HuffmanTree obj;
obj.DoAll();
return 0;
}