求助!!帮忙看一下这个算法程序有什么错误??
c语言吧
全部回复
仅看楼主
level 1
happygongc 楼主
#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:");   

2010年04月18日 11点04分 1
level 1
happygongc 楼主
       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);   

2010年04月18日 11点04分 2
level 1
happygongc 楼主

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

2010年04月18日 11点04分 3
level 1
happygongc 楼主

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

2010年04月18日 11点04分 4
level 1
happygongc 楼主
         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);   
   }   
visual c下可以编译通过,但是执行到 dictionary=(struct cell *)malloc(sizeof(struct    cell));    这一行的时候就不能进行了,另外开头这个
struct    cell{   
   char    *point;   
   struct    cell    *next;}*dictionary;   
结构体我没有看懂。。
牛人帮我改一下啊 ,谢谢谢谢!!!!!
2010年04月18日 11点04分 5
1