以下源代碼:(會回圈輸出)
#include <iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class Node
{
public:
Node();
~Node();
char ch;
Node(int w,char c):ch(c),weight(w)
{
left=right=next=NULL;
isReal=true;
}
private:
char code;
int weight;
Node *next;
Node *left;
Node *right;
bool isReal;
friend class HaFuMan;
};
Node::Node()
{
left=right=next=NULL;
isReal=true;
}
class HaFuMan
{
public:
HaFuMan(char*);
~HaFuMan();
bool printEnCode(char*ch)
{
Node*temp=new Node;
int len=strlen(ch);
if (len==0)return true;
for (char *p=ch; p<ch+len; p++)
{
temp->ch=*p;
if (!printPathCode(temp))
{
cout<<"......Error......"<<endl;
exit(0);
}
}
return true;
};
bool printPathCode(Node* s)
{
Node*b=nd->next;
Node* St[10];
Node* p;
int flag,top=-1;
do
{
while (b!=NULL)
{
top++;
St[top]=b;
b=b->left;
}
p=0;
flag=1;
while(top!=-1&&flag)
{
b=St[top];
if (b->right==p)
{
if (b->isReal&&b->ch==s->ch)
{
for (int i = 1; i <= top; i++)
cout<<St[i]->code;
//cout<<"@";
return true;
}
else
{
top--;
p=b;
}
}
else
{
b=b->right;
flag=0;
}
}
}
while (top!=-1);
return false;
}
bool isCharExits(Node*);
void printDeCode(char*);
void pre(Node*);
void mil(Node*);
Node* getStartNode()
{
return nd->next;
}
private:
Node *nd;
void deleteNode(Node*);
void insertNext(Node*temp)
{
Node*pre=nd;
Node*cur=nd->next;
while (cur!=NULL&&temp->weight>cur->weight)
{
pre=cur;
cur=cur->next;
}
temp->next=pre->next;
pre->next=temp;
}
void initTree()
{
Node* temp;
while(nd->next!=NULL)
{
if (nd->next->next==NULL)
{
if (nd->next->isReal==false)
{
nd->next->code='1';
}
return;
}
temp=new Node;
temp->isReal=false;
temp->weight=nd->next->weight;
temp->left=nd->next;
nd->next->code='1';
nd->next=nd->next->next;
if (nd->next!=NULL)
{
temp->weight+=nd->next->weight;
temp->right=nd->next;
nd->next->code='0';
nd->next=nd->next->next;
}
insertNext(temp);
}
}
int getChar(char*ch);
};
int HaFuMan::getChar(char*ch)
{
Node* temp=nd->next;
int len=strlen(ch);
for (int i = 0; i < len+1; i++)
{
if (temp->left!=NULL&&temp->left->code==ch[i])
{
temp=temp->left;
continue;
}
if (temp->right!=NULL&&temp->right->code==ch[i])
{
temp=temp->right;
continue;
}
if (temp->left==NULL&&temp->right==NULL)
{
cout<<temp->ch;
return i;
}
}
}
bool HaFuMan::isCharExits(Node* s)
{
Node*b=nd->next;
Node* St[10];
Node* p;
int flag,top=-1;
do
{
while (b!=NULL)
{
top++;
St[top]=b;
b=b->left;
}
p=0;
flag=1;
while(top!=-1&&flag)
{
b=St[top];
if (b->right==p)
{
if (b->isReal&&b->ch==s->ch)
{
return true;
}
else
{
top--;
p=b;
}
}
else
{
b=b->right;
flag=0;
}
}
}
while (top!=-1);
return false;
}
void HaFuMan::printDeCode(char*codes)
{
int len=strlen(codes);
int k=0;
while(k<len)
{
k+=getChar(&codes[k]);
}
cout<<endl;
};
HaFuMan::HaFuMan(char*path)
{
nd=new Node;
freopen(path,"r",stdin);
int count=0;
cin>>count;
int weight;
char chtemp;
for (int i = 0; i < count; i++)
{
cin>>chtemp>>weight;
insertNext(new Node(weight,chtemp));
}
initTree();
}
HaFuMan::~HaFuMan()
{
deleteNode(getStartNode());
}
void HaFuMan::deleteNode(Node *nd)
{
//if (nd)
//{
// deleteNode(nd->left);
// deleteNode(nd->right);
// delete nd;
//}
}
void HaFuMan::pre(Node*p)
{
if (p!=NULL)
{
if (p->isReal)
cout<<p->ch;
else
cout<<"@";
pre(p->left);
pre(p->right);
}
}
void HaFuMan::mil(Node*p)
{
if (p!=NULL)
{
mil(p->left);
if (p->isReal)
cout<<p->ch;
else
cout<<"@";
mil(p->right);
}
}
void printMenue();
void Operate(char*op,HaFuMan *);
int main()
{
HaFuMan *hafuman=new HaFuMan("in.txt");
while (true)
{
printMenue();
char op;
cin>>op;
}
return 0;
}
void Operate(char*op,HaFuMan *hafuman){
switch (*op)
{
case 'B':
case 'b':
{
cout<<"樹已經創建好了"<<endl;
break;
}
case 'T':
case 't':
{
cout<<"先序:";
hafuman->pre(hafuman->getStartNode());
cout<<endl;
cout<<"中序:";
hafuman->mil(hafuman->getStartNode());
cout<<endl;
break;
}
case 'E':
case 'e':
{
char* chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
int len=strlen(chars);
Node* forOutprint=new Node;
for (int i=0; i<len; i++)
{
forOutprint->ch=chars[i];
if (hafuman->isCharExits(forOutprint))
{
cout<<chars[i]<<" : ";
hafuman->printPathCode(forOutprint);
cout<<endl;
}
}
}
break;
case 'c':
case 'C':
{
char*q=new char;
cin>>*q;
cout<<*q;
hafuman->printEnCode(q);
break;
}
case 'D':
case 'd':
{
char*q=new char;
cin>>*q;
cout<<*q;
hafuman->printDeCode(q);
}
default:
cout<<"輸入有誤"<<endl;
break;
}
}
void printMenue()
{
cout<<"*****************輸入要進行的操作對應的字符****************\n"<<endl;
cout<<"B--建樹:讀入字符集和各字符頻度,建立哈夫曼樹\n\t格式為\ttotal\n\t\tchar weight\n\t\tchar weight \n\t\t......\n"<<endl;
cout<<"T--遍歷:先序和中序遍歷二叉樹\n"<<endl;
cout<<"E--生成編碼:根據已建立的哈夫曼樹,產生各字符的哈夫曼編碼\n"<<endl;
cout<<"C--編碼:輸入由字符集中字符組成的任意字串,利用已生成的哈夫曼編碼進行編碼\n顯示編碼結果,并將輸入的字串及其編碼結果分別保存在磁盤檔案textfile.txt和codefile.txt中\n"<<endl;
cout<<"D--譯碼:讀入codefile.txt,利用已建立的哈夫曼樹進行譯碼,并將譯碼結果存入磁盤檔案result.txt\n"<<endl;
cout<<"P--列印:螢屏顯示檔案textfile.txt、codefile.txt和result.txt\n"<<endl;
cout<<"X--退出\n"<<endl;
cout<<"***********************************************************"<<endl;
}
以下是對比檔案:(不會回圈輸出)
#include<iostream>
using namespace std;
void printMenue();
int main()
{
while (true)
{
printMenue();
char op;
cin>>op;
}
return 0;
}
void printMenue()
{
cout<<"*****************輸入要進行的操作對應的字符****************\n"<<endl;
cout<<"B--建樹:讀入字符集和各字符頻度,建立哈夫曼樹\n\t格式為\ttotal\n\t\tchar weight\n\t\tchar weight \n\t\t......\n"<<endl;
cout<<"T--遍歷:先序和中序遍歷二叉樹\n"<<endl;
cout<<"E--生成編碼:根據已建立的哈夫曼樹,產生各字符的哈夫曼編碼\n"<<endl;
cout<<"C--編碼:輸入由字符集中字符組成的任意字串,利用已生成的哈夫曼編碼進行編碼\n顯示編碼結果,并將輸入的字串及其編碼結果分別保存在磁盤檔案textfile.txt和codefile.txt中\n"<<endl;
cout<<"D--譯碼:讀入codefile.txt,利用已建立的哈夫曼樹進行譯碼,并將譯碼結果存入磁盤檔案result.txt\n"<<endl;
cout<<"P--列印:螢屏顯示檔案textfile.txt、codefile.txt和result.txt\n"<<endl;
cout<<"X--退出\n"<<endl;
cout<<"***********************************************************"<<endl;
}
uj5u.com熱心網友回復:
流被重定向至檔案未關閉uj5u.com熱心網友回復:
挽尊,那么長又沒標注重點,除非遇到個人心情超好時間超多又古道心腸也許會幫LZ看看,(多半也不會看代碼,頂多幫你除錯)uj5u.com熱心網友回復:
謝謝,不好意思,這是我第一次在這上面發問題,下次一定注意轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/130838.html
