三九宝宝网宝宝教育教学论文

哈夫曼编码译码C语言编写

03月15日 编辑 39baobao.com

[编写c语言二维数组]一: #include <stdio.h> void main(){ int i,j; int a[5][3]; for(i=0;i<5;i++) for(j=0;j<3;j++){ scanf("%d",&a[i][j]); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf("%d...+阅读

#include #include #define null 0#define ok 1#define error 0#define overflow -2#define max_num 10000#define max 60 typedef int status; typedef char **huffmancode; typedef struct{ unsigned int weight; unsigned int parent,lchild,rchild; }htnode,*huffmantree; typedef struct{ huffmantree ht; char *c; int longth; huffmancode hc; }huffman; void select(huffmantree ht,int end,int *s1,int *s2) { int i; int min1=max_num; int min2; for (i=1;i { if (ht[i].parent==0&ht[i].weight { *s1=i; min1=ht[i].weight; } } min2=max_num; for(i=1;i { if(ht[i].parent==0&(*s1!=i)&min2>ht[i].weight) { *s2=i; min2=ht[i].weight; } } } huffman huffmancoding(huffman hfm) { /*---------------------------------*/ int i,n,m,s1,s2,start; int c,f; char *cd; n=hfm.longth; if(n m=2*n-1; for(i=n+1;i { select(hfm.ht,i-1,&s1,&s2); hfm.ht[s1].parent=i; hfm.ht[s2].parent=i; hfm.ht[i].lchild=s1; hfm.ht[i].rchild=s2; hfm.ht[i].weight=hfm.ht[s1].weight+hfm.ht[s2].weight; } /*------------------------------------------*/ hfm.hc=(huffmancode)malloc((n+1)*sizeof(char *)); cd=(char *)malloc(n*sizeof(char)); cd[n-1]='\0'; for(i=1;i { start=n-1; for(c=i,f=hfm.ht[i].parent;f!=0;c=f,f=hfm.ht[f].parent) { if(c==hfm.ht[f].lchild) cd[--start]='0'; else cd[--start]='1'; } hfm.hc[i]=(char *)malloc((n-start)*sizeof(char)); strcpy(hfm.hc[i],&cd[start]); } free(cd); return hfm; } huffman inputhuffman(huffman hfm) { int i,n; clrscr(); printf("\n\n\t\t********************initial*********************\n"); printf("\tthe chars and weights will be saved in the file \"hfmtree\"\n"); printf("please input the number of the chars: "); scanf("%d",&n); hfm.ht=(huffmantree)malloc((2*n)*sizeof(htnode)); hfm.c=(char *)malloc((n+1)*sizeof(char)); for(i=1;i { printf("please input the char: "); scanf("%s",&hfm.c[i]); printf("please input the weight of the char: "); scanf("%d",&hfm.ht[i].weight); hfm.ht[i].parent=0; hfm.ht[i].lchild=0; hfm.ht[i].rchild=0; } for(;i { hfm.ht[i].weight=0; hfm.ht[i].parent=0; hfm.ht[i].lchild=0; hfm.ht[i].rchild=0; } hfm.longth=n; return hfm; } huffman inithuffman(huffman hfm) { int n,i; file *fp; fp=fopen("hfmtree","rt"); if(fp==null) { hfm=inputhuffman(hfm); fp=fopen("hfmtree","wt"); fprintf(fp,"%d\n",hfm.longth); for(i=1;i fprintf(fp,"%c %d",hfm.c[i],hfm.ht[i].weight); rewind(fp); } else { fscanf(fp,"%d\n",&n); hfm.c=(char *)malloc((n+1)*sizeof(char)); hfm.ht=(huffmantree)malloc((2*n)*sizeof(htnode)); for(i=1;i fscanf(fp,"%s %d",&hfm.c[i],&hfm.ht[i].weight); for(i=1;i { hfm.ht[i].parent=0; hfm.ht[i].lchild=0; hfm.ht[i].rchild=0; } for(;i { hfm.ht[i].weight=0; hfm.ht[i].parent=0; hfm.ht[i].lchild=0; hfm.ht[i].rchild=0; } hfm.longth=n; } fclose(fp); hfm=huffmancoding(hfm); return hfm; } void output(huffman hfm) { int i,n; n=hfm.longth; printf("\n\n******************output the code of the chars****************\n\n"); for(i=1;i { printf("\n"); printf("char: %c\t",hfm.c[i]); printf("weight: %d\t",hfm.ht[i].weight); printf("code: "); puts(hfm.hc[i]); } } void encoding(huffman hfm) { int i=0,j=0,n; char ch[max]; file *fp,*ffp; n=hfm.longth; printf("\n\n*******************encoding**************************\n\n"); if((ffp=fopen("tobetran","rt"))==null) { printf("\nplease input the sentence: "); scanf("%s",&ch); printf("\n"); fp=fopen("codefile","wt+"); } else { fscanf(ffp,"%s",ch); fclose(ffp); } while(ch[j]) { for(i=1;i if(ch[j]==hfm.c[i]) { printf("%s",hfm.hc[i]); fprintf(fp,"%s",hfm.hc[i]); break; } j++; } rewind(fp); fclose(fp); } void decoding(huffman hfm) { huffmantree p; int i,n; int j=0; char d[50]; file *fp; n=hfm.longth; printf("\n\n******************decoding************************\n\n"); if((fp=fopen("codefile","rt"))==null) { printf("please input the code:"); scanf("%s",&d); } else { fscanf(fp,"%s",d); fclose(fp); } printf("\nthe file is : "); fp=fopen("textfile","wt+"); while(d[j]) { p=&hfm.ht[2*n-1]; while(p->lchild||p->rchild) { if(d[j]=='0') { i=p->lchild; p=&hfm.ht[i]; } else { i=p->rchild; p=&hfm.ht[i]; } j++; } printf("%c",hfm.c[i]); fprintf(fp,"%c",hfm.c[i]); } fclose(fp); } huffman rebuildhuffman(huffman hfm) { int n,i; file *fp; fp=fopen("hfmtree","wt"); hfm=inputhuffman(hfm); fprintf(fp,"%d\n",hfm.longth); for(i=1;i fprintf(fp,"%c %d",hfm.c[i],hfm.ht[i].weight); rewind(fp); fclose(fp); hfm=huffmancoding(hfm); return hfm; } int main() { huffman hfm; char; hfm=inithuffman(hfm); while(choice!='q') { clrscr(); printf("\n\n\n\t\t*************************************\n\n"); printf("\t\t\ta. encoding:\n\n"); printf("\t\t\tb. decoding:\n\n"); printf("\t\t\tc. print all codes:\n\n"); printf("\t\t\td. rebuild the huffmantree:\n\n"); printf("\t\t\tq. quit...\n\n"); printf("\n\t\t************************************\n\n"); printf("please enter your choice: "); scanf("%s",&choice); switch(choice) { case 'a': clrscr(); encoding(hfm); printf("\n\n*******please enter anykey to continue*******\n"); getch(); break; case 'b': clrscr(); ...

