SlideShare une entreprise Scribd logo
1  sur  47
第四章 串操作应用举例
文本编辑

• 文本编辑程序是一个面向用户的系统服
务程序 , 广泛应用于源程序的输入和修
改 . 其实质是修改字符数据的形式或格
式 . 基本操作包括 : 串查找 , 插入和删
除 等基本操作 .
文本编辑
                                201
• 文本可以看成是一个
字符串 ( 文本串 ). 页 m a i n ( ) { ↵
是文本串的字串 , 行 f l o a t      a , b ,
是页的字串 .        m a x ; ↵     s c a
 文本 :                           n f   (   “ %f    ,   %f ”
 main() {                       ,&    a   , & b   )   ; ↵
      float a, b,max;             i   f     a >   b       m
      scanf(“%f,%f”, &a, &b);   a x   =   a ; e   l   s e
      if a>b max=a;             m a   x   = b ;   ↵   } ; ↵
      else max=b;
 };
文本编辑
• 建立文本串的 页表与行表
页表: 记录页号和对应页的起始行
号
行表:行号,对应行的起始地址和行字串的长度
。
   行号     起始地址   长度
   100     201   8
   101     209   17
   102     226   24
   103     250   17
   104     267   15
   105     282   2
文本编辑

• 文本串的操作的实现
设立页指针,行指针,字符指针,分别
指示当前操作的页,行和字符。

 插入与删除操作:
 字符的插入或删除
 行或页的插入与删除
文本编辑

• 文本串的操作的实现
 插入或删除字符

 • 修改行表中行的长度
 • 行超出设定的存储空间,需要重新分配存储
   空间,同时修改行的起始位置。
文本编辑

• 文本串的操作的实现
 行的插入或删除

• 删除行若为页的起始行,需要修改对应页的
  起始行号
• 行表的顺序存储要求,执行插入或删除操作
  时需移动插入或删除位置后的所有表项。
• 访问以页表和行表为索引,进行行或页的操
  作时只需对行表和页表进行修改,不涉及字
  符。
文本编辑程序的实现
                         HString   char

• 数据结构的描述                  ch
1. 采用串的堆分配存结构 length
c4-2.h
struct HString
{ char *ch; // 若是非空串 , 则按串长分配存储区 ;
    否则 ch 为 NULL
    int length; // 串长度
}
文本编辑程序的实现
2. 串的基本操作 bo4-2.cpp
void InitString(HString &T); // 串的初始化
void StrAssign(HString &T,char *chars) ; // 生成一
个其值等于串常量 c ha rs 的串 T
int Index(HString S, HString T, int pos);
//T 为非空子串 . 若主串 S 中第 p o s 个字符后
存在与 T 相等的子串 , 则返回第一个这样的子串
在 S 中的位置 ; 否则返回 0
文本编辑程序的实现
2. 串的基本操作 bo4-2.cpp
Status StrInsert(HString &S,int pos, HString T)// 在
串 S 的第 p o s 个字符前插入串 T
Status StrDelete(HString &S,int pos, int len) ; // 从
串 S 中删除第 p o s 个字符起长度为 le n 的子串
...
文本编辑程序的实现

• 引入的头文件及宏定义
#include “c1.h”
#include “c4-2.h” // 采用串的堆分配存储结构
#include “bo4-2.cpp” // 串的堆分配基本操作
#define MAX_LEN 25 // 文件的最大行数
#define LINE_LEN 75 // 每行字符数最大值 +1
#define NAME_LEN 20 // 文件名最大长度
文本编辑程序的实现 T
                                       第 1 行内容
                          [0]
 • 存储结构与全局变量                     n1
 HString T[MAX_LEN];                   第 2 行内容
                          [1]
 char str[LINE_LEN],             n2
 filename[NAME_LEN]=“”;                第 3 行内容
                          [2]
 FILE *fp;                       n3
 int n=0; // 文本行数                ...
                          [24]
