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

哈夫曼编码和译码c语言的源程序,第1张

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <malloc.h>

#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 intweight

unsigned intparent,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=1i<=endi++)

{

if (HT[i].parent==0&&HT[i].weight<min1)

{

*s1=i

min1=HT[i].weight

}

}

min2=MAX_NUM

for(i=1i<=endi++)

{

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<=1) return Hfm

m=2*n-1

for(i=n+1i<=m++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=1i<=n++i)

{

start=n-1

for(c=i,f=Hfm.HT[i].parentf!=0c=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=1i<=ni++)

{

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<=2*n-1++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=1i<=Hfm.longthi++)

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=1i<=ni++)

fscanf(fp,"%s %d",&Hfm.c[i],&Hfm.HT[i].weight)

for(i=1i<=ni++)

{

Hfm.HT[i].parent=0

Hfm.HT[i].lchild=0

Hfm.HT[i].rchild=0

}

for(i<=2*n-1++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=1i<=ni++)

{

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=1i<=ni++)

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=1i<=Hfm.longthi++)

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 choice='a'

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()

Decoding(Hfm)

printf("\n\n*******Please enter anykey to continue********\n")

getch()break

case 'c':

clrscr()

Output(Hfm)

printf("\n\n*******Please enter anykey to continue********\n")

getch()break

case 'd':

clrscr()

Hfm=RebuildHuffman(Hfm)

printf("\n\n********************Initial*********************\n")

getch()break

case 'q':

break

default:

printf(" Your choice is wrong!\n Please enter anykey to choice again!\n")

getch()break

}

}

return 0

}

网件精灵安卓版是由美国网件公司(NETGEAR)推出的手机路由器管理app。通过APP,就可以轻松访问路由器的功能,还可以远程 *** 控路由器,管理连接到路由器的设备。赶快下载体验吧!

软件介绍

网件精灵NETGEAR Genie 是网件(NETGEAR)公司的路由器管理软件,支持所有网件(NETGEAR)路由器.

使用 NETGEAR 精灵可以很容易的管理、监测和修复您的家庭网络。精灵可以自动修复常见的无线网络问题。精灵让您轻松访问路由器的功能,如家长控制、访客网络、无线频道、 速度测试等。您可以使用网络橘枝辩映射查看连接到您的家庭网络的所有设备。

NETGEAR 精灵与任何 NETGEAR 的路由器相兼容。如要通过精灵管理路由器 设置,您可能需要更新路由器固件到最新版本。

软件特色

1. *** 搭桐作界面非常简单,所有的信息均采用了图形化设计

2.可圆缺以轻松的设置家长管理和来宾模式的管理等

3.网络地图更是可以看出与路由器连接的设备的以及 *** 作系统

功能介绍

使用MyMedia 通过智能手机或平板电脑远程控制您家庭网络中的多媒体

免费的NETGEAR Andriod端管理程序,网络监管软件

查看连接到家庭网络中的所有设备

使用AirPrint 通过Andriod 打印


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12351448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存