三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 422|回复: 0
打印 上一主题 下一主题

C语言数据结构-algo4-4.c

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-9-1 08:36:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* algo4-4.c 生成书名关键词索引文件bookidx.txt,算法4.9~4.14 */
  2. /* 为运行algo4-5.c做准备 */
  3. #include "c1.h"
  4. typedef int ElemType;
  5. #include"c2-5.h"
  6. #include"bo2-6.c"
  7. #include"c4-2.h"
  8. #include"bo4-2.c"

  9. #define MaxKeyNum 25 /* 索引表的最大容量(关键词的最大数) */
  10. #define MaxLineLen 51 /* 书目串(书名+书号)buf的最大长度 */
  11. #define MaxWordNum 10 /* 词表(一本书的关键词)的最大容量 */
  12. #define MaxNoIdx 10 /* 常用词(仅指大写)的最大数 */
  13. typedef struct
  14. {
  15.    char *item[MaxWordNum]; /* 词表(字符串)指针数组 */
  16.    int last; /* 词的数量 */
  17. }WordListType; /* 词表类型(顺序表) */

  18. typedef struct
  19. {
  20.    HString key; /* 关键词(堆分配类型,c4-2.h) */
  21.    LinkList bnolist; /* 存放书号索引的链表(c2-5.h) */
  22. }IdxTermType; /* 索引项类型 */

  23. typedef struct
  24. {
  25.    IdxTermType item[MaxKeyNum+1];
  26.    int last; /* 关键词的个数 */
  27. }IdxListType; /* 索引表类型(有序表) */

  28. typedef struct
  29. {
  30.    char *item[MaxNoIdx]; /* 常用词表指针数组 */
  31.    int last; /* 常用词的数量 */
  32. }NoIdxType; /* 常用词表类型(有序表) */

  33. /* 全局变量 */
  34. char buf[MaxLineLen+1]; /* 当前书目串(包括'\0') */
  35. WordListType wdlist; /* 暂存一本书的词表 */
  36. NoIdxType noidx; /* 常用词表 */

  37. void InitIdxList(IdxListType *idxlist)
  38. { /* 初始化操作,置索引表idxlist为空表,且在idxliat.item[0]设一空串 */
  39.    (*idxlist).last=0;
  40.    InitString(&(*idxlist).item[0].key); /* bo4-2.c */
  41.    InitList(&(*idxlist).item[0].bnolist); /* bo2-6.c */
  42. }

  43. void ExtractKeyWord(int *BookNo)
  44. { /* 从buf中提取书名关键词到词表wdlist,书号存入BookNo */
  45.    int i,l,f=1; /* f是字符串结束标志 0: 结束 1: 未结束 */
  46.    char *s1,*s2;
  47.    if(buf[0]<'0'||buf[0]>'9') /* buf的首字母不是数字 */
  48.      exit(OVERFLOW);
  49.    for(i=1;i<=wdlist.last;i++) /* 释放上一个书目在词表wdlist的存储空间 */
  50.    {
  51.      free(wdlist.item[i]);
  52.      wdlist.item[i]=NULL;
  53.    }
  54.    wdlist.last=0;
  55.    *BookNo=(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0'); /* 前三位为书号 */
  56.    s2=&buf[2]; /* s1指向书号的尾字符 */
  57.    do
  58.    { /* 提取书名关键词到词表wdlist */
  59.      s1=s2+1; /* s1向后移动一个单词 */
  60.      s2=strchr(s1,' '); /* s2指向s1的第一个空格,如没有,返回NULL */
  61.      if(!s2) /* 到串尾 */
  62.      {
  63.        s2=strchr(s1,'\12'); /* s2指向buf的最后一个字符(回车符) */
  64.        f=0;
  65.      }
  66.      l=s2-s1; /* 单词长度 */
  67.      if(s1[0]>='A'&&s1[0]<='Z') /* 单词首字母为大写 */
  68.      { /* 写入词表 */
  69.        wdlist.item[wdlist.last]=(char *)malloc((l+1)*sizeof(char));
  70.        /* 生成串空间(包括'\0') */
  71.        for(i=0;i<l;i++)
  72.          wdlist.item[wdlist.last][i]=s1[i]; /* 写入词表 */
  73.        wdlist.item[wdlist.last][l]=0;
  74.        for(i=0;i<noidx.last;i++) /* 查找是否为常用词 */
  75.          if(!strcmp(wdlist.item[wdlist.last],noidx.item[i]))
  76.            break;
  77.        if(i!=noidx.last) /* 是常用词 */
  78.        {
  79.          free(wdlist.item[wdlist.last]); /* 从词表中删除该词 */
  80.          wdlist.item[wdlist.last]=NULL;
  81.        }
  82.        else
  83.          wdlist.last++; /* 词表长度+1 */
  84.      }
  85.    }while(f);
  86. }

  87. void GetWord(int i,HString *wd)
  88. { /* 用wd返回词表wdlist中第i个关键词 */
  89.    StrAssign(wd,wdlist.item[i]); /* 生成关键字字符串 bo4-2.c */
  90. }

  91. int Locate(IdxListType *idxlist,HString wd,Status *b)
  92. { /* 在索引表idxlist中查询是否存在与wd相等的关键词。若存在,则返回其 */
  93.    /* 在索引表中的位置,且b取值TRUE;否则返回插入位置,且b取值FALSE */
  94.    int i,m;
  95.    for(i=(*idxlist).last;(m=StrCompare((*idxlist).item[i].key,wd))>0;--i); /* bo4-2.c */
  96.    if(m==0) /* 找到 */
  97.    {
  98.      *b=TRUE;
  99.      return i;
  100.    }
  101.    else
  102.    {
  103.      *b=FALSE;
  104.      return i+1;
  105.    }
  106. }

  107. void InsertNewKey(IdxListType *idxlist,int i,HString wd)
  108. { /* 在索引表idxlist的第i项上插入新关键词wd,并初始化书号索引的链表为空表 */
  109.    int j;
  110.    InitList(&(*idxlist).item[(*idxlist).last+1].bnolist); /* bo2-6.c */
  111.    for(j=(*idxlist).last;j>=i;--j) /* 后移索引项 */
  112.      (*idxlist).item[j+1]=(*idxlist).item[j];
  113.    InitString(&(*idxlist).item[i].key); /* bo4-2.c */
  114.    StrCopy(&(*idxlist).item[i].key,wd); /* 串拷贝插入新的索引项 bo4-2.c */
  115.    InitList(&(*idxlist).item[i].bnolist); /* 初始化书号索引表为空表 bo2-6.c */
  116.    (*idxlist).last++;
  117. }

  118. void InsertBook(IdxListType *idxlist,int i,int bno)
  119. { /* 在索引表idxlist的第i项中插入书号为bno的索引 */
  120.    Link p;
  121.    if(!MakeNode(&p,bno)) /* 分配失败 bo2-6.c */
  122.      exit(OVERFLOW);
  123.    p->next=NULL;
  124.    Append(&(*idxlist).item[i].bnolist,p); /* 插入新的书号索引 bo2-6.c */
  125. }

  126. void InsIdxList(IdxListType *idxlist,int bno)
  127. { /* 将书号为bno的关键词插入索引表 */
  128.    int i,j;
  129.    Status b;
  130.    HString wd;
  131.    InitString(&wd); /* bo4-2.c */
  132.    for(i=0;i<wdlist.last;i++)
  133.    {
  134.      GetWord(i,&wd);
  135.      j=Locate(idxlist,wd,&b);
  136.      if(!b)
  137.        InsertNewKey(idxlist,j,wd); /* 插入新的索引项 */
  138.      InsertBook(idxlist,j,bno); /* 插入书号索引 */
  139.    }
  140. }

  141. void PutText(FILE *f,IdxListType idxlist)
  142. { /* 将生成的索引表idxlist输出到文件f */
  143.    int i,j;
  144.    Link p;
  145.    fprintf(f,"%d\n",idxlist.last);
  146.    for(i=1;i<=idxlist.last;i++)
  147.    {
  148.      for(j=0;j<idxlist.item[i].key.length;j++)
  149.        fprintf(f,"%c",idxlist.item[i].key.ch[j]);
  150.      fprintf(f,"\n%d\n",idxlist.item[i].bnolist.len);
  151.      p=idxlist.item[i].bnolist.head;
  152.      for(j=1;j<=idxlist.item[i].bnolist.len;j++)
  153.      {
  154.        p=p->next;
  155.        fprintf(f,"%d ",p->data);
  156.      }
  157.      fprintf(f,"\n");
  158.    }
  159. }

  160. void main()
  161. {
  162.    FILE *f; /* 任何时间最多打开一个文件 */
  163.    IdxListType idxlist; /* 索引表 */
  164.    int BookNo; /* 书号变量 */
  165.    int k,l;
  166.    f=fopen("NoIdx.txt","r"); /* 打开常用词文件 */
  167.    if(!f)
  168.      exit(OVERFLOW);
  169.    fscanf(f,"%d",&noidx.last); /* 常用词个数 */
  170.    for(k=0;k<noidx.last;k++) /* 把常用词文件的内容拷到noidx中 */
  171.    {
  172.      fscanf(f,"%s",buf);
  173.      l=strlen(buf);
  174.      noidx.item[k]=(char*)malloc(l*sizeof(char));
  175.      strcpy(noidx.item[k],buf);
  176.    }
  177.    fclose(f);
  178.    f=fopen("BookInfo.txt","r"); /* 打开书目文件 */
  179.    if(!f)
  180.      exit(FALSE);
  181.    InitIdxList(&idxlist); /* 初始化索引表idxlist为空 */
  182.    wdlist.last=0; /* 词表长度初值为0 */
  183.    while(!feof(f))
  184.    {
  185.      fgets(buf,MaxLineLen,f);
  186.      l=strlen(buf);
  187.      if(l<=1)
  188.        break;
  189.      ExtractKeyWord(&BookNo); /* 从buf中提取关键词到词表,书号存入BookNo */
  190.      InsIdxList(&idxlist,BookNo);
  191.    }
  192.    fclose(f);
  193.    f=fopen("BookIdx.txt","w");
  194.    if(!f)
  195.      exit(INFEASIBLE);
  196.    PutText(f,idxlist); /* 将生成的索引表idxlist输出到文件f */
  197.    fclose(f);
  198. }
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-12-8 00:50 , Processed in 0.030635 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表