用于存储记录过的字符数_第1页
用于存储记录过的字符数_第2页
用于存储记录过的字符数_第3页
用于存储记录过的字符数_第4页
用于存储记录过的字符数_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include "stdio.h"#include "conio.h"#include "string.h"#include "stdlib.h"#include "malloc.h"typedef struct code    char c;    int weight;CODE;CODE s128; /

2、*用于存储记录过的字符数*/typedef struct node    char c;    int tag;          /*是否有双亲的标志*/    int weight;    char code100;     

3、    /*该字符的huffman编码*/    struct node *parents,*lc,*rc;NODE;NODE *head;char filename140=0;                 /*源文件名*/char filename240=0;   

4、;              /*统计结果文件名称*/char filename340=0;                 /*压缩文件名称*/char filename440=0;       &

5、#160;         /*解压后源文件名称*/int n;/*保存统计结果保存函数*/void save(void)    int i;    FILE *fp;    printf("nPlease input the huffcode file path:");

6、0;   gets(filename2);    if(fp=fopen(filename2,"wt")=NULL)            printf("Can't open creat the huffcode file!nPlease cheak the flie!");&#

7、160;       getch();        return         n=0;    for(i=1;i<=128;i+)            if(si.weight=0)&#

8、160;           continue;        n+;                    /*不存在的元素不输出*/      &

9、#160; fprintf(fp,"%d",n);    s10.c='' /*因为回车在输出到huffcode中后不便于后面的读取,所以用英文中几乎不出现的字符  来代替会回车*/    s32.c='' /*空格在读取字符时会出错,用代替*/    s9.c=127;    for(i=1;i<=128;i+) &#

