indexHeader.h
#define MaxBookNum 1000
#define MaxKeyNum 2500
#define MaxLineLen 500
#define MaxWordNum 20
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
typedef struct{
string item[MaxWordNum];
int length;
}WordListType;
typedef struct{
string key;
LinkList bnolist;
}IdxTermType;
typedef struct{
IdxTermType item[MaxKeyNum+1];
int length;
}IdxListType;
void InitIdxList(IdxListType& idxlist);
void ExtractKeyWord(string wd);
void InsIdxList(IdxListType& idxlist, ElemType bno);
void OutputText(IdxListType idxlist);
//void GetWord(WordListType wdlist, int i);
int Locate(IdxListType idxlist, string wd, bool& b);
void InsertNewKey(IdxListType& idxlist, string wd, int i);
void InsertBook(IdxListType& idxlist, ElemType bno, int i);
*******************************************************************************************
index.cpp
/*
*此程序是建立词索引表,使用关键字对数目进行查找
*/
#pragma warning(disable:4786)
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
#include"indexHeader.h"
using namespace std;
string str[] = {"an","a","of","the","at","it","to","and","in","is","but","no","or","by"};
vector<string> generalWord(str, str+14);
WordListType wdlist;
void InitIdxList(IdxListType& idxlist)
{
LinkList LList;
LList = (LinkList)malloc(sizeof(LNode));
LList->data = 0;
LList->next = NULL;
idxlist.item[0].bnolist = LList;
idxlist.item[0].key = "";
idxlist.length = 1;
}
void ExtractKeyWord(string wd)
{
int i=0;
while(i <generalWord.size() && strcmp(wd.c_str(), str[i].c_str()) != 0)
{
i++;
}
if(i == generalWord.size())
{
wdlist.item[wdlist.length] = wd;
wdlist.length++;
}
}
int Locate(IdxListType idxlist, string wd, bool& b)
{
for(int i=0, m; i < idxlist.length && (m=strcmp(idxlist.item[i].key.c_str(), wd.c_str())) < 0; i++);
if(m == 0)
{
b = true;
return i;
}
else
{
b = false;
return i;//(i == idxlist.length)? i: (i+1);
}
}
void InsertNewKey(IdxListType& idxlist, string wd, int j)
{
for(int i=idxlist.length-1; i >= j; i--)
{
idxlist.item[i+1] = idxlist.item[i];
}
idxlist.item[j].key = wd;
LinkList LList;
LList = (LinkList)malloc(sizeof(LNode));
LList->data = 0;
LList->next = NULL;
idxlist.item[j].bnolist= LList;
idxlist.length++;
}
void InsertBook(IdxListType& idxlist, ElemType bno, int j)
{
LinkList LList;
LList = (LinkList)malloc(sizeof(LNode));
LList->data = bno;
LList->next = NULL;
LinkList p;
p = idxlist.item[j].bnolist;
while(p->next)
{
p = p->next;
}
p->next = LList;
}
void InsIdxList(IdxListType& idxlist, ElemType bno)
{
bool b;
for(int i=0, j; i < wdlist.length ; i++)
{
j = Locate(idxlist, wdlist.item[i], b);
if(!b)
{
InsertNewKey(idxlist, wdlist.item[i], j);
}
InsertBook(idxlist, bno, j);
}
}
void OutputText(IdxListType idxlist, ofstream& outfile)
{
for(int i=1; i < idxlist.length; i++)
{
outfile<<idxlist.item[i].key<<" ";
int n=0;
LinkList p;
p = idxlist.item[i].bnolist->next;
while(p)
{
if(n) outfile<<",";
outfile<<p->data;
p = p->next ;
n++;
}
outfile<<endl;
}
}
int main(int argc, char *argv[])
{
IdxListType idxlist;
InitIdxList(idxlist);
ifstream infile("bookinfo.txt");
ofstream outfile("bookindex.txt");
for(string line; getline(infile, line); )
{
istringstream sin(line);
ElemType bno;
sin>>bno;
wdlist.length = 0;
for(string wd; sin>>wd; )
{
ExtractKeyWord(wd);
}
InsIdxList(idxlist, bno);
//OutputText(idxlist, outfile);
//outfile<<endl;
}
OutputText(idxlist, outfile);
return 0;
}
编程过程中遇到的问题:
1、warning c4787的问题
原因: 标识符过长,超过了最大限定255个字,类名超过了255个字,使用时就会报4786的waring。
在使用STL(C++标准模板库)的时候经常引发类似的错误,尤其是vector,map这类模板类,
模板中套模板,一不小心就超长了。
解决方法:
一种是直接定义别名:
#define VeryLongClassNameA A
#define VeryLongClassNameB B
#endif
另一种是屏蔽4786warning:
#pragma warning(disable : 4786)
注意屏蔽语句必须放在报错的模板类的引用声明(如#include <vector>)之前,否则还是不起作用。
2、vector的批量初始化
例如:string str[] = {"an","a","of","the","at","it","to","and","in","is","but","no","or","by"};
vector<string> generalWord(str, str+14);
但是问题string 数组的长度怎么求呢?
3、typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
LNode 是节点,LinkList 是节点指针,可以用LNode.data, LNode.next 但是对于LinkList则用,LinkList->data, LinkList->next
LinkList LList;
LList = (LinkList)malloc(sizeof(LNode));
LList->data = 0;
LList->next = NULL;
LinkList的初始化必须要分配存储空间,然后才能初始化,如:idxlist.item[j].bnolist= LList;
4、int strcmp(const char *a, const char *b);
函数的参数是常量字符指针,如果实参是string对象str,则要进行转换,如:str.c_str(),
此函数的返回值是int, a>b则返回大于0的数,等于则返回0,小于则返回小于0 的数