哈夫曼编码和译码c语言的源程序

/*文件名:exp7-6.cpp*/ #include #include #define N 50 /*叶子结点数*/ #define M 2*N-1 /*树中结点总数*/ typedef struct { char data[5]; /*结点值*/ int weight; /*权重*/ int parent; /*双亲结点*/ int lchild; /*左孩子结点*/ int rchild; /*右孩子结点*/ } HTNode; typedef struct { char cd[N]; /*存放哈夫曼码*/ int start; } HCode; void CreateHT(HTNode ht[],int n) { int i,k,lnode,rnode; int min1,min2; for (i=0;i<2*n-1;i++) /*所有结点的相关域置初值-1*/ ht[i].parent=ht[i].lchild=ht[i].rchild=-1; for (i=n;i<2*n-1;i++) /*构造哈夫曼树*/ { min1=min2=32767; /*lnode和rnode为最小权重的两个结点位置*/ lnode=rnode=-1; for (k=0;k<=i-1;k++) if (ht[k].parent==-1) /*只在尚未构造二叉树的结点中查找*/ { if (ht[k].weight

huffman编码译码

呵呵,你所有的要求我都有,但是是C++版本的~~没法帮你了

输入字符串:

OHOLE!!LOEH

字符频率统计及相应的哈夫曼编码:

O: 3: 10

H: 2: 00

L: 2: 111

E: 2: 110

!: 2: 01

哈夫曼树打印:

------------2

------4

------------2

11

------------3

------7

------------------2

------------4

------------------2

输入电码:(注意此处请输入标准01电码,不作其他检验)

001101111111001

译码结果:

HELLO!

请按任意键继续. . .

以下为关联文档:

C语言程序编写此题应该属于约瑟夫问题范围。 语句注释,我习惯用了//,请楼主注意哈。 #include"stdlib.h" #include"stdio.h" #define NULL 0 typedef struct point { int data; int NO; struct...

用C语言编写函数/* 1-1/2+1/3-1/4+……+pow(-1,n)*1/n */#include#includevoid JiSuan(int);main(){ int n; printf("n="); scanf("%d",&n); JiSuan(n); getch();}void JiSuan(int n){ double r...

C语言编写函数?在C语言中,一个标准的函数定义语句块必须包含函数返回值的类型标识符、函数名、形参类型及数量、函数体、返回值表达式。如果函数返回值类型为 void (即无返回值),则在两个大括...

编写一个C语言函数typedef unsigned char BYTE8; void setbit(BYTE8 bits[], int index) { int a = index>>3;/*计算字节数*/ int b = index&7;/*计算字节内的位数*/ BYTE8 mask=0x80;/*第一位是...

C语言编写函数int func1(int a,int b) { if(a!=0)return func1(b%a,a); return b; } int func2(int a,int b) {int s,sum=0; if(a<b) { s=b; b=a; a=s; } for(int i=(0==a%2?a+1:a);i<=b;...

c语言编写快速排序int partition(int n[],int left,int right) //int类型的方法,传入参数(int类型的 数组n,int类型的参数left和right) { int lo,hi,pivot,t;//定义int类型的变量 pivot=n[left];//...

c语言中汉字使用什么编码方式?易语言简称:e、ve、e语言 最新版本:5.11 正式版(支持静态编译) 特色:第一个中国人自己打造的编程语言! ★全中文支持,无需跨越英语门槛。 ★全可视化编程,支持所见即所得程序界面设...

c语言编写实例#includevoid main() { double number1 = 0.0; /* 第一个操作数 */ double number2 = 0.0; /* 第二个操作数 */ char operation = 0; /* 操作运算符*/ printf("\nEnter the calc...

C语言和汉字编码的问题求教为了区别于普通ASCII码,汉字编码一般在ASCII码之外,或者是两字节的第一字节在其之外,方法就是在汉字区位码上加一个固定的数字。但汉字有多套编码,互相不通用,可汉字编码了解详细...

推荐阅读
图文推荐