- Visual C++源码
- Visual Basic源码
- C++ Builder源码
- Java源码
- Delphi源码
- C/C++源码
- PHP源码
- Perl源码
- Python源码
- Asm源码
- Pascal源码
- Borland C++源码
- Others源码
- SQL源码
- VBScript源码
- JavaScript源码
- ASP/ASPX源码
- C#源码
- Flash/ActionScript源码
- matlab源码
- PowerBuilder源码
- LabView源码
- Flex源码
- MathCAD源码
- VBA源码
- IDL源码
- Lisp/Scheme源码
- VHDL源码
- Objective-C源码
- Fortran源码
- tcl/tk源码
- QT源码
HUFFMAN.CPP
资源名称:HUFFMAN.zip [点击查看]
上传用户:lzq06628
上传日期:2021-04-24
资源大小:3k
文件大小:9k
源码类别:
压缩解压
开发平台:
C/C++
- #include <stdio.h>
- #include <stdlib.h>
- #include <dir.h>
- #include <io.h>
- #include <conio.h>
- #include <string.h>
- #include <ctype.h>
- #include <dos.h>
- struct node
- {
- unsigned char c;
- unsigned long int freq;
- struct node *up,*left,*right;
- };
- struct ftable
- {
- unsigned long int freq;
- };
- struct ftable ft;
- struct node leaf[256];
- struct node *t[256];
- struct node *a,*b;
- int i,j,k;
- int buf[50],bc;
- int buft[20],bct;
- int buftv[20];
- unsigned char q;
- long int y=0;/*For debugging*/
- int init(char *);
- int sortt(int);//
- int maketree();//
- void add2buffCom(int);
- void add2buffDec(int);
- void refreshbufferCom(FILE *);
- void refreshbufferDec(FILE *);
- int nodecmp(struct node *,struct node *);
- int nodecpy(struct node *,struct node *);
- int getzero();//
- unsigned char oddchar();
- char *getname(char *);
- void init();
- int retrieveft(char *);
- int getzero()
- {
- int h=0;
- for(i=0;i<256;i++)
- {
- if(leaf[i].freq==0)
- h++;
- }
- return (255-h);
- }
- char *getname(char *filepath)
- {
- char drive[4],dir[67],file[15],ext[5];
- fnsplit(filepath,drive,dir,file,ext);
- strcat(file,ext);
- return file;
- }
- unsigned char oddchar()
- {
- for(i=bc;i<8;i++)
- {
- buf[i]=0;
- }
- return ((1*buf[7])+(2*buf[6])+(4*buf[5])+(8*buf[4])+(16*buf[3])+(32*buf[2])+(64*buf[1])+(128*buf[0]));
- }
- int nodecmp(struct node *a,struct node *b)
- {
- if(a->c==b->c && a->freq==b->freq && a->up==b->up &&a->left==b->left && a->right==b->right)
- return 0;
- return -1;
- }
- int nodecpy(struct node *a,struct node *b)
- {
- a->c=b->c;
- a->freq=b->freq;
- a->up=b->up;
- a->left=b->left;
- a->right=b->right;
- return 0;
- }
- int init(char *filename) //Membuka file//
- {
- FILE *p;
- for(i=0;i<256;i++) //deklarasi awal tree//
- {
- leaf[i].c=i;
- leaf[i].freq=0;
- leaf[i].up=NULL;
- leaf[i].left=NULL;
- leaf[i].right=NULL;
- }
- p=fopen(filename,"rb");
- if(p==NULL)
- {
- return -1; /*Could not open file */
- }
- while(j=fgetc(p),j!=EOF)
- {
- leaf[j].freq++;
- if(j<0 || j>255)
- {
- printf("nError...");
- getch();
- }
- }
- fclose(p);
- for(i=0;i<256;i++)
- {
- t[i]=&leaf[i];
- if((t[i]->up)!=NULL)
- {
- printf("nError..");
- getch();
- }
- }
- bc=0; /*Setting the global buffer counter to 0*/
- return 0;
- }
- int sortt(int z)/*sorts upto t[z] and not t[z-1]*/
- {
- for(k=0;k<=z;k++)
- for(j=(k+1);j<=z;j++)
- {
- if((t[k]->freq)<(t[j]->freq))
- {
- //b=NULL;
- b=t[k];
- t[k]=t[j];
- t[j]=b;
- }
- }
- return 0;
- }
- int maketree()
- {
- sortt(255);
- for(i=getzero();i>0;i--)
- {
- sortt(i);
- a=NULL;
- a=(struct node *)malloc(sizeof(struct node));
- if(a==NULL)
- {
- printf("nMemory allocation error...");
- getch();
- return -1; /*Memory allocation error*/
- }
- /*Assingning values*/
- a->freq=(t[i]->freq)+(t[i-1]->freq);
- a->right=t[i];
- a->left=t[i-1];
- a->up=NULL;
- a->c='';
- t[i]->up=a;
- t[i-1]->up=a;
- /*Changing the pointer*/
- t[i-1]=a;
- }
- return 0;
- }
- void add2buffCom(int r)
- {
- bct=-1;
- if(r>255 || r<0)
- {
- printf("nValue error...");
- getch();
- }
- a=&leaf[r];
- while((a->up)!=NULL)
- {
- if(nodecmp((a->up->left),a)==0)
- {
- buft[++bct]=0;
- }
- else if(nodecmp((a->up->right),a)==0)
- {
- buft[++bct]=1;
- }
- else
- {
- printf("nParent Error"); /*For debugging*/
- }
- a=a->up;
- }
- for(i=0;i<=bct;i++)
- {
- buftv[bct-i]=buft[i];
- }
- for(i=0;i<=bct;i++)
- {
- buf[bc+i]=buftv[i];
- }
- bc=bc+bct+1;
- return; /*Successful completetion of function*/
- }
- void refreshbufferCom(FILE *p)
- {
- while(bc>=8)
- {
- q=(1*buf[7])+(2*buf[6])+(4*buf[5])+(8*buf[4])+(16*buf[3])+(32*buf[2])+(64*buf[1])+(128*buf[0]);
- if(fputc(q,p)!=(unsigned)q || q<0 || q>255) printf("nError");
- for(i=8;i<bc;i++)
- {
- buf[i-8]=buf[i];
- }
- bc-=8;
- }
- }
- void refreshbufferDec(FILE *p)
- {
- int count=0,j,i;
- a=t[0];i=0;
- for(i=0;i<=bc;i++)
- {
- if(a->left==NULL && a->right==NULL)
- {
- fputc(a->c,p);
- /*For debugging*/
- y++;
- /*debugging over*/
- for(j=count;j<bc;j++)
- {
- buf[j-count]=buf[j];
- }
- bc-=count;
- //i=0;
- count=0;
- a=t[0];
- }
- else if(buf[count]==0)
- {
- a=a->left;
- count++;
- //i++;
- }
- else if(buf[count]==1)
- {
- a=a->right;
- count++;
- //i++;
- }
- else
- {
- printf("nError");
- }
- }
- return;
- }
- void add2buffDec(int c)
- {
- bct=-1;
- while(c!=0)
- {
- buft[++bct]=(c%2);
- c/=2;
- }
- for(i=(bct+1);i<8;i++)
- {
- buft[i]=0;
- }
- for(i=(0);i<8;i++)
- {
- buftv[7-i]=buft[i];
- }
- for(i=0;i<8;i++)
- {
- buf[bc+i]=buftv[i];
- }
- bc+=8;
- }
- int retrieveft(char *filename)
- {
- FILE *p;
- p=fopen(filename,"rb");
- if(p==NULL)
- {
- return -1;/*Could not open file */
- }
- for(i=0;i<256;i++)
- {
- fread(&ft,sizeof(struct ftable),1,p);
- leaf[i].c=(unsigned char)i;
- leaf[i].freq=ft.freq;
- leaf[i].up=NULL;
- leaf[i].right=NULL;
- leaf[i].left=NULL;
- }
- fclose(p);
- return 0;
- }
- void init()
- {
- int i;
- for(i=0;i<256;i++)
- {
- leaf[i].c=(unsigned char)i;
- leaf[i].freq=0;
- leaf[i].up=NULL;
- leaf[i].left=NULL;
- leaf[i].right=NULL;
- t[i]=&leaf[i];
- }
- for(i=0;i<256;i++)
- {
- t[i]=&leaf[i];
- }
- return;
- }
- main()
- {
- FILE *p,*q;
- int ch,ct;
- char filename[100],encfile[100],outfile[100];
- long int filesize,encfilesize;
- long int filelen,count=1024;
- float ratio;
- char pil;
- while(pil!=27)
- {
- clrscr();
- printf("nHUFFMANn-------------------n");
- printf("[1]Compressionn[2]Decompressionn[ESC]exitn");
- pil=getch();
- if(pil=='C'||pil=='c')
- {
- clrscr();
- printf("nMasukkan Nama File yang akan dikompresi:");
- scanf("%s",filename);
- printf("nMasukkan Nama File hasil kompresi:");
- scanf("%s",encfile);
- if(init(filename)==-1)
- {
- printf("nError...Tidak bisa membuka file");
- fcloseall();
- return -1;
- }
- if(maketree()==-1)
- {
- printf("nAllokasi error");
- fcloseall();
- return -1;
- }
- q=fopen(encfile,"wb");
- if(q==NULL)
- {
- printf("nError...Tidak bisa membuka file");
- fcloseall();
- return -1;
- }
- p=fopen(filename,"rb");
- if(p==NULL)
- {
- printf("nError...Tidak bisa membuka file");
- fcloseall();
- return -1;
- }
- filesize=filelength(fileno(p));
- /****To write the decoding table */
- for(i=0;i<256;i++)
- {
- ft.freq=leaf[i].freq;
- fwrite(&ft.freq,sizeof(struct ftable),1,q);
- }
- /*To write the character that denotes the size of filenamelength*/
- fputc(strlen(getname(filename))+1,q);
- /*To write the filename*/
- fwrite(getname(filename),strlen(getname(filename))+1,1,q);
- /***Completed writing of decoding table*****/
- while(ch=fgetc(p),ch!=EOF)
- {
- add2buffCom(ch);
- refreshbufferCom(q);
- }
- fputc(oddchar(),q);
- fputc(bc,q);
- fcloseall();
- printf("Kompresi Sukses");
- /*****For display of compression summary****/
- p=fopen(encfile,"rb");
- if(p==NULL)
- {
- printf("nHasil Kompresi tidak bisa ditampilkan");
- return -1;
- }
- encfilesize=filelength(fileno(p));fcloseall();
- printf("nnInformasi Kompresin");
- printf("nInput filesize :%ld bytes",filesize);
- printf("nOutput filesize:%ld",encfilesize);
- ratio=(float)(encfilesize*100/filesize);
- printf("nHasil Kompresi %.3f%% dari hasil asli",ratio);
- getch();
- }else if(pil=='D'||pil=='d')
- {
- printf("nMasukkan file yang ingin didekompres:");scanf("%s",filename);
- init();
- if(retrieveft(filename)==-1)
- {
- printf("nTidak bisa membuka file");
- return -1;
- }
- maketree();
- /*To retrieve the filename from the compressed file*/
- p=fopen(filename,"rb");
- fseek(p,1024,SEEK_SET);
- ct=fgetc(p);
- fread(outfile,ct,1,p);
- /***Filename retrieval finished*/
- p=fopen(filename,"rb");
- /***check for user renaming of output file*/
- printf("nFile yang akan didekompres mengandung file asli: %s",outfile);
- {
- printf("nnMasukkan nama file hasil dekompresi:");scanf("%s",outfile);
- }
- /******check for renaming complete*****/
- q=fopen(outfile,"wb");
- if(q==NULL || p==NULL)
- {
- printf("nTidak bisa membuka file");
- fcloseall();
- return -1;
- }
- fseek(p,256*sizeof(struct ftable)+1+ct,SEEK_SET);
- filelen=filelength(fileno(p));
- count=1024+1+ct;
- while(ch=fgetc(p),count++,ch!=EOF)
- {
- if(count==(filelen-1))
- {
- add2buffDec(ch);
- bc-=8;
- bc+=fgetc(p);
- refreshbufferDec(q);
- while(bc!=0)
- {
- refreshbufferDec(q);
- }
- }
- else
- {
- add2buffDec(ch);
- refreshbufferDec(q);
- }
- }
- printf("nDecompression complete.n");
- printf("nOutput file tercipta %s ",outfile);
- getch();
- }
- }
- return -1;
- }