| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 761 人关注过本帖
标题:(求助)改写程序(Huffman编码)
只看楼主 加入收藏
loveszc
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2006-9-12
收藏
 问题点数:0 回复次数:0 
(求助)改写程序(Huffman编码)

希望大家帮忙把这个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;
}

搜索更多相关主题的帖子: Huffman include 结点 
2006-12-08 13:09
快速回复:(求助)改写程序(Huffman编码)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021301 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved