




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1现在参照上面的编码伪代码可以试着用所学的C语言来实现它:void compress(FILE *in,FILE *out,struct cell *dic)FILE *ifp,*ofp;struct cell *table;char c2,*ctemp,*p;int j; ifp=in;ofp=out;table=dic; ctemp=malloc(sizeof(char);p=malloc(sizeof(char); p0=0;c0=fgetc(ifp);c1=0; while(!feof(ifp)/*逐步单个字符读ifp文本文件知道文件结束*/ strcpy(ctemp,p);strcat(ctemp,c); if(j=inthetable(ctemp,table)!=-1)/*函数inthetable(ctemp,table)是判断字符串ctemp是否在字典table中,它的返回值是ctem在字典table中的位置,但若ctemp不存在则返回-1.*/strcpy(p,ctemp); else j=inthetable(p,table);fwrite(&j,sizeof(int),1,ofp);/*把整数j写入ofp文件中,ofp实指向被压缩文件的指针*/addtodictionary(ctemp,table);/*函数addtodictionary(ctemp,table)是把字符串ctemp加入到字 table的尾部*/strcpy(p,c); c0=fgetc(ifp); j=inthetable(p,table); fwrite(&j,sizeof(int),1,ofp);/*End the Compress()*/对于子函数inthetable()和addtodictionary我们可以暂时只要明确它们的功能,具体实现可以留在后面再做2现也用C语言来实现它:void expand(FILE *in,FILE *out,struct cell *dic)FILE *ifp,*ofp;struct cell *table,*dtemp,*dtemp1; int cw,pw,i,j,length,*w; char *ctemp,*p,c2; w=malloc(sizeof(int);ctemp=malloc(sizeof(char);p=malloc(sizeof(char); ifp=in;ofp=out;table=dic; dtemp=table; for(i=1;inext; dtemp1=dtemp-next; dtem是判断字典的位置cw,后者是判断字符串ctemp*/ temp1=table; for(i=1;inext; strcpy(ctemp,dtemp1-point);length=strlen(ctemp); fwrite(ctemp,sizeof(char),length,ofp); dtemp1=table; for(i=1;inext; strcpy(p,dtemp1-point); dtemp1=table; for(i=1;inext; c0=*(dtemp1-point);c1=0; strcpy(ctemp,p); strcat(ctemp,c);addtodictionary(ctemp,table);else dtemp1=table; for(i=1;inext; strcpy(p,dtemp1-point);dtemp1=table; for(i=1;inext; c0=*(dtemp1-point);c1=0; strcpy(ctemp,p); strcat(ctemp,c); length=strlen(ctemp); fwrite(ctemp,sizeof(char),length,ofp); addtodictionary(ctemp,table); /*End The Expand()*/现在我们所剩下的只有inthetable(),addtodictionary()和inthestringtable()三个子函数的实现问题了,按照自己事先所预定的函数功能,可以很容易给出这三个字函数的C语言描述:int inthetable(char *c,struct cell *dic)char *ctemp;struct cell *table;int j=0; ctemp=malloc(sizeof(char);strcpy(ctemp,c);table=dic; while(table!=NULL) if(strcmp(table-point,ctemp)=0) return(j);/*存在*/ elsetable=table-next;j+; return(-1);/*不存在*/* End the inthetable()*/void addtodictionary(char *p,struct cell *dic)char *ctemp;struct cell *table,*dtemp;int i;ctemp=malloc(sizeof(char); strcpy(ctemp,p);table=dic; for(i=1;inext; dtemp=(struct cell *)malloc(sizeof(struct cell); strcpy(dtemp-point,ctemp); table-next=dtemp;dtemp-next=NULL;n+;/*n为全局变量,用来纪录字典的长度*/*End the addtodictionary()*/int inthestringtable(int k,struct cell *dic) struct cell *dtemp;int j=0;dtemp=dic; while(dtemp-next!=NULL) if(jnext;j+; else if(j=k)return(j);/*存在*/if(j=k) return(j); else return(-1);/*不存在*/四、程序的进一步实现(源代码):#include#includestruct cellchar point100;struct cell *next;*dictionary;void compress();void expand();void addtodictionary();void *malloc();int inthetable();int inthestringtable();int n=31;main() int choise,i; char *infilename,*outfilename;FILE *infile,*outfile; struct cell *dtemp1,*dtemp2; infilename=malloc(sizeof(char);outfilename=malloc(sizeof(char); dictionary=(struct cell *)malloc(sizeof(struct cell); *(dictionary-point)=44;*(dictionary-point)+1)=0; dtemp1=(struct cell*)malloc(sizeof(struct cell); dictionary-next=dtemp1;*(dtemp1-point)=46;*(dtemp1-point)+1)=0; dtemp2=(struct cell*)malloc(sizeof(struct cell); dtemp1-next=dtemp2;*(dtemp2-point)=58;*(dtemp2-point)+1)=0;dtemp1=dtemp2; dtemp2=(struct cell*)malloc(sizeof(struct cell); dtemp1-next=dtemp2;*(dtemp2-point)=59;*(dtemp2-point)+1)=0;dtemp1=dtemp2; dtemp2=(struct cell*)malloc(sizeof(struct cell); dtemp1-next=dtemp2;*(dtemp2-point)=10;*(dtemp2-point)+1)=0;dtemp1=dtemp2; dtemp2=(struct cell*)malloc(sizeof(struct cell); dtemp1-next=dtemp2;*(dtemp2-point)=13;*(dtemp2-point)+1)=0;dtemp1=dtemp2; for(i=97;inext=dtemp2;dtemp2-next=NULL; dtemp1=dtemp2; *(dtemp2-point)=i;*(dtemp2-point)+1)=0; /*Dui Ditionary Chu SHi Hua*/ printf(n for Compress.n for Expand.n for Exit!n ); printf(Please Input Your Choise:); scanf(%d,&choise); if(choise=1) printf(nPlease Input The Compressing File Name:); scanf(%s,infilename); if(infile=fopen(infilename,rb)=NULL) printf(Cannot open the %s file!n,infilename);exit(1); printf(nPlease Input The Name for the Compressed File:); scanf(%s,outfilename); if(outfile=fopen(outfilename,wb)=NULL) printf(Cannot open the %s file!n,outfilename);exit(1); compress(infile,outfile,dictionary);/*Can Compress*/ fclose(infile); fclose(outfile); else if(choise=2) printf(nPlease Input THe Expanding File Name:); scanf(%s,infilename); if(infile=fopen(infilename,rb)=NULL) printf(Cannot open the %s file!n,infilename);exit(1); printf(nPlease INput the Name for THe Expanded File:); scanf(%s,outfilename); if(outfile=fopen(outfilename,wb)=NULL) printf(Cannot open the %s file!n,outfilename);exit(1); /*if(infile=fopen(infilename,rb)=NULL|(outfile=fopen(outfilename,wb)=NULL)printf(Cannot Open THe %s File or The %s File!n,infilename,outfilename);exit(1); else*/ expand(infile,outfile,dictionary);/*Can Expand*/ fclose(infile);fclose(outfile); else exit(1);void compress(FILE *in,FILE *out,struct cell *dic) FILE *ifp,*ofp;struct cell *table;char c2,*ctemp,*p;int j; ifp=in;ofp=out;table=dic; ctemp=malloc(sizeof(char);p=malloc(sizeof(char); p0=0;c0=fgetc(ifp);c1=0; while(!feof(ifp) strcpy(ctemp,p);strcat(ctemp,c); if(j=inthetable(ctemp,table)!=-1)strcpy(p,ctemp); else j=inthetable(p,table);printf( %d.,j);fwrite(&j,sizeof(int),1,ofp);addtodictionary(ctemp,table);strcpy(p,c); c0=fgetc(ifp); j=inthetable(p,table); printf( %d.,j); fwrite(&j,sizeof(int),1,ofp);/*End the Compress()*/int inthetable(char *c,struct cell *dic) char *ctemp;struct cell *table;int j=0; ctemp=malloc(sizeof(char); strcpy(ctemp,c);table=dic;while(table!=NULL) if(strcmp(table-point,ctemp)=0) return(j); else table=table-next; j+; return(-1);/* End the inthetable()*/void addtodictionary(char *p,struct cell *dic) char *ctemp;struct cell *table,*dtemp;int i; ctemp=malloc(sizeof(char); strcpy(ctemp,p);table=dic; for(i=1;inext; dtemp=(struct cell *)malloc(sizeof(struct cell); strcpy(dtemp-point,ctemp); table-next=dtemp;dtemp-next=NULL; n+;/*End the addtodictionary()*/void expand(FILE *in,FILE *out,struct cell *dic) FILE *ifp,*ofp;struct cell *table,*dtemp,*dtemp1; int cw,pw,i,j,length,*w; char *ctemp,*p,c2; w=malloc(sizeof(int);ctemp=malloc(sizeof(char);p=malloc(sizeof(char); ifp=in;ofp=out;table=dic; /*Chong Xin Ding Yi n*/ dtemp=table; for(i=1;inext; dtemp1=dtemp-next; dtemp-next=NULL; for(i=33;inext; free(dtemp1); dtemp1=dtemp; /*Chong Xin Ding YIi Dictionary*/ j=fread(w,sizeof(int),1,ifp); cw=*w;printf( %d.,cw); dtemp=table; for(i=1;inext; strcpy(ctemp,dtemp-point);length=strlen(ctemp); while(j!=0) pw=cw; j=fread(w,sizeof(int),1,ifp); cw=*w;printf( %d.,cw); if(i=inthestringtable(cw,dic)!=-1) dtemp1=table; for(i=1;inext; strcpy(ctemp,dtemp1-point); length=strlen(ctemp); fwrite(ctemp,sizeof(char),length,ofp); dtemp1=table; for(i=1;inext; strcpy(p,dtemp1-point); dtemp1=table; for(i=1;inext; c0=*(dtemp1-point);c1=0; strcpy(ctemp,p); strcat(ctemp,c); addtodictionary(ctemp,table); else dtemp1=table; for(i=1;inext; strcpy(p,dtemp1-point); dtemp1=table; for(i=1;inext; c0=*(dtemp1-point);c1=0; strcpy(ctemp,p); strcat(ctemp,c); length=strlen(ctemp); fwrite(ctemp,sizeof(char),length,ofp); addtodictionary(ctemp,table); /*End The Expand()*/int inthestringtable(int k,struct cell *dic) struct cell *dtemp;int j=0; dtemp=dic; while(dtemp-next!=NULL) if(jnext; j+; else if(j=k)return(j); if(j=k) return(j); else return(-1);五、程序的调试:通过调试,在排除程序的所有语法,算法错误和所有的警告后,对程序进行初步尝试性运行,我先新建一文本文件c:tczk.txt,其内容只由小写字母“a,b,c,“和“,。;:“以及回车和换行符组成的“abacaba”,然后运行LZW.C: for Compress. for Expand. for Exit! Please Input Your Choise:1Please Input The Compressing File Name:d:tcas.txtPlease Input The Name for the Compressed File:d:tc123.txt元文本文件zk.txt的文件大小是7字节,被压缩后的文件123.txt文件大小却是12字节,显然没有起到我们前面所预期的压缩功能,仔细思索其中的缘故,发现问题的所在是我们对这两个文件所进行的操作方式的差异- 我们对zk.txt文件的读入方式用的是fgetc(ifp),也就是说是按照字符大小为单位的,而我们在对123.txt文件的写入操作用得却是fwrite(&j,sizeof(int),1,ofp);址操作是按照整数大小为单位来进
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 科明五金车间主管年终总结
- 《诗经》二首蒹葭课件
- 《诗经·岂曰无衣》课件
- 建筑项目施工成本管理体系及成本控制措施(流程图)
- 《行香子》课件教学课件
- 物业礼仪工作汇报
- 公司火灾安全培训总结课件
- 福建省招标采购从业人员考试(招标采购专业实务初级)在线复习题库及答案(2025年)
- 转运中心调度负责人年终总结
- 流动红旗评选汇报
- GB/T 17505-2016钢及钢产品交货一般技术要求
- 26个英语字母手写体(含笔顺)精要课件
- 第二章第一节认识网络 课件 【知识精讲+备课精研+高效课堂】 教育科学出版社选择性必修二网络基础
- 神经外科术后并发症观察及护理课件整理
- 【教师必备】部编版五年级语文上册第三单元【集体备课】
- 脊柱弯曲异常筛查结果记录表
- IATF16949-2016内审员理论考试题库及答案
- 各种煤气化技术介绍课件
- 中国文学理论批评史全套教学课件
- (新版)民用航空安全检查规则100题
- 超高压线路成套保护装置技术和使用说明书
评论
0/150
提交评论