帮忙修改一个算法的程序,谢谢了
#include "stdafx.h"#include<string.h>
#include <malloc.h>
#include <stdlib.h>
struct cell{
char *point;
struct cell *next;}*dictionary;
void compress(FILE *in,FILE *out,struct cell *dic);
void expand(FILE *in,FILE *out,struct cell *dic);
void addtodictionary(char *p,struct cell *dic);
int inthetable(char *c,struct cell *dic);
int inthestringtable(int k,struct cell *dic);
int n=95;
main()
{
int choise,i;
char *infilename,*outfilename;
FILE *infile,*outfile;
struct cell *dtemp1,*dtemp2;
dictionary=(struct cell *)malloc(sizeof(struct cell));
*(dictionary->point)=32;
dictionary->next=NULL;
dtemp1=dictionary;
for(i=33;i<=127;i++)
{
dtemp2=(struct cell*)malloc(sizeof(struct cell));
dtemp1->next=dtemp2;dtemp2->next=NULL;
*(dtemp2->point)=i;
} /*Dui Ditionary Chu SHi Hua*/
printf("<1> for Compress.\n<2> for Expand.\n<0> for Exit!\n ");
printf("Please Input Your Choise:");
scanf("%d",&choise);
if(choise==1)
{
printf("\nPlease Input The Compressing File Name:");
scanf("%s",infilename);
printf("\nPlease Input The Name for the Compressed File:");
scanf("%s",outfilename);
/*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*/
compress(infile,outfile,dictionary);
/*Can Compress*/
fclose(infile);
fclose(outfile);
}
else if(choise==2)
{
printf("Please Input THe Expanding File Name.\n");
scanf("%s",infilename);
printf("Please INput the Name for THe Expanded File.\n");
scanf("%s",outfilename);
/*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);
return 0;
}
void compress(FILE *in,FILE *out,struct cell *dic)
{
FILE *ifp,*ofp;
struct cell *table;
char c[2],*ctemp,*p;
int j;
ifp=in;
ofp=out;
table=dic;
p[0]='\0';
c[0]=fgetc(ifp);
c[1]='\0';
while(c[0]!=EOF)
{
strcpy(ctemp,p);
strcpy(ctemp,c);
if((j=inthetable(ctemp,table))!=-1)
strcpy(p,ctemp);
else {
fwrite(&j,sizeof(int),1,ofp);
addtodictionary(ctemp,table);
strcpy(p,c);
}
c[0]=fgetc(ifp);
}
j=inthetable(p,table);
fwrite(&j,sizeof(int),1,ofp);
}/*End the Compress()*/
int inthetable(char *c,struct cell *dic)
{
char *ctemp;struct cell *table;int j=0;
strcpy(ctemp,c);table=dic;
while(table!=NULL)
{
if(strcmp(table->point,ctemp)==0)
{ return(j);exit(1);}
else{
table=table->next;
j++;}
return(-1);
}
}/* End the inthetable()*/
void addtodictionary(char *p,struct cell *dic)
{
char *ctemp;
struct cell *table,*dtemp;
strcpy(ctemp,p);
table=dic;
table=table+n*sizeof(struct cell);
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;
int cw,pw,i,j,length,*w;
char *ctemp,*p,c[2];
ifp=in;
ofp=out;
table=dic;
n=95;/*Chong Xin Ding Yi n*/
dtemp=table+n*sizeof(struct cell);
dtemp->next=NULL;/*Chong Xin Ding YI\i Dictionary*/
j=fread(w,sizeof(int),1,ifp);
cw=*w;
dtemp=table+cw*sizeof(struct cell);
strcpy(ctemp,dtemp->point);
length=strlen(ctemp);
while(j!=0)
{
pw=cw;
j=fread(w,sizeof(int),1,ifp);
cw=*w;
if((i=inthestringtable(cw,dic))!=-1)
{
strcpy(ctemp,(table+cw*sizeof(struct cell))->point);
length=strlen(ctemp);
fwrite(ctemp,sizeof(char),length,ofp);
strcpy(p,(table+pw*sizeof(struct cell))->point);
c[0]=*((table+cw*sizeof(struct cell))->point);c[1]='\0';
strcpy(ctemp,p);
strcat(ctemp,c);
addtodictionary(ctemp,table);
}
else
{
strcpy(p,(table+pw*sizeof(struct cell))->point);
c[0]=*((table+cw*sizeof(struct cell))->point);
c[1]='\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)
{
dtemp=dtemp->next;
j++;
}
if(j==k)
return(j);
else
return(-1);
}
一个字典算法的 c实现 用vc6.0 执行的时候有问题, 谁能帮我修改一下。谢谢了!!!