str[pos] 为当前位置 pos 的字
符
T[n] 为当前的文本行
文本编辑程序的实现
•程序功能与算法描述
void Open( ) // 打开文件 , 若文件存在则打开 , 若不
存在则创建
void Save( ) // 存盘
void List( ) // 显示文本内容
void Insert( ) // 插入行
void Delete( ) // 删除行
void Search( ) // 查找字符串
...
文本编辑程序的实现
void Open( )
{ if (filename[0]) printf(“ 已存在打开的文件 n”); // 文件已
    打开
  else { printf(“ 请输入文件名 : ” ) ; scanf(“&s”,filename);
  fp = fopen(filename,“r”); // 以只读方式打开文件
  if(fp) // 已存在此文件
  { ...
     // 将文件中的内容输入至编辑系统的存储结构
    中
      fclose(fp); // 关闭文件
    }
    else printf(“ 新文件 n”);
    } //end else
}
文本编辑程序的实现
void Open( )
{ ...
    // 将文件中的内容输入至编辑系统的存储结构
    中
    while (fgets(str,LINE_LEN,fp)) // 由文件读入一行字
     符成功
    { str[strlen(str)-1] = 0; // 将换行符换为串结束符 0
        if (n>=MAX_LEN){
           printf(“ 文件太大 ” ); return;}
        StrAssign(T[n], str); // 读一行字符入当前行 T[n]
        n++;
      }
    ...
}
文本编辑程序的实现
void Insert () // 插入行操作
{ int i, l, m;
  printf(“ 在第 l 行前插入 m 行,输入 l 和 m: ”);
  scanf(“%d%d*c”,&l,&m);
  if ( n+m > MAX_LEN ) { printf(“ 插入行太多 n”); return;}
  if ( n>=l-1&&l>0) // 输入的插入位置满足条件
  for ( i=n-1; i>=l-1;i--)
            T[i+m]=T[i]; // 将插入位置后的行向后移动
  n+=m;
  printf(“ 请顺序输入待插入内容 :n”);
  ...
文本编辑程序的实现
void Insert () // 插入行操作
{ printf(“ 请顺序输入待插入内容 :n”);
    for(i=l-1; i<l-1+m;i++){
       gets(str);// 取插入字符串
       InitString(T[i]);
       StrAssign(T[i],str);
    }
  }
  else printf(“ 行超出范围 ” );
}
文本编辑程序的实现
void Search( ) // 查找字符串
{ int i,k,f=1; //f 为继续查找标志
  char b[2];
  HString s;
  printf(“ 请输入待查找的字符串: ” );
   scanf(“&s&*c”,str);
  InitString(s); StrAssign(s,str);
  for (i=0;i<n&&f;i++) { // 逐行查找
       k = 1; // 由每行的第一个字符开始查找
       while(k) {
           k=Index(T[i],s,k); // 由本行的第 k 个字符开始查
   找
           ...
文本编辑程序的实现
void Search( ) // 查找字符串
{ ...
    if (k) // 找到
   { printf(“ 第 %d 行: ” , i+1); strPrint(T[i]);
       pirntf(“ 第 %d 个字符处找到 n” , k);
       printf(“ 继续查找吗( Y/N)?”); gets(b);
       if (b[0]!=‘Y’&&b[0]!=‘y’) // 中断查找
       { f = 0; break; }
       else k++;
   } //end if(k)
  }//end while(k)
}//end for
if(f) printf(“ 没有找到 n”);
}
文本编辑程序的实现
main ( )
{ Status s = TRUE;
  int i,k;
  for( i = 0; i<MAX_LEN,i++) // 初始化串
              InitString (T[i]);
  while(s);
{ printf(“ 请选择 ” : 1. 打开文件         2. 显示文件内容 n”);
  printf(“             3. 插入行 4. 删除行 5. 拷贝行 6.
       修改行 n”);
  printf(“             7 . 查找字符串      8. 替换字符串 n”);
  printf(“             9. 存盘退出          0. 放弃编辑 n”);
  scanf(%d”,&k);
  ...
文本编辑程序的实现
main ( )
{ ...
  switch(k)
  { case 1: Open(); break;
       case 2: List( ); break;
       case 3: Insert( ); break;
           。。。
       case 0: s = FALSE;
   } //end switch(k)
} //end while(s)
}
建立词索引表
• 词的索引表常常用在信息检索中,
以图书馆的书目检索为例。在实际
系统图书馆的书目检索系统通常采
用 “书名关键词索引”
建立词索引表
书号              书名                关键词             书号索引
005   Computer Data Structure    algorithms    034
010   Introduction to Data        analysis     034,050,067
      Structures                 computer      005,034
023   Fundamentals of Data         data        005,010,023
      Structures                   design      034
034   The Design and Analysis   fundamentals 023
      of Computer algorithms
                                Introduction   010,050
050   Introduction to
      Numerical Analysis         numerical     050,067
067   Numerical Analysis         structures    005,010,023

       书目文件                          关键词索引表
关键词索引表通常按词典顺序排列
建立索引表                            关键词                 书号索引
                               algorithms      034
例如 :                             analysis      034,050,067
                                computer       005,034
1. 检索词 :
“algorithms”+“analysis”              data      005,010,023
                                      ...      ...
结果:
(034)=(034)∧ (034,050,067)                   书目文件
                               书号                    书名
                               ...     ...
                               034     The Design and Analysis of
                                       Computer algorithms
                               ...     ...
建立索引表                              关键词              书号索引

                           algorithms        034
2. 检索词 :                   analysis          034,050,067
“algorithms” ,“analysis”            ...      ...
结果:
                           书号                  书名
034,050,067=(034)∨ (034,
                             ...                   ...
050,067)                    034     The Design and Analysis of
                                    Computer algorithms
                            050     Introduction to Numerical
                                    Analysis
                            067     Numerical Analysis
如何建立索引表
重复下列操作直至文件结束:
(2) 从书目文件中读入一个书目串
(3) 从书目串中提取所有关键词插入词表
(4) 对词表中的每一个关键词,在索引表
  中进行查找并作相应的插入操作。
如何建立索引表
重复下列操作直至文件结束:
(2) 从书目文件中读入一个书目串
      BookInfo.txt
 005                           buf
 Computer Data Structures      005 Computer Data
 010                           Structures
 Introduction to Data
 Structures
 023                          从书目文件中读取 1
 Fundamentals of Data
 Structures                   行信息到缓存 b uf
 034
 The Design and Analysis of
 Computer
 ...
如何建立索引表                 wdlist

                      [0]            Computer
(2) 从书目串中提取                            Data
                      [1]
    所有关键词插入
                      [2]            Structure
    词表
   buf
 005 Computer
 Data Structures      ...    ...


 BookNo     005
                      [9]
                      last       3
                   当前书号对应的关键词
                   表
如何建立索引表
(3) 对词表中的每一个关键词,在索引表中进行查找并
 作相应的插入操作。
                              关键词                书号索引

  [0]           Computer     algorithms    034

  [1]             Data        analysis     034,050,067
                              computer     034 005
  [2]           Structure
                               data        010,023 005
                               design      034
   ...    ...
                            fundamentals   023
                            Introduction   010,050
  [9]
                             numerical     050,067
  last    3
     wdlist                  structure     010,023 005
如何建立索引表
提取关键词的处理:
     顺序扫描书目单,分词,剔除常
用词,为此设定“停用词表”。将确定为
关键词的词汇置于临时词表中。
书名: The Design and Analysis of Computer algorithms
剔除: The and of
关键词: Design Analysis Computer Algorithm
如何建立索引表

• 索引表中查询关键词                关键词
                          Algorithm     034
                                              书号索引

  的处理                                             034
书 034 : The Design and     analysis     050,067
Analysis of Computer      computer      005,034
algorithms                   data       005,010,023
关键词: Design Analysis
Computer Algorithm          design      034
                         fundamentals   023
Analysis                 Introduction   010,050
                          numerical     050,067
Algorithm
                          structures    005,010,023
如何建立索引表                       wdlist

                            [0]            design

•数据结构                       [1]            Analysis

词表: 线性表,存放关键                [2]            Computer
词                           [3]            Algorithms
Typ e d e f s truc t{        ...   ...
c ha r * ite m[ ] ; // 字符
串的数组                        [9]
 int la s t ; // 词表的长度      last       4
} Wo rld L is tTyp e ; //
词表类型(顺序表)
如何建立索引表

•数据结构
书号类型:
Typedef int ElemType; // 书号类型
索引项类型:
Typedef struct{
   HString key; // 关键词
   LinkList bnolist;// 存放书号索引的链
表
} IdxTermType; // 索引项类型
如何建立索引表

•数据结构
索引表类型(有序表)
Typedef struct{
   IndexTermType
item[MaxKeyNum+1]
   int last;
} IndexListType;
关键词               key   bnolist
                                         存放书号索引的链表
                    NULL          0          NULL
             [0]
                         0
Algorithms                        1
                                                    34   NULL
                         10
 Analysis                         3
                                                    34
                         8
                                                    50
                   ...
                                                    67   NULL


                                  1
[MaxKeyNum]                               索引表 idxlist 的结构
        last       9                          ( 有序表 )
如何建立索引表                noidx

                     [0]           A
•主要变量                [1]           An
Char *buf; // 书目缓    [2]           The
冲区                   [3]           Of
WordListType         ...    ...
wdlist; // 词表 , 存放
书目的关键词包括 ,           [9]
停用词表也采用              last      4
WordListType
如何建立索引表

•基本操作
void InitInxList(IndxListType &idxlist);
// 初始化操作 , 置索引表 idxlist 为空表 , 且在
idxlist.item[0] 设一空串
void GetLine(FILE f); // 从文件 f 读入一个书目信
息到书目缓冲区 buf
void ExtractKeyWord(ElemType &bno); // 从 buf
中提取书名关键词到词表 wdlist, 书号存入 bno
如何建立索引表

•基本操作
Status InsIndxList(IdxListType &idxlist, ElemType
bno); // 将书号为 bno 的书名关键词按照词典顺
序插入索引表 idxlist 中

Void PutText(FILE g, IdxListType idxlist); // 将生
成的索引表 idxlist 输出到文件 g
如何建立索引表
 •主函数
void main( ){
If (f = openf(“BookInfo.txt”,”r”))
  if( g = openf(“BookIdx.txt”,”w”)){
  InitIndxList(idxlist);   // 初始化索引表为空表
  while(!feof(f)) {
   GetLine(f); // 从文件 f 读入一个书目信息到 buf
   ExtractKeyWord(BookNo); // 从 buf 提取关键词到词表 , 书号存入 BookN
   InsIdxList(idxlist, BookNo); // 将书号为 BookNo 的关键词插入索引表
  } //end while
  PutText(g, idxlist); // 将生成的索引表 idxlist 输出到文件 g
}}//end main
如何建立索引表
初始化操作
void InitIdxList( IdxListType &idxlist)
{ // 置索引表 idxlist 为空表 , 且在 idxlist.item[0] 设空串
  idxlist.last = 0;
  InitString(idxlist.item[0].key); // 初始化 [0] 单元
  InitList(idxlist.item[0].bnolist); // 初始化 [0] 单元
}
                           idxlist
                  NULL      0
            [0]                          NULL
                  0
           last   0
如何建立索引表
在索引表上进行插入的操作
Status InsIdxList(IdxListType &idxlist,int bno)
// 将书号为 bno 的关键词插入索引表
{ for (i=0; i<wdlist.last;++i){
 GetWord(i,wd); // 用 wd 返回词表 wdlist 中第 i 个关键词
 j = Locate(idxlist,wd,b); // 在索引表 idxlist 中查询是否存
 在与 wd 相等的关键词 . 若存在 , 则返回其在索引表中的
 位置 , 且 b 取 TRUE; 否则返回插入位置 , 且 b 取值
 FALSE
 ...
如何建立索引表
在索引表上进行插入的操作
Status InsIdxList(IdxListType &idxlist,int bno)
{ ...
 if(!b) InsertNewKey(idxlist,j,wd); // 在索引表 idxlist 的第 j
 项上插入新关键词 , 并初始化书号索引的链表为空表
 if(!InsertBook(idxlist,j,bno)) return OVERFLOW; // 在索引
 表 idxListType 的第 i 项中插入书号为 bno 的索引
 } //end for
 return OK;
}//InsertIdexList
如何建立索引表
四个操作的具体实现

Void GetWord( int i, HString &wd) {
// 用 wd 返回词表 wdlist 中第 i 个关键词
   p = *(wdlist.item+i); // 取词表中第 i 个字符串
   StrAssign(wd,p); // 生成关键字字符串
}
如何建立索引表
四个操作的具体实现
int Locate(idxListType &idxlist, HString wd,Boolean &b)
// 在索引表 idxlist 中查询是否存在与 wd 相等的关键词 . 若存在 , 则
  返回其在索引表中的位置 , 且 b 取 TRUE; 否则返回插入位置 , 且
  b 取值 FALSE
{ for (i = idlist.last-1;
      (m=StrCompare(idxlist.item[i].key,wd))>0; --i);
  if (m==0) { b=TRUE; return i;} // 找到
  else (b=FALSE; return i+1;}
} //end locate
如何建立索引表
void InsertNewKey(int i, StrType wd) {
// 在索引表 idxlist 的第 j 项上插入新关键词 , 并初始化书号索
引的链表为空表
  for(j=idxlist.last-1;j>=i; --j)
      idxlist.item[j+1] = idlist.item[j]; // 后移索引项
  // 插入新的索引项
  StrCopy(idxlist.item[i].key,wd); // 串赋值
  InitList(idxlist.item[i].bnolist); // 初始化书号索引表为空表
  ++idxlist.last;
} //InsertNewKey
如何建立索引表
Status InsertBook (IdxListType &idxlist, int I, int bno) {
// 在索引表 idxListType 的第 i 项中插入书号为 bno 的索引

  if (!MakeNode(p,bno) return ERROR; // 分配失败
  Appand(idxlist.item[i].bnolist,p); // 插入新的书号索引
  return OK;
} //InsertBook
关键词                key   bnolist
                                           存放书号索引的链表
                      NULL          0         NULL
              [0]
                           0
 Algorithms                         1
                                                     34   NULL
                           10
  Analysis                          3
                                                     34
                           8
                                                     50   NULL
                     ...
       P
     67       NULL


Appand(idxlist.item[i].bnolist,p); // 插入新的书号索引

Contenu connexe

Tendances

JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1Sheng-Han Su
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记Lingfei Kong
 
STL Container Usage Tips
STL Container Usage TipsSTL Container Usage Tips
STL Container Usage Tips永泉 韩
 
C程式-陣列與指標
C程式-陣列與指標C程式-陣列與指標
C程式-陣列與指標艾鍗科技
 
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509tidesq
 
《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)jane2006
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509tidesq
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Derek Lee
 
Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Derek Lee
 
C程式-函式與巨集
C程式-函式與巨集C程式-函式與巨集
C程式-函式與巨集艾鍗科技
 
Mysql 高级优化之 理解查询执行
Mysql 高级优化之 理解查询执行Mysql 高级优化之 理解查询执行
Mysql 高级优化之 理解查询执行nigel889
 
Data Analysis with Python - Pandas | WeiYuan
Data Analysis with Python - Pandas | WeiYuanData Analysis with Python - Pandas | WeiYuan
Data Analysis with Python - Pandas | WeiYuanWei-Yuan Chang
 

Tendances (20)

Ch10 範例
Ch10 範例Ch10 範例
Ch10 範例
 
JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1JavaScript 快速複習 2017Q1
JavaScript 快速複習 2017Q1
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
Ch10
Ch10Ch10
Ch10
 
Scala+RDD
Scala+RDDScala+RDD
Scala+RDD
 
Ch12 教學
Ch12 教學Ch12 教學
Ch12 教學
 
Ch8 教學
Ch8 教學Ch8 教學
Ch8 教學
 
STL Container Usage Tips
STL Container Usage TipsSTL Container Usage Tips
STL Container Usage Tips
 
C程式-陣列與指標
C程式-陣列與指標C程式-陣列與指標
C程式-陣列與指標
 
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509
Linux c++ 编程之链接与装载 -提高篇--v0.3--20120509
 
《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)《Java程序设计》期末考试试题 (六)
《Java程序设计》期末考试试题 (六)
 
Ch7 範例
Ch7 範例Ch7 範例
Ch7 範例
 
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
Linux c++ 编程之链接与装载 -基础篇--v0.3--20120509
 
twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹twMVC#27 | C# 7.0 新功能介紹
twMVC#27 | C# 7.0 新功能介紹
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 
Python入門:5大概念初心者必備
Python入門:5大概念初心者必備Python入門:5大概念初心者必備
Python入門:5大概念初心者必備
 
C程式-函式與巨集
C程式-函式與巨集C程式-函式與巨集
C程式-函式與巨集
 
Mysql 高级优化之 理解查询执行
Mysql 高级优化之 理解查询执行Mysql 高级优化之 理解查询执行
Mysql 高级优化之 理解查询执行
 
Appendix B 範例
Appendix B 範例Appendix B 範例
Appendix B 範例
 
Data Analysis with Python - Pandas | WeiYuan
Data Analysis with Python - Pandas | WeiYuanData Analysis with Python - Pandas | WeiYuan
Data Analysis with Python - Pandas | WeiYuan
 

En vedette

Asesoria De Sistema
Asesoria De SistemaAsesoria De Sistema
Asesoria De Sistemaalexasecaira
 
Dia da Área de Projecto - exposição de trabalhos
Dia da Área de Projecto - exposição de trabalhosDia da Área de Projecto - exposição de trabalhos
Dia da Área de Projecto - exposição de trabalhosO Ciclista
 
Aula4 Trabalhandoideias
Aula4 TrabalhandoideiasAula4 Trabalhandoideias
Aula4 TrabalhandoideiasLuciane Lira
 
Corta-Mato em Vagos
Corta-Mato em VagosCorta-Mato em Vagos
Corta-Mato em VagosO Ciclista
 
Imagens da Viagem ao Alentejo 2
Imagens da Viagem ao Alentejo 2Imagens da Viagem ao Alentejo 2
Imagens da Viagem ao Alentejo 2O Ciclista
 
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...Rosa Cristina Parra Lozano
 
Well system
Well system   Well system
Well system qifey
 
G5 Brasil Qualificação de Leads2014
G5 Brasil Qualificação de Leads2014G5 Brasil Qualificação de Leads2014
G5 Brasil Qualificação de Leads2014G5 Brasil
 
Audacityを使った音声ノイズ除去手順
Audacityを使った音声ノイズ除去手順Audacityを使った音声ノイズ除去手順
Audacityを使った音声ノイズ除去手順Tasuku Abe
 
第二章 线性表
第二章 线性表第二章 线性表
第二章 线性表Wang Yizhe
 
2014 07-02 - trabalhos alunos 7º f
2014 07-02 - trabalhos alunos 7º f2014 07-02 - trabalhos alunos 7º f
2014 07-02 - trabalhos alunos 7º fO Ciclista
 
中國信託增美鑫
中國信託增美鑫中國信託增美鑫
中國信託增美鑫huangmcintyre
 
2012410172749 (2)
2012410172749 (2)2012410172749 (2)
2012410172749 (2)Buu Dang
 

En vedette (20)

Asesoria De Sistema
Asesoria De SistemaAsesoria De Sistema
Asesoria De Sistema
 
Dia da Área de Projecto - exposição de trabalhos
Dia da Área de Projecto - exposição de trabalhosDia da Área de Projecto - exposição de trabalhos
Dia da Área de Projecto - exposição de trabalhos
 
Aula4 Trabalhandoideias
Aula4 TrabalhandoideiasAula4 Trabalhandoideias
Aula4 Trabalhandoideias
 
Corta-Mato em Vagos
Corta-Mato em VagosCorta-Mato em Vagos
Corta-Mato em Vagos
 
Imagens da Viagem ao Alentejo 2
Imagens da Viagem ao Alentejo 2Imagens da Viagem ao Alentejo 2
Imagens da Viagem ao Alentejo 2
 
Zeca Pagodinho
Zeca  PagodinhoZeca  Pagodinho
Zeca Pagodinho
 
Brigada procedimientos marzo17
Brigada procedimientos marzo17Brigada procedimientos marzo17
Brigada procedimientos marzo17
 
Konsep proker
Konsep prokerKonsep proker
Konsep proker
 
Fgos1
Fgos1Fgos1
Fgos1
 
Goldbourt
GoldbourtGoldbourt
Goldbourt
 
Sapatossujos
SapatossujosSapatossujos
Sapatossujos
 
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...
Gestión de información y conocimiento en Salud Pública y la usabilidad en la ...
 
Well system
Well system   Well system
Well system
 
G5 Brasil Qualificação de Leads2014
G5 Brasil Qualificação de Leads2014G5 Brasil Qualificação de Leads2014
G5 Brasil Qualificação de Leads2014
 
Cyn
CynCyn
Cyn
 
Audacityを使った音声ノイズ除去手順
Audacityを使った音声ノイズ除去手順Audacityを使った音声ノイズ除去手順
Audacityを使った音声ノイズ除去手順
 
第二章 线性表
第二章 线性表第二章 线性表
第二章 线性表
 
2014 07-02 - trabalhos alunos 7º f
2014 07-02 - trabalhos alunos 7º f2014 07-02 - trabalhos alunos 7º f
2014 07-02 - trabalhos alunos 7º f
 
中國信託增美鑫
中國信託增美鑫中國信託增美鑫
中國信託增美鑫
 
2012410172749 (2)
2012410172749 (2)2012410172749 (2)
2012410172749 (2)
 

Similaire à 第四章 串操作应用举例

Python 脚本入门基础
Python 脚本入门基础Python 脚本入门基础
Python 脚本入门基础wklken
 
Information Retrieval
Information RetrievalInformation Retrieval
Information Retrievalyxyx3258
 
ajax_onlinemad
ajax_onlinemadajax_onlinemad
ajax_onlinemadKitor23
 
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社pingjiang
 
12, string
12, string12, string
12, stringted-xu
 
Itpub电子杂志第四期第二稿
Itpub电子杂志第四期第二稿Itpub电子杂志第四期第二稿
Itpub电子杂志第四期第二稿yiditushe
 
神州泰岳测试试题(笔试)
神州泰岳测试试题(笔试)神州泰岳测试试题(笔试)
神州泰岳测试试题(笔试)yiditushe
 
搜索引擎技术介绍
搜索引擎技术介绍搜索引擎技术介绍
搜索引擎技术介绍bigqiang zou
 
An introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubAn introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubjiangxu
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2terry28853669
 
Lotus domino开发教程
Lotus domino开发教程Lotus domino开发教程
Lotus domino开发教程dyzm_2000
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程deer hope
 
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC
 
Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南HonestQiao
 

Similaire à 第四章 串操作应用举例 (20)

Python 脚本入门基础
Python 脚本入门基础Python 脚本入门基础
Python 脚本入门基础
 
Information Retrieval
Information RetrievalInformation Retrieval
Information Retrieval
 
ajax_onlinemad
ajax_onlinemadajax_onlinemad
ajax_onlinemad
 
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社
数据结构(用面向对象方法与C++语言描述第二版)殷人昆编著清华大学出版社
 
12, string
12, string12, string
12, string
 
Itpub电子杂志第四期第二稿
Itpub电子杂志第四期第二稿Itpub电子杂志第四期第二稿
Itpub电子杂志第四期第二稿
 
香港六合彩
香港六合彩香港六合彩
香港六合彩
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
getPDF.aspx
getPDF.aspxgetPDF.aspx
getPDF.aspx
 
HW4_0711282.pdf
HW4_0711282.pdfHW4_0711282.pdf
HW4_0711282.pdf
 
神州泰岳测试试题(笔试)
神州泰岳测试试题(笔试)神州泰岳测试试题(笔试)
神州泰岳测试试题(笔试)
 
搜索引擎技术介绍
搜索引擎技术介绍搜索引擎技术介绍
搜索引擎技术介绍
 
An introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pubAn introduce to n hibernate (part 1) pub
An introduce to n hibernate (part 1) pub
 
Python 温故
Python 温故Python 温故
Python 温故
 
Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2Excel函數進階班(北市政府公訓處) 2
Excel函數進階班(北市政府公訓處) 2
 
[系列活動] Python 爬蟲實戰
[系列活動] Python 爬蟲實戰[系列活動] Python 爬蟲實戰
[系列活動] Python 爬蟲實戰
 
Lotus domino开发教程
Lotus domino开发教程Lotus domino开发教程
Lotus domino开发教程
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程
 
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning ServicestwMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
twMVC#46_SQL Server 資料分析大躍進 Machine Learning Services
 
Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南Coreseek/Sphinx 全文检索实践指南
Coreseek/Sphinx 全文检索实践指南
 

Plus de Wang Yizhe

第六章 树和二叉树2
第六章 树和二叉树2第六章 树和二叉树2
第六章 树和二叉树2Wang Yizhe
 
第九章+查找[4]
第九章+查找[4]第九章+查找[4]
第九章+查找[4]Wang Yizhe
 
第九章 查找[3]
第九章 查找[3]第九章 查找[3]
第九章 查找[3]Wang Yizhe
 
第九章 查找[2]
第九章 查找[2]第九章 查找[2]
第九章 查找[2]Wang Yizhe
 
第九章 查找[1]
第九章 查找[1]第九章 查找[1]
第九章 查找[1]Wang Yizhe
 
第三章 栈和队列(新)
第三章 栈和队列(新)第三章 栈和队列(新)
第三章 栈和队列(新)Wang Yizhe
 
第三章 栈和队列
第三章 栈和队列第三章 栈和队列
第三章 栈和队列Wang Yizhe
 
第七章 图[4]1
第七章 图[4]1第七章 图[4]1
第七章 图[4]1Wang Yizhe
 
第七章 图[3]
第七章 图[3]第七章 图[3]
第七章 图[3]Wang Yizhe
 
第七章 图[2]1
第七章 图[2]1第七章 图[2]1
第七章 图[2]1Wang Yizhe
 
第七章 图[1]
第七章 图[1]第七章 图[1]
第七章 图[1]Wang Yizhe
 
数据结构 总结与复习
数据结构 总结与复习数据结构 总结与复习
数据结构 总结与复习Wang Yizhe
 

Plus de Wang Yizhe (14)

第四章 串
第四章 串第四章 串
第四章 串
 
第六章 树和二叉树2
第六章 树和二叉树2第六章 树和二叉树2
第六章 树和二叉树2
 
第六章1
第六章1第六章1
第六章1
 
第九章+查找[4]
第九章+查找[4]第九章+查找[4]
第九章+查找[4]
 
第九章 查找[3]
第九章 查找[3]第九章 查找[3]
第九章 查找[3]
 
第九章 查找[2]
第九章 查找[2]第九章 查找[2]
第九章 查找[2]
 
第九章 查找[1]
第九章 查找[1]第九章 查找[1]
第九章 查找[1]
 
第三章 栈和队列(新)
第三章 栈和队列(新)第三章 栈和队列(新)
第三章 栈和队列(新)
 
第三章 栈和队列
第三章 栈和队列第三章 栈和队列
第三章 栈和队列
 
第七章 图[4]1
第七章 图[4]1第七章 图[4]1
第七章 图[4]1
 
第七章 图[3]
第七章 图[3]第七章 图[3]
第七章 图[3]
 
第七章 图[2]1
第七章 图[2]1第七章 图[2]1
第七章 图[2]1
 
第七章 图[1]
第七章 图[1]第七章 图[1]
第七章 图[1]
 
数据结构 总结与复习
数据结构 总结与复习数据结构 总结与复习
数据结构 总结与复习
 

第四章 串操作应用举例

  • 2. 文本编辑 • 文本编辑程序是一个面向用户的系统服 务程序 , 广泛应用于源程序的输入和修 改 . 其实质是修改字符数据的形式或格 式 . 基本操作包括 : 串查找 , 插入和删 除 等基本操作 .
  • 3. 文本编辑 201 • 文本可以看成是一个 字符串 ( 文本串 ). 页 m a i n ( ) { ↵ 是文本串的字串 , 行 f l o a t a , b , 是页的字串 . m a x ; ↵ s c a 文本 : n f ( “ %f , %f ” main() { ,& a , & b ) ; ↵ float a, b,max; i f a > b m scanf(“%f,%f”, &a, &b); a x = a ; e l s e if a>b max=a; m a x = b ; ↵ } ; ↵ else max=b; };
  • 4. 文本编辑 • 建立文本串的 页表与行表 页表: 记录页号和对应页的起始行 号 行表:行号,对应行的起始地址和行字串的长度 。 行号 起始地址 长度 100 201 8 101 209 17 102 226 24 103 250 17 104 267 15 105 282 2
  • 6. 文本编辑 • 文本串的操作的实现 插入或删除字符 • 修改行表中行的长度 • 行超出设定的存储空间,需要重新分配存储 空间,同时修改行的起始位置。
  • 7. 文本编辑 • 文本串的操作的实现 行的插入或删除 • 删除行若为页的起始行,需要修改对应页的 起始行号 • 行表的顺序存储要求,执行插入或删除操作 时需移动插入或删除位置后的所有表项。 • 访问以页表和行表为索引,进行行或页的操 作时只需对行表和页表进行修改,不涉及字 符。
  • 8. 文本编辑程序的实现 HString char • 数据结构的描述 ch 1. 采用串的堆分配存结构 length c4-2.h struct HString { char *ch; // 若是非空串 , 则按串长分配存储区 ; 否则 ch 为 NULL int length; // 串长度 }
  • 9. 文本编辑程序的实现 2. 串的基本操作 bo4-2.cpp void InitString(HString &T); // 串的初始化 void StrAssign(HString &T,char *chars) ; // 生成一 个其值等于串常量 c ha rs 的串 T int Index(HString S, HString T, int pos); //T 为非空子串 . 若主串 S 中第 p o s 个字符后 存在与 T 相等的子串 , 则返回第一个这样的子串 在 S 中的位置 ; 否则返回 0
  • 10. 文本编辑程序的实现 2. 串的基本操作 bo4-2.cpp Status StrInsert(HString &S,int pos, HString T)// 在 串 S 的第 p o s 个字符前插入串 T Status StrDelete(HString &S,int pos, int len) ; // 从 串 S 中删除第 p o s 个字符起长度为 le n 的子串 ...
  • 11. 文本编辑程序的实现 • 引入的头文件及宏定义 #include “c1.h” #include “c4-2.h” // 采用串的堆分配存储结构 #include “bo4-2.cpp” // 串的堆分配基本操作 #define MAX_LEN 25 // 文件的最大行数 #define LINE_LEN 75 // 每行字符数最大值 +1 #define NAME_LEN 20 // 文件名最大长度
  • 12. 文本编辑程序的实现 T 第 1 行内容 [0] • 存储结构与全局变量 n1 HString T[MAX_LEN]; 第 2 行内容 [1] char str[LINE_LEN], n2 filename[NAME_LEN]=“”; 第 3 行内容 [2] FILE *fp; n3 int n=0; // 文本行数 ... [24] str[pos] 为当前位置 pos 的字 符 T[n] 为当前的文本行
  • 13. 文本编辑程序的实现 •程序功能与算法描述 void Open( ) // 打开文件 , 若文件存在则打开 , 若不 存在则创建 void Save( ) // 存盘 void List( ) // 显示文本内容 void Insert( ) // 插入行 void Delete( ) // 删除行 void Search( ) // 查找字符串 ...
  • 14. 文本编辑程序的实现 void Open( ) { if (filename[0]) printf(“ 已存在打开的文件 n”); // 文件已 打开 else { printf(“ 请输入文件名 : ” ) ; scanf(“&s”,filename); fp = fopen(filename,“r”); // 以只读方式打开文件 if(fp) // 已存在此文件 { ... // 将文件中的内容输入至编辑系统的存储结构 中 fclose(fp); // 关闭文件 } else printf(“ 新文件 n”); } //end else }
  • 15. 文本编辑程序的实现 void Open( ) { ... // 将文件中的内容输入至编辑系统的存储结构 中 while (fgets(str,LINE_LEN,fp)) // 由文件读入一行字 符成功 { str[strlen(str)-1] = 0; // 将换行符换为串结束符 0 if (n>=MAX_LEN){ printf(“ 文件太大 ” ); return;} StrAssign(T[n], str); // 读一行字符入当前行 T[n] n++; } ... }
  • 16. 文本编辑程序的实现 void Insert () // 插入行操作 { int i, l, m; printf(“ 在第 l 行前插入 m 行,输入 l 和 m: ”); scanf(“%d%d*c”,&l,&m); if ( n+m > MAX_LEN ) { printf(“ 插入行太多 n”); return;} if ( n>=l-1&&l>0) // 输入的插入位置满足条件 for ( i=n-1; i>=l-1;i--) T[i+m]=T[i]; // 将插入位置后的行向后移动 n+=m; printf(“ 请顺序输入待插入内容 :n”); ...
  • 17. 文本编辑程序的实现 void Insert () // 插入行操作 { printf(“ 请顺序输入待插入内容 :n”); for(i=l-1; i<l-1+m;i++){ gets(str);// 取插入字符串 InitString(T[i]); StrAssign(T[i],str); } } else printf(“ 行超出范围 ” ); }
  • 18. 文本编辑程序的实现 void Search( ) // 查找字符串 { int i,k,f=1; //f 为继续查找标志 char b[2]; HString s; printf(“ 请输入待查找的字符串: ” ); scanf(“&s&*c”,str); InitString(s); StrAssign(s,str); for (i=0;i<n&&f;i++) { // 逐行查找 k = 1; // 由每行的第一个字符开始查找 while(k) { k=Index(T[i],s,k); // 由本行的第 k 个字符开始查 找 ...
  • 19. 文本编辑程序的实现 void Search( ) // 查找字符串 { ... if (k) // 找到 { printf(“ 第 %d 行: ” , i+1); strPrint(T[i]); pirntf(“ 第 %d 个字符处找到 n” , k); printf(“ 继续查找吗( Y/N)?”); gets(b); if (b[0]!=‘Y’&&b[0]!=‘y’) // 中断查找 { f = 0; break; } else k++; } //end if(k) }//end while(k) }//end for if(f) printf(“ 没有找到 n”); }
  • 20. 文本编辑程序的实现 main ( ) { Status s = TRUE; int i,k; for( i = 0; i<MAX_LEN,i++) // 初始化串 InitString (T[i]); while(s); { printf(“ 请选择 ” : 1. 打开文件 2. 显示文件内容 n”); printf(“ 3. 插入行 4. 删除行 5. 拷贝行 6. 修改行 n”); printf(“ 7 . 查找字符串 8. 替换字符串 n”); printf(“ 9. 存盘退出 0. 放弃编辑 n”); scanf(%d”,&k); ...
  • 21. 文本编辑程序的实现 main ( ) { ... switch(k) { case 1: Open(); break; case 2: List( ); break; case 3: Insert( ); break; 。。。 case 0: s = FALSE; } //end switch(k) } //end while(s) }
  • 23. 建立词索引表 书号 书名 关键词 书号索引 005 Computer Data Structure algorithms 034 010 Introduction to Data analysis 034,050,067 Structures computer 005,034 023 Fundamentals of Data data 005,010,023 Structures design 034 034 The Design and Analysis fundamentals 023 of Computer algorithms Introduction 010,050 050 Introduction to Numerical Analysis numerical 050,067 067 Numerical Analysis structures 005,010,023 书目文件 关键词索引表
  • 24. 关键词索引表通常按词典顺序排列 建立索引表 关键词 书号索引 algorithms 034 例如 : analysis 034,050,067 computer 005,034 1. 检索词 : “algorithms”+“analysis” data 005,010,023 ... ... 结果: (034)=(034)∧ (034,050,067) 书目文件 书号 书名 ... ... 034 The Design and Analysis of Computer algorithms ... ...
  • 25. 建立索引表 关键词 书号索引 algorithms 034 2. 检索词 : analysis 034,050,067 “algorithms” ,“analysis” ... ... 结果: 书号 书名 034,050,067=(034)∨ (034, ... ... 050,067) 034 The Design and Analysis of Computer algorithms 050 Introduction to Numerical Analysis 067 Numerical Analysis
  • 27. 如何建立索引表 重复下列操作直至文件结束: (2) 从书目文件中读入一个书目串 BookInfo.txt 005 buf Computer Data Structures 005 Computer Data 010 Structures Introduction to Data Structures 023 从书目文件中读取 1 Fundamentals of Data Structures 行信息到缓存 b uf 034 The Design and Analysis of Computer ...
  • 28. 如何建立索引表 wdlist [0] Computer (2) 从书目串中提取 Data [1] 所有关键词插入 [2] Structure 词表 buf 005 Computer Data Structures ... ... BookNo 005 [9] last 3 当前书号对应的关键词 表
  • 29. 如何建立索引表 (3) 对词表中的每一个关键词,在索引表中进行查找并 作相应的插入操作。 关键词 书号索引 [0] Computer algorithms 034 [1] Data analysis 034,050,067 computer 034 005 [2] Structure data 010,023 005 design 034 ... ... fundamentals 023 Introduction 010,050 [9] numerical 050,067 last 3 wdlist structure 010,023 005
  • 30. 如何建立索引表 提取关键词的处理: 顺序扫描书目单,分词,剔除常 用词,为此设定“停用词表”。将确定为 关键词的词汇置于临时词表中。 书名: The Design and Analysis of Computer algorithms 剔除: The and of 关键词: Design Analysis Computer Algorithm
  • 31. 如何建立索引表 • 索引表中查询关键词 关键词 Algorithm 034 书号索引 的处理 034 书 034 : The Design and analysis 050,067 Analysis of Computer computer 005,034 algorithms data 005,010,023 关键词: Design Analysis Computer Algorithm design 034 fundamentals 023 Analysis Introduction 010,050 numerical 050,067 Algorithm structures 005,010,023
  • 32. 如何建立索引表 wdlist [0] design •数据结构 [1] Analysis 词表: 线性表,存放关键 [2] Computer 词 [3] Algorithms Typ e d e f s truc t{ ... ... c ha r * ite m[ ] ; // 字符 串的数组 [9] int la s t ; // 词表的长度 last 4 } Wo rld L is tTyp e ; // 词表类型(顺序表)
  • 33. 如何建立索引表 •数据结构 书号类型: Typedef int ElemType; // 书号类型 索引项类型: Typedef struct{ HString key; // 关键词 LinkList bnolist;// 存放书号索引的链 表 } IdxTermType; // 索引项类型
  • 34. 如何建立索引表 •数据结构 索引表类型(有序表) Typedef struct{ IndexTermType item[MaxKeyNum+1] int last; } IndexListType;
  • 35. 关键词 key bnolist 存放书号索引的链表 NULL 0 NULL [0] 0 Algorithms 1 34 NULL 10 Analysis 3 34 8 50 ... 67 NULL 1 [MaxKeyNum] 索引表 idxlist 的结构 last 9 ( 有序表 )
  • 36. 如何建立索引表 noidx [0] A •主要变量 [1] An Char *buf; // 书目缓 [2] The 冲区 [3] Of WordListType ... ... wdlist; // 词表 , 存放 书目的关键词包括 , [9] 停用词表也采用 last 4 WordListType
  • 37. 如何建立索引表 •基本操作 void InitInxList(IndxListType &idxlist); // 初始化操作 , 置索引表 idxlist 为空表 , 且在 idxlist.item[0] 设一空串 void GetLine(FILE f); // 从文件 f 读入一个书目信 息到书目缓冲区 buf void ExtractKeyWord(ElemType &bno); // 从 buf 中提取书名关键词到词表 wdlist, 书号存入 bno
  • 38. 如何建立索引表 •基本操作 Status InsIndxList(IdxListType &idxlist, ElemType bno); // 将书号为 bno 的书名关键词按照词典顺 序插入索引表 idxlist 中 Void PutText(FILE g, IdxListType idxlist); // 将生 成的索引表 idxlist 输出到文件 g
  • 39. 如何建立索引表 •主函数 void main( ){ If (f = openf(“BookInfo.txt”,”r”)) if( g = openf(“BookIdx.txt”,”w”)){ InitIndxList(idxlist); // 初始化索引表为空表 while(!feof(f)) { GetLine(f); // 从文件 f 读入一个书目信息到 buf ExtractKeyWord(BookNo); // 从 buf 提取关键词到词表 , 书号存入 BookN InsIdxList(idxlist, BookNo); // 将书号为 BookNo 的关键词插入索引表 } //end while PutText(g, idxlist); // 将生成的索引表 idxlist 输出到文件 g }}//end main
  • 40. 如何建立索引表 初始化操作 void InitIdxList( IdxListType &idxlist) { // 置索引表 idxlist 为空表 , 且在 idxlist.item[0] 设空串 idxlist.last = 0; InitString(idxlist.item[0].key); // 初始化 [0] 单元 InitList(idxlist.item[0].bnolist); // 初始化 [0] 单元 } idxlist NULL 0 [0] NULL 0 last 0
  • 41. 如何建立索引表 在索引表上进行插入的操作 Status InsIdxList(IdxListType &idxlist,int bno) // 将书号为 bno 的关键词插入索引表 { for (i=0; i<wdlist.last;++i){ GetWord(i,wd); // 用 wd 返回词表 wdlist 中第 i 个关键词 j = Locate(idxlist,wd,b); // 在索引表 idxlist 中查询是否存 在与 wd 相等的关键词 . 若存在 , 则返回其在索引表中的 位置 , 且 b 取 TRUE; 否则返回插入位置 , 且 b 取值 FALSE ...
  • 42. 如何建立索引表 在索引表上进行插入的操作 Status InsIdxList(IdxListType &idxlist,int bno) { ... if(!b) InsertNewKey(idxlist,j,wd); // 在索引表 idxlist 的第 j 项上插入新关键词 , 并初始化书号索引的链表为空表 if(!InsertBook(idxlist,j,bno)) return OVERFLOW; // 在索引 表 idxListType 的第 i 项中插入书号为 bno 的索引 } //end for return OK; }//InsertIdexList
  • 43. 如何建立索引表 四个操作的具体实现 Void GetWord( int i, HString &wd) { // 用 wd 返回词表 wdlist 中第 i 个关键词 p = *(wdlist.item+i); // 取词表中第 i 个字符串 StrAssign(wd,p); // 生成关键字字符串 }
  • 44. 如何建立索引表 四个操作的具体实现 int Locate(idxListType &idxlist, HString wd,Boolean &b) // 在索引表 idxlist 中查询是否存在与 wd 相等的关键词 . 若存在 , 则 返回其在索引表中的位置 , 且 b 取 TRUE; 否则返回插入位置 , 且 b 取值 FALSE { for (i = idlist.last-1; (m=StrCompare(idxlist.item[i].key,wd))>0; --i); if (m==0) { b=TRUE; return i;} // 找到 else (b=FALSE; return i+1;} } //end locate
  • 45. 如何建立索引表 void InsertNewKey(int i, StrType wd) { // 在索引表 idxlist 的第 j 项上插入新关键词 , 并初始化书号索 引的链表为空表 for(j=idxlist.last-1;j>=i; --j) idxlist.item[j+1] = idlist.item[j]; // 后移索引项 // 插入新的索引项 StrCopy(idxlist.item[i].key,wd); // 串赋值 InitList(idxlist.item[i].bnolist); // 初始化书号索引表为空表 ++idxlist.last; } //InsertNewKey
  • 46. 如何建立索引表 Status InsertBook (IdxListType &idxlist, int I, int bno) { // 在索引表 idxListType 的第 i 项中插入书号为 bno 的索引 if (!MakeNode(p,bno) return ERROR; // 分配失败 Appand(idxlist.item[i].bnolist,p); // 插入新的书号索引 return OK; } //InsertBook
  • 47. 关键词 key bnolist 存放书号索引的链表 NULL 0 NULL [0] 0 Algorithms 1 34 NULL 10 Analysis 3 34 8 50 NULL ... P 67 NULL Appand(idxlist.item[i].bnolist,p); // 插入新的书号索引