⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nen.c

📁 nen du lieu nhanh chong
💻 C
字号:
/*# include */
# include <stdio.h>
# include <stdlib.h>
# include <dir.h>
# include <io.h>


/*Khai bao bien toan cuc ***/
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[15],bct;
int buftv[15];
unsigned char q;


/*Khai bao ham*/
int init(char *);
int sortt(int);
int maketree();
void add2buff(int);
void refreshbuffer(FILE *);
int nodecmp(struct node *,struct node *);
int nodecpy(struct node *,struct node *);
int getzero();
unsigned char oddchar();
char *getname(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)
{
	FILE *p;
	for(i=0;i<256;i++)
	{
		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; /*Khong the mo file*/
	}
	while(j=fgetc(p),j!=EOF)
	{
		leaf[j].freq++;
		if(j<0 || j>255)
		{
			printf("\nLoi...");
                        getch();
		}
	}
	fclose(p);
	for(i=0;i<256;i++)
	{
		t[i]=&leaf[i];
		if((t[i]->up)!=NULL)
		{
			printf("\nLoi..");
			getch();
		}
	}
	bc=0;  
	return 0;
}

int sortt(int z)/*Sap xep 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("\nCap phat bo nho loi...");
			getch();
			return -1;  /*Loi cap phat vung nho*/
		}
		/*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='\0';
		t[i]->up=a;
		t[i-1]->up=a;
		/*Thay doi con tro*/
		t[i-1]=a;
	}
	return 0;
}

void add2buff(int r)
{
	bct=-1;
	if(r>255 || r<0)
	{
		printf("\nLoi gia tri...");
		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("\nLoi");  /*sua loi*/
		}
		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;  /*hoan tat ham*/
}

void refreshbuffer(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("\nLoi");
		for(i=8;i<bc;i++)
		{
			buf[i-8]=buf[i];
		}
		bc-=8;
	}
}

main()
{
	FILE *p,*q;
	int ch;
	char filename[100],encfile[100];
	long int filesize,encfilesize;
	float ratio;
	clrscr();
	printf("+------------------------------------------------------+\n");
	printf("|             Lop Dien Tu K32                          |\n");
	printf("|                Nhom: 13			       |\n");
	printf("|          Thanh vien trong nhom: 		       |\n");
	printf("|       				               |\n");
	printf("|       Nguyen Van Can         1063715                 |\n");
	printf("|       Ngo Minh Dan           1063717                 |\n");
	printf("| 	Phi Thai Duy           1063719 	               |\n");
	printf("|       Nguyen Ngoc Tu         1063776                 |\n");
	printf("|       Le Thi Ngoc Nguyen     1063813                 |\n");
	printf("|       Nguyen Huu Tri         1063833                 |\n");	
	printf("+------------------------------------------------------+\n");
	printf("\nNhap ten file can nen (ton tai trong thu muc hien hanh): ");
	scanf("%s",filename);
	printf("\nNhap ten file sau khi nen: ");
	scanf("%s",encfile);
	if(init(filename)==-1)
	{
		printf("\nKhong the mo file can nen..");
		getch();
		fcloseall();
		return -1;
	}
	printf("\nKhoi tao hoan tat.\nChuan bi nen...");
	if(maketree()==-1)
	{
		printf("\nLoi cap phat vung nho..");
		fcloseall();
		return -1;
	}
	q=fopen(encfile,"wb");
	if(q==NULL)
	{
		printf("\nKhong the mo file..");
		fcloseall();
		return -1;
	}
	p=fopen(filename,"rb");
	if(p==NULL)
	{
		printf("\nKhong the mo file...");
		fcloseall();
		return -1;
	}
	filesize=filelength(fileno(p));
	/****Viet bang ma  */
	for(i=0;i<256;i++)
	{
		ft.freq=leaf[i].freq;
		fwrite(&ft.freq,sizeof(struct ftable),1,q);
	}
	fputc(strlen(getname(filename))+1,q);
	/*Viet ten file*/
	fwrite(getname(filename),strlen(getname(filename))+1,1,q);
	/***Hoan tat viet bang ma*****/
	printf("\nDang nen...");
	while(ch=fgetc(p),ch!=EOF)
	{
		add2buff(ch);
		refreshbuffer(q);
	}
	fputc(oddchar(),q);
	fputc(bc,q);
	fcloseall();
	printf("\Nen file hoan tat.");
	/*****Hien thi ket qua****/
	p=fopen(encfile,"rb");
	if(p==NULL)
	{
		printf("\nKhong the mo file  ");
		printf("\nKhong the hien thi ket qua nen");
		return -1;
	}
	encfilesize=filelength(fileno(p));fcloseall();
	printf("\n\nKet qua nen:\n");
	printf("\nDung luong file truoc khi nen :%ld bytes",filesize);
	printf("\nDung luong file sau khi nen:%ld bytes",encfilesize);
	ratio=(float)(100-encfilesize*100/filesize);
	printf("\nDa nen %.3f%% so voi file goc.",ratio);
	getch();
	return 0;
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -