Đây là đoạn code mà mình đã viết,tuy nhiên mình thắc mắc làm sao để đưa Chuối Huffman sau khi mã hóa,tức từng kí tự trong chuỗi mã đều được đưa ra màn hình ứng với các kí tự trong file nguồn.Code:// Huffman coding.cpp : Defines the entry point for the console application. // #include<StdAfx.h> #include<stdio.h> #include <iostream> #include<string.h> using namespace std; int solantrai=0; int solanphai=0; char ma[50]=""; struct Node { char c; char ma[100]; int tansuat; Node* trai; Node* phai; Node* tren; int duyettrai; int duyetphai; }; void LNR(Node *root) { if (root != NULL) { LNR(root->trai); cout<<root->c<<"haha"; LNR(root->phai); } } Node boma[100],*boma1[100],*list; int sophantu; void duyetcay(Node *node) { char temp[100]=""; int c; while(1) { if (node!= NULL) { if(node->trai==NULL||node->duyettrai==1) {if(node->phai==NULL||node->duyetphai==1) node=node->tren; else {node->duyetphai=1; node=node->phai; strcpy(temp,node->tren->ma); strcat(temp,"1"); strcpy(node->ma,temp); for(int i=0;i<sophantu;i++) {if(boma[i].c==node->c) strcpy(boma[i].ma,node->ma);} } } else { node->duyettrai=1; node=node->trai; strcpy(temp,node->tren->ma); strcat(temp,"0"); strcpy(node->ma,temp); for(int i=0;i<sophantu;i++) {if(boma[i].c==node->c) strcpy(boma[i].ma,node->ma);} } if(node==NULL) break;} } } int duyetphai=0; void ThongKe(char *fileName); void TaoFileNhiPhan(char *filedauvao,char* filenhiphan); void KhoiPhuc(char* filenhiphan,char* filedaura); void main() { ThongKe("dauvao.text"); TaoFileNhiPhan("dauvao.text","FileNhiPhan.text"); KhoiPhuc("FileNhiPhan.text","FileKhoiPhuc.text"); for(int i=0;i<sophantu;i++) cout<<"ky tu: "<<boma[i].c<<" xuat hien: "<<boma[i].tansuat<<" lan"<<" ma nhi phan: "<<boma[i].ma<<endl; cout<<"chuoi ma sau khi nen"<< cout<<" Ket Thuc !\n"; cin.get(); } void ThongKe(char *fileName) { FILE *f=fopen(fileName,"rb");//Đọc file int i=0; sophantu=0; while(1) {bool daco=false; char tam; fread(&tam,sizeof(tam),1,f); if(feof(f))break; if (sophantu==0){sophantu=1; boma[0].c=tam; boma[0].tansuat=1; boma[0].phai=NULL; boma[0].trai=NULL; boma1[0]=new Node; boma1[0]->c=tam; boma1[0]->tansuat=1; boma1[0]->phai=NULL; boma1[0]->trai=NULL; boma1[0]->duyetphai=0; boma1[0]->duyettrai=0; } else { daco=false; for(i=0;i<sophantu;i++) if (boma[i].c==tam) {boma[i].tansuat++; boma1[i]->tansuat++; daco=true;} if (!daco) { boma[sophantu].c=tam; boma[sophantu].tansuat=1; boma[sophantu].phai=NULL; boma[sophantu].trai=NULL; boma1[sophantu]=new Node; boma1[sophantu]->c=tam; boma1[sophantu]->tansuat=1; boma1[sophantu]->phai=NULL; boma1[sophantu]->trai=NULL; boma1[0]->duyetphai=0; boma1[0]->duyettrai=0; sophantu++;} } } fclose(f); } void TaoFileNhiPhan(char *filedauvao,char* filenhiphan) { Node tam; Node* contro=new Node(); // sap xep theo thu tu giam dan so lan suat hien tung chu cai for(int i=0;i<sophantu-1;i++) for(int j=i+1;j<sophantu;j++) if (boma[j].tansuat>boma[i].tansuat) {contro=new Node(); tam=boma[i]; boma[i]=boma[j]; boma[j]=tam; contro=boma1[i]; boma1[i]=boma1[j]; boma1[j]=contro; } int so=sophantu; if(so==0){boma1[0]=NULL;} for(int n=0;n<sophantu-1;n++) {if (so<2)break; for(int i=0;i<so-1;i++) for(int j=i+1;j<so;j++) if (boma1[j]->tansuat>boma1[i]->tansuat) {contro=boma1[i]; boma1[i]=boma1[j]; boma1[j]=contro; } contro=new Node(); contro->phai=boma1[so-1]; contro->trai=boma1[so-2]; boma1[so-1]->tren=contro; boma1[so-2]->tren=contro; contro->tansuat=boma1[so-1]->tansuat+boma1[so-2]->tansuat; boma1[so-2]=contro; so--; } boma1[0]->tren=NULL; // gan cac chu da suat hien trong bai voi chuoi bit nhi phan; list=boma1[0]; duyetcay(list); // ghi ra file nhi phan FILE *f1,*f2; f1=fopen(filedauvao,"rb");//doc file dauvao f2=fopen(filenhiphan,"wt");//Ghi ra File nhi phan while(1) {char tam; fread(&tam,sizeof(tam),1,f1); if(feof(f1))break; for(int i=0;i<sophantu;i++) if(tam==boma[i].c) fprintf(f2,"%s",boma[i].ma); } fclose(f1); fclose(f2); } void KhoiPhuc(char* filenhiphan,char* filedaura) { FILE *f1,*f2; char temp; char chuoi[100]=""; int j=0; f1=fopen(filenhiphan,"rb");//doc file nhi phan f2=fopen(filedaura,"wt");//Ghi ra file daura while(1) { lable: fread(&temp,sizeof(temp),1,f1); { if(feof(f1))break; chuoi[j]=temp; j++; for(int i=0;i<sophantu;i++) {if(strcmp(boma[i].ma,chuoi)==0) { fprintf(f2,"%c",boma[i].c); for(int k=0;k<j;k++) chuoi[k]=NULL; j=0; goto lable; } } } } fclose(f1); fclose(f2); }
Mình đã tạo hàm xuất các kí tự đó ra một file txt ,nhưng thầy mình muốn nó còn xuất ra màn hình nữa,mình không biết chèn thêm lệnh gì vào để ra
Mình cũng đã đính kèm file nguồn vào trong đây