数据结构课程设计-赫夫曼编码 联系客服

发布时间 : 星期三 文章数据结构课程设计-赫夫曼编码更新完毕开始阅读2fb9039084868762caaed5dd

{

ifstream iFile1(\

if(!iFile1) {

cout<<\对不起您未建哈夫曼树同时文件中也不存在!\ return ; }

char ch,th,str1[20]; iFile1>>leaves;

h=(HfmTeee)malloc((leaves+1)*sizeof(HtNode));

code=(HuffCode)malloc((leaves+1)*sizeof(char *)); int ant=1;

while(iFile1.peek()!=EOF) {

iFile1.get(ch);//接收上一行的换行符 iFile1.get(ch); iFile1>>th>>str1; int l=strlen(str1); h[ant].ch=ch;

code[ant]=(char*)malloc((l+1)*sizeof(char)); strcpy(code[ant++],str1); }

iFile1.close(); }

ifstream iFile(\if(!iFile) {

cout<<\当前无可译的代码!\ return ; }

ofstream oFile(\if(!oFile) {

cout<<\您还未构建哈夫曼树,请先建树!\ return ; }

iFile>>str;

while(iFile.peek()!=EOF)

{ int k=0;

int ant=0; while(str[k]!='\\0') { for(int i=1;i<=leaves;i++)

13

{ int x=k; int j; for(j=0;j

//res[ant++]=h[i].ch ; k=k+strlen(code[i]);

break; } } }

oFile<>str; }

iFile.close(); oFile.close();

cout<<\编译完成,编译后的结果已经存入Textfile.txt中\}

4、打印代码文件

//-------------------------------------打印代码文件-------------------------------------- void Print() {

ifstream iFile(\ if(!iFile) {

cout<<\文件不存在或未找到药打印的代码!\ return ; }

cout<<\输出CodeFile.txt中的代码,每行50个:\string str; iFile>>str; int ant=1;

while(iFile.peek()!=EOF) {

int l=str.length(); for(int i=0;i

cout<

14

}

cout<

iFile>>str; }

cout<

5、主函数

//-------------------------------------主函数--------------------------------------

void main() { HfmTeee h; HuffCode code; int n=0; char choice='a'; cout<<\

cout<<\计科(1)班\程方远\\n\

while(choice!='Q'&&choice!='q') { cout<<\赫夫曼编码/译码器*************************\\n\

cout<<\ \ \\

cout<<\请输入您要操作的步骤:\cin>>choice; switch(choice) { case 'I':case 'i':

cout<<\请输入字符个数:\ cin>>n; getchar(); CreateHfmTree(h,code,n);

ok=1; break; case 'E':case 'e': Encoding(h,code,n); break; case 'D':case 'd': Decoding(h,code,n); break;

case 'P':case 'p':

15

Print(); break;

case 'Q':case'q': break; } } }

16