10、160;          if(si.weight=0)            continue;          /*过滤权值非0字符*/        fprintf(fp,"

11、60;%c %d",si.c,si.weight);         fclose(fp);/*建树模块*/NODE *BuildTree(void)    int min1=32767,min2=32767,i,j;    NODE *p1,*p2;    char cd20;    F

12、ILE *fp;        if(fp=fopen(filename2,"rt")=NULL)            printf("Sorry!the code file can't be opened!nPlease check it!");  

13、;      exit(0)         fscanf(fp,"%d",&n);    head=(NODE *)malloc(2*n*sizeof(NODE);/*多申请一个空间,0号空间空留不用,构成一个结构体数组head2*n*/           

14、        /*n为字符的种类*/    for(i=1;i<=n;i+)            fscanf(fp," %c %d",&headi.c,&headi.weight);    /* %c和%d之前的空格不能少,因为是格式化输出的

15、*/        headi.tag=0; /*无双亲*/        headi.parents=NULL;        headi.lc=NULL;        headi.rc=NULL;     

16、0;  headi.code0='0'        for(i=1;i<=3;i+)                         /*还原被代替的字符*/     

17、0;  if(headi.c=127)            headi.c=9;        else if (headi.c='')            headi.c='n'  

18、      else if(headi.c='')            headi.c=' '        else           for(i=1;i<n;i+)

19、     /*n个字符操作n1次*/            min1=min2=32767;        p1=NULL;p2=NULL;        for(j=1;j<n+i;j+)      &

20、#160;     if(headj.weight<min1)&&(headj.tag=0)                            min2=min1;      

21、0;         p2=p1;                min1=headj.weight;                p1=&headj;  &#

22、160;                     else if(headj.weight<min2)&&(headj.tag=0)                   

23、;         min2=headj.weight;                p2=&headj;                   

24、60;              /*找出了最小p1的次小的p2*/        p1->tag=1;        p2->tag=1;        headn+i.weight=p1->weig

25、ht+p2->weight;        p1->parents=&headn+i;        p2->parents=&headn+i;        headn+i.parents=NULL;        headn+i.lc=p1;

26、        headn+i.rc=p2;        headn+i.tag=0;        if(n=1)                    &

27、#160;         /*一种字符构不成huffman树,定义其代码*/            head1.code0='0'        head1.code1='0'        el

28、se            for(i=1;i<=n;i+)                    j=19;            cdj='0

29、'            p1=&headi;            for(p2=p1->parents;p2!=NULL;p2=p1->parents)              

30、0;             if(p1=p2->lc)                    cd-j='0'           &#

31、160;    else                    cd-j='1'                     /*用栈来存

32、储编码实现对其顺序的调整*/                p1=p2;                        strcpy(headi.code,&cdj);  

33、              fclose(fp);    return head;             /*申请了2n个空间,0号不用最后一个是2n1*/*文件校验函数*/void CheckSymbol(void)    F

34、ILE *fp,*fp1;    char ch1,ch2;    int  num=0;    if(fp=fopen(filename1,"rt")=NULL)            printf("Can't open the source

35、60;file!");        getch();            return         if(fp1=fopen(filename4,"rt")=NULL)        

36、0;   printf("Can't open the restore file!");        getch();        return         while(feof(fp)=0|feof(fp1)=0)  &#

37、160;         ch1=fgetc(fp);        ch2=fgetc(fp1);        if(ch1=ch2)            num+; continue;  &#

38、160;      else                    printf("nThe %d character is difficult!",num);          

39、  printf("nPlease check it!");            getch();            return             

40、0;   printf("nSucceeful!The file decoded is right!");    getch();/*统计主函数*/void CountSymbol(void)    FILE *fp;    char t;    int i;    f

41、or(i=0;i<=128;i+)            si.c=i;        si.weight=0;        printf("nPlease input the file's path and file na

42、me:");    getchar();    gets(filename1);    if(fp=fopen(filename1,"rt")=NULL)            printf("Can't open the source file!nPlease 

43、;cheak the flie!");        getch();        return         t=0;    while(feof(fp)=0) /*0时文件未结束*/      

44、0;     t=fgetc(fp);        st.weight+;       /*按照ASCII码表的对应位置统计权值*/         save();    fclose(fp);    printf("

45、nThe code has been saved succeful!");    getch();/*编码模块*/void Encode(NODE *head)    FILE *fp,*fpc;    char t,cd20;    int i;    fp=fopen(filename

46、1,"rt");                        /*遍历比较得到huffman编码*/    fpc=fopen("encode.txt","wt");       

47、0;/*生成中间0 1 代码文件*/    while(feof(fp)=0)            t=fgetc(fp);        for(i=1;i<=n;i+)            if(t=hea

48、di.c)                            strcpy(cd,headi.code);                fprintf(f

49、pc,"%s",cd);                    fclose(fp);    fclose(fpc);/*7位压缩模块*/void Compress(void)    int n=0,i=6;    long &#

50、160;num=0;                 /*统计0 1 个数*/    unsigned char ch=0,ch1;    FILE *fp,*fp1;    if(fp=fopen("encode.txt","r

51、t")=NULL)            printf("Can't open the code file!nPlease cheak the flie!");        getch();        re

52、turn         printf("nplease input the compressed file path and name:");    getchar();    gets(filename3);    if(fp1=fopen(filename3,"wt")=N

53、ULL)            printf("Can't open the compressed file!nPlease cheak the file!");        getch();        return

54、         while(feof(fp)=0)            num+;        ch1=fgetc(fp);           /*统计出了 0 1

55、0;个数*/                                /*遍历文件得到字符的个数,保存在filename3文件中*/    num-;    rewind (fp);&

56、#160;                /*将文件指针移回文件的头*/    fprintf(fp1,"%ld",num);    ch1=''    fprintf(fp1,"%c",ch1);     

57、60;/*注意输出的格式*/    while(feof(fp)=0)            ch1=fgetc(fp);           /*将数字0和1转换为字符0和1*/        if(ch1=255)  

58、0;          /*因为ch1只可能是结束标志或0 、1*/                    fputc(ch,fp1);            break

59、;                   ch1-=48;        ch1<<=i-;               /*从文件中读取字符*/  

60、      ch=ch|ch1;               /*与0取或将7个数字合并未一个*/        n+;        if(n%7=0)      &#

61、160;             if(ch=7)           /*屏对文件有影响的字符*/                ch=128;    

62、60;    /*7号显示不出,8号后退一个吃掉前一个字符*/            else if (ch=8)     /*13号显示不出,10号是换行,26号文件结束字符*/                

63、;ch=129;            else if (ch=9)                ch=130;            else if (ch

64、=10)                ch=131;            else if (ch=13)                

65、ch=132;            else if (ch=26)                ch=133;            else   

66、;                            fputc(ch,fp1);            n=0;i=6;ch=0;   /*初始化*/ &

67、#160;              fclose(fp1);    fclose(fp);/*解码模块*/void Decode(NODE *head,int n)    FILE *fp,*fp1;    char t='0'   

68、0;      /*随便赋初值*/    NODE *p1,*p0;    p1=p0=&head2*n-1;    if(fp=fopen("d:encode.txt","rt")=NULL)            printf("

69、;Can't open the compress file!");        getch();        return         if(fp1=fopen(filename4,"wt")=NULL)     

70、       printf("Can't open the decode file!");        getch();        return         while(feof(fp)=0) 

71、;           t=fgetc(fp);        /*只有一个节点是构不成huffman树*/        if(t='0')              &#

72、160;     if(p1->lc=NULL)                fprintf(fp1,"%c",p1->c);            else      

73、60;                                    p1=p1->lc;           &#

74、160;                      if(p1->lc=NULL&&p1->rc=NULL)                    &#

75、160;               fprintf(fp1,"%c",p1->c);                    p1=p0;       &

76、#160;                                    else if(t='1')        

77、0;           p1=p1->rc;            if(p1->rc=NULL&&p1->lc=NULL)                 

78、60;          fprintf(fp1,"%c",p1->c);                p1=p0;                &#

79、160;           fclose(fp);    fclose(fp1);/*解7位压缩模块*/void Uncompress(void)    FILE *fp,*fp1;    long num;    int i;    unsign

80、ed char ch,exp;      /*在tc环境下默认有符号,但在vc环境下默认有符号*/    if(fp=fopen(filename3,"rt")=NULL)            printf("Can't open file!nPlease cheak the&#

81、160;flie!");        getch();        exit(1);        if(fp1=fopen("d:encode.txt","wt")=NULL)          

82、0; printf("Can't open file!nPlease cheak the flie!");        getch();        exit(1);        fscanf(fp,"%ld",&num); &

83、#160;/*注意格式*/    fscanf(fp,"%c",&ch);    /*取出''*/    while(feof(fp)=0) /*只读出有用字符,而且解决了中断问题!*/            ch=fgetc(fp);      

84、;  if(ch=255)   /*这块有问题,最后11111111时会出错!*/            break;        if(ch=128)           /*屏蔽对文件有影响的ASCII码*/  &

85、#160;         ch=7;         /*7号显示不出,8号后退一个吃掉前一个字符,9号tab多时出错*/        else if (ch=129)     /*13号显示不出,10号是换行,26号文件结束字符*/  

86、0;         ch=8;        else if (ch=130)            ch=9;        else if (ch=131)  &#

87、160;         ch=10;        else if (ch=132)            ch=13;        else if (ch=133) 

88、0;          ch=26;        else          for(i=1;i<8;i+)                 

89、   exp=ch;  /*寄存*/            exp<<=i;            exp>>=7;              &

90、#160;      exp+=48;           /*将从文件中读出的字符中的有效数字还原成数字*/            if(num!=0)      /*输出的总的有效的位数够后就不进行输出*/   &#

91、160;                        num-;     /*只将有效的字符输出*/                fputc(ex

92、p,fp1);                            fclose(fp1);    fclose(fp);/*压缩率计算模块*/void Rate(void)    FILE *fp,*fp1;

93、0;   char ch=1;    float num=0.0,num1=0.0;    float weight,weight1,rate;    fp=fopen(filename1,"rt");    fp1=fopen(filename3,"rt");    while(feof(fp)=

94、0)            ch=fgetc(fp);          /*比较源文件和压缩后的文件*/        num+;        num-;  /*减去末尾结束标志*/ &

95、#160;  weight=num/1024;    while(feof(fp1)=0)            ch=fgetc(fp1);        num1+;        num1-;/*减去末尾结束标志*/   &

96、#160;weight1=num1/1024;    rate=weight1/weight;    printf("nThe source file weight is %.4f Kb!",weight);    printf("nThe compressed file weight is %.4fKb!",weight1);

97、    printf("nThe compresse rete is %.2f %!",rate*100);    getch();/*编码压缩主函数*/void EncodeSymbol(void)    NODE *head=NULL;    head=BuildTree();     

98、0; /*得到了huffman树的首地址*/    Encode(head);    Compress();/*    remove("d:encode.txt"); */    /*将中间文件删除*/    printf("nThe file has been compressed!");

99、0;   printf("nIt was saved in %s",filename2);    Rate();/*解码压缩主函数*/void DecodeSymbol(void)    NODE *head=NULL;    printf("nPlease input the code file path:

100、");    getchar();    gets(filename2);    printf("nPlease input the compressed file path:");    gets(filename3);    printf("nPlease input the 

101、;restored file path:");    gets(filename4);    head=BuildTree();    Uncompress();    Decode(head,n);/*    remove("d:encode.txt");  */*删除中间文件*/    

102、;printf("nThe file has been restored!");    printf("nIt is saved in the %s!",filename4);    getch();/*主函数*/void main(void)    int choice;    do            clrscr();        printf("nnnnnnn");        printf("        &

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论