⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 new.txt

📁 lzwdechengxu.rar
💻 TXT
字号:
#include<stdio.h>
#include<string.h>
struct   cell{
char   point[100];
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;i<=122;i++)
    {
      dtemp2=(struct   cell*)malloc(sizeof(struct   cell));
      dtemp1->next=dtemp2;dtemp2->next=NULL;
      dtemp1=dtemp2;
      *(dtemp2->point)=i;*((dtemp2->point)+1)='\0';
    }   /*Dui   Ditionary   Chu   SHi   Hua*/
    printf("\n<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);
  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   c[2],*ctemp,*p;int   j;
  ifp=in;ofp=out;table=dic;
  ctemp=malloc(sizeof(char));p=malloc(sizeof(char));
  p[0]='\0';c[0]=fgetc(ifp);c[1]='\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);
              }
    c[0]=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;i<=n;i++)
  table=table->next;
  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,c[2];
  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;i<=31;i++)
  dtemp=dtemp->next;
  dtemp1=dtemp->next;
  dtemp->next=NULL;
  for(i=33;i<=n;i++)
  {   dtemp=dtemp1->next;
      free(dtemp1);
      dtemp1=dtemp;}     /*Chong   Xin   Ding   YI\i   Dictionary*/
  j=fread(w,sizeof(int),1,ifp);
  cw=*w;printf("     %d.",cw);
  dtemp=table;
  for(i=1;i<=cw;i++)
  dtemp=dtemp->next;
  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;i<=cw;i++)
      dtemp1=dtemp1->next;
      strcpy(ctemp,dtemp1->point);
      length=strlen(ctemp);
      fwrite(ctemp,sizeof(char),length,ofp);
      dtemp1=table;
      for(i=1;i<=pw;i++)
      dtemp1=dtemp1->next;
      strcpy(p,dtemp1->point);
      dtemp1=table;
      for(i=1;i<=cw;i++)
      dtemp1=dtemp1->next;
      c[0]=*(dtemp1->point);c[1]='\0';
      strcpy(ctemp,p);
      strcat(ctemp,c);
      addtodictionary(ctemp,table);
    }
    else
    {
    dtemp1=table;
    for(i=1;i<=pw;i++)
    dtemp1=dtemp1->next;
    strcpy(p,dtemp1->point);
    dtemp1=table;
    for(i=1;i<=cw;i++)
    dtemp1=dtemp1->next;
    c[0]=*(dtemp1->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)
    {
      if(j<k){   dtemp=dtemp->next;
        j++;}
      else   if(j==k){return(j);}
    }
    if(j==k)
        return(j);
    else
        return(-1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -