| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 598 人关注过本帖
标题:帮忙修改一个算法的程序,谢谢了
只看楼主 加入收藏
happygongc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-4-18
结帖率:0
收藏
已结贴  问题点数:20 回复次数:6 
帮忙修改一个算法的程序,谢谢了
#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 执行的时候有问题, 谁能帮我修改一下。谢谢了!!!
搜索更多相关主题的帖子: 算法 
2010-04-18 19:08
雨夜星光
Rank: 4
等 级:业余侠客
帖 子:66
专家分:230
注 册:2009-11-25
收藏
得分:6 
缺少文件吧?

C/C++交流群:65802335
2010-04-18 19:30
happygongc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-4-18
收藏
得分:0 
不缺少文件啊,visual c 下可以编译通过的,就是结果不对。
struct   cell{   
  char   *point;   
  struct   cell   *next;}*dictionary;   
 
刚开头这个结构体就看不太懂。。
2010-04-18 19:39
雨夜星光
Rank: 4
等 级:业余侠客
帖 子:66
专家分:230
注 册:2009-11-25
收藏
得分:0 
我没有通过。不知你的这个算法是什么样的算法?
刚开头的那个结构你都不懂?那是定义一个cell结构的指针变量。

C/C++交流群:65802335
2010-04-18 21:31
yukun314
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:35
专家分:120
注 册:2010-4-18
收藏
得分:6 
struct是定义结构体的,
struct   cell{   
  char   *point;   
  struct   cell   *next;}*dictionary;
 这种写法是在定义结构体的同时定义一个结构体指针变量
2010-04-18 21:51
happygongc
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-4-18
收藏
得分:0 
我在书上找到这结构,是叫链表结构,但是就是不知道哪里错了。 哎!
2010-04-18 22:02
小兔子慢慢
Rank: 2
等 级:论坛游民
帖 子:45
专家分:30
注 册:2009-4-13
收藏
得分:6 
好庞大
2010-04-19 08:28
快速回复:帮忙修改一个算法的程序,谢谢了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013566 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved