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

📄 algo4-5.cpp

📁 Status StrAssign(SString T,char *chars) { // 生成一个其值等于chars的串T int i if(strlen(chars)>MAXST
💻 CPP
字号:
 // algo4-5.cpp 根据algo4-4.cpp产生的文件,索引查询图书
 #include"c1.h"
 typedef int ElemType;
 #include"c2-5.h"
 #include"bo2-6.cpp"
 #include"c4-2.h"
 #include"bo4-2.cpp"

 #define MaxBookNum 10 // 假设只对10本书建索引表
 #define MaxKeyNum 25 // 索引表的最大容量(关键词的最大数)
 #define MaxLineLen 51 // 书目串(书名+书号)buf的最大长度

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

 struct IdxListType // 索引表类型(有序表)
 {
   IdxTermType item[MaxKeyNum+1];
   int last; // 关键词的个数
 };

 struct BookTermType // 书目项类型
 {
   char bookname[MaxLineLen]; // 书目串
   int bookno; // 书号
 };

 struct BookListType // 书目表类型(有序表)
 {
   BookTermType item[MaxBookNum];
   int last; // 书目的数量
 };

 void main()
 {
   FILE *f; // 任何时间最多打开一个文件
   IdxListType idxlist; // 索引表
   BookListType booklist; // 书目表
   char buf[MaxLineLen+1]; // 当前书目串(包括'\0')
   HString ch; // 索引字符串
   int BookNo; // 书号变量
   int i,k,l;
   Link p;
   InitString(ch); // 初始化HString类型的变量
   f=fopen("BookIdx.txt","r"); // 打开书名关键词索引表文件
   if(!f)
     exit(OVERFLOW);
   fscanf(f,"%d",&idxlist.last); // 书名关键词个数
   for(k=0;k<idxlist.last;k++) // 把关键词文件的内容拷到idxlist中
   {
     fscanf(f,"%s",buf);
     i=0;
     while(buf[i])
       buf[i++]=tolower(buf[i]); // 字母转为小写
     InitString(idxlist.item[k].key);
     StrAssign(idxlist.item[k].key,buf);
     InitList(idxlist.item[k].bnolist); // 初始化书号链表 bo2-6.cpp
     fscanf(f,"%d",&i);
     for(l=0;l<i;l++)
     {
       fscanf(f,"%d",&BookNo);
       if(!MakeNode(p,BookNo)) // 分配失败 bo2-6.cpp
         exit(OVERFLOW);
       p->next=NULL;
       Append(idxlist.item[k].bnolist,p); // 插入新的书号索引 bo2-6.cpp
     }
   }
   fclose(f);
   f=fopen("BookInfo.txt","r"); // 打开书目文件
   if(!f)
     exit(FALSE);
   i=0;
   while(!feof(f)) // 把书目文件的内容拷到booklist中
   {
     fgets(buf,MaxLineLen,f);
     booklist.item[i].bookno=(buf[0]-'0')*100+(buf[1]-'0')*10+(buf[2]-'0'); // 前三位为书号
     strcpy(booklist.item[i].bookname,buf);
     i++;
   }
   booklist.last=i;
   printf("请输入书目的关键词(一个)");
   scanf("%s",buf);
   i=0;
   while(buf[i])
     buf[i++]=tolower(buf[i]); // 字母转为小写
   StrAssign(ch,buf);
   i=0;
   do
   {
     k=StrCompare(ch,idxlist.item[i].key); // bo4-2.cpp
     i++;
   }while(k&&i<=idxlist.last);
   if(!k) // 索引表中有此关键词
   {
     p=idxlist.item[i-1].bnolist.head->next;
     while(p)
     {
       l=0;
       while(l<booklist.last&&p->data!=booklist.item[l].bookno)
         l++;
       if(l<booklist.last)
         printf("%s",booklist.item[l].bookname);
       p=p->next;
     }
   }
   else
     printf("没找到\n");
 }

⌨️ 快捷键说明

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