69,371
社区成员
发帖
与我相关
我的任务
分享
#ifndef _WORD_H
#define _WORD_H
#include <stdio.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
typedef enum tagRET_CODE_E
{
SUCCESS,
FAILURE,
PARA_ERR,
MEM_ERR,
}RET_CODE_E;
#define NHASH 29989
#define MULT 31
#define TRUE 1
#define FALSE 0
typedef void VOID;
typedef char CHAR;
typedef unsigned long ULONG;
typedef struct tagWORD_NODE_S
{
CHAR *pcWord;
ULONG ulCnt;
struct tagWORD_NODE_S* pstNext;
}WORD_NODE_S;
#define WORD_ARE_EQUAL(pstr1, pstr2) (!strcmp(pstr1, pstr2) ? TRUE:FALSE)
#define WORD_SHOWINFO(str) printf("%s", str)
VOID Word_ShowStatistics();
ULONG Word_GenHashIndex(CHAR* pcWordString, ULONG* pulHashIdx);
VOID Word_UpdateHashTable(CHAR* pcWord);
WORD_NODE_S* Word_SearchIdenticalNode(WORD_NODE_S* pstStartNode, CHAR* pcWord);
VOID Word_DeInit();
#ifdef __cplusplus
}
#endif
#endif
/*TextQuery.h*/
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
using namespace std;
class TextQuery
{
public:
typedef vector< string >::size_type line_no;
typedef map< string,set<line_no> >::iterator map_word_iterator;
void read_file(ifstream &is)
{
store_file(is);
build_map();
}
set< line_no > run_query(const string &) const;
string text_line(line_no) const;
private:
void store_file(ifstream &);
void build_map();
vector< string > lines_of_text;
map< string,set< line_no > > word_map;
};
/*TestQuery.cpp*/
#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include "textQuery.h"
using namespace std;
void TextQuery::store_file(ifstream &is)
{
string textline;
while (getline(is, textline))
lines_of_text.push_back(textline);
}
void TextQuery::build_map()
{
for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num)
{
istringstream line(lines_of_text[line_num]);
string word;
while (line >>word)
word_map[word].insert(line_num);
}
}
set< TextQuery::line_no > TextQuery::run_query(const string &query_word) const
{
map< string, set< line_no > >::const_iterator loc = word_map.find(query_word);
if (loc == word_map.end())
return set<line_no>();
else
return loc->second;
}
string TextQuery::text_line(line_no line) const
{
if (line <lines_of_text.size())
return lines_of_text[line];
throw out_of_range("line number out of ranger");
}
/*main.cpp*/
#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include "textQuery.h"
using namespace std;
typedef set<TextQuery::line_no> line_nums;
void print_results(const set<TextQuery::line_no> &locs, const string &sought, const TextQuery &file);
ifstream& open_file(ifstream &in, const string &file);
int main()
{
ifstream infile;
if ( !open_file(infile, "text_in.txt") )
{
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
TextQuery tq;
tq.read_file(infile);
while (true)
{
cout << "enter word to look for,or q for qiut: ";
string s;
cin >> s;
if(!cin || s == "q")
break;
set<TextQuery::line_no> locs =tq.run_query(s);
print_results(locs, s, tq);
}
return 0;
}
void print_results(const set<TextQuery::line_no> &locs, const string &sought, const TextQuery &file)
{
line_nums::size_type size = locs.size();
cout << "\n" <<sought << " occurs " << size << " " <</*make_plural(size, "time" , "s") <<*/endl;
line_nums::const_iterator it = locs.begin();
for( ; it != locs.end(); ++it)
{
cout <<"\t(line " << (*it) +1 << ")" <<file.text_line(*it) <<endl;
}
}
ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
#include <stdio.h>
#include "word.h"
#include<string.h>
int main()
{
CHAR acWord[20] = {0};
FILE* fin=fopen("f:\\test.txt","r");
if (fin==NULL)
{
printf("Open files error\n");
return 0;
}
while (!feof(fin))
{
fscanf(fin,"%s",acWord);
if (strlen(acWord)==1 && acWord[0]==' ')
{
continue;
}
else
{
Word_UpdateHashTable((CHAR*)acWord);
}
}
fclose(fin);
Word_ShowStatistics();
Word_DeInit();
}
/* word.c */
#include "word.h"
WORD_NODE_S gstWordNodePtrArray[NHASH] = {0};
ULONG Word_Gen_HashIndex(CHAR* pcWordString, ULONG* pulHashIdx)
{
CHAR *pcTmp = NULL;
ULONG ulHashSum = 0;
if ((NULL == pcWordString) || (NULL == pulHashIdx))
{
return PARA_ERR;
}
pcTmp = pcWordString;
while ('\0' != *pcTmp)
{
ulHashSum = ulHashSum * MULT + *pcTmp;
pcTmp++;
}
*pulHashIdx = ulHashSum % NHASH;
return SUCCESS;
}
VOID Word_UpdateHashTable(CHAR* pcWord)
{
ULONG ulHashIdx = 0;
ULONG ulRetVal = 0;
WORD_NODE_S *pstWordNode = NULL;
if (NULL == pcWord)
{
return;
}
ulRetVal = Word_Gen_HashIndex(pcWord, &ulHashIdx);
if (SUCCESS != ulRetVal)
{
return;
}
pstWordNode = Word_SearchIdenticalNode(gstWordNodePtrArray[ulHashIdx].pstNext,
pcWord);
if (NULL != pstWordNode)
{
pstWordNode->ulCnt++;
return;
}
pstWordNode = (WORD_NODE_S*)malloc(sizeof(WORD_NODE_S));
if (NULL == pstWordNode)
{
WORD_SHOWINFO("Error: memory shortage.");
return;
}
pstWordNode->pcWord = (CHAR*)malloc(strlen(pcWord) + 1);
if(NULL == pstWordNode->pcWord)
{
WORD_SHOWINFO("Error: memory shortage.");
free(pstWordNode);
return;
}
strcpy(pstWordNode->pcWord, pcWord);
pstWordNode->ulCnt = 1;
pstWordNode->pstNext = gstWordNodePtrArray[ulHashIdx].pstNext;
gstWordNodePtrArray[ulHashIdx].pstNext = pstWordNode;
return;
}
WORD_NODE_S* Word_SearchIdenticalNode(WORD_NODE_S* pstStartNode,
CHAR* pcWord)
{
WORD_NODE_S* pstTmpNode = NULL;
if ((NULL == pstStartNode) || (NULL == pcWord))
{
return NULL;
}
pstTmpNode = pstStartNode;
while (NULL != pstTmpNode)
{
if (TRUE == WORD_ARE_EQUAL((CHAR*)pstTmpNode->pcWord, pcWord))
{
break;
}
pstTmpNode = pstTmpNode->pstNext;
}
return pstTmpNode;
}
VOID Word_ShowStatistics()
{
ULONG ulLoopCnt = 0;
WORD_NODE_S *pstTmpNode = NULL;
for (ulLoopCnt = 0; ulLoopCnt < NHASH; ulLoopCnt++)
{
if (NULL == gstWordNodePtrArray[ulLoopCnt].pstNext)
{
continue;
}
pstTmpNode = gstWordNodePtrArray[ulLoopCnt].pstNext;
while(NULL != pstTmpNode)
{
printf("%20s: %10d\n", pstTmpNode->pcWord, pstTmpNode->ulCnt);
pstTmpNode = pstTmpNode->pstNext;
}
}
}
VOID Word_DeInit()
{
ULONG ulLoopCnt = 0;
WORD_NODE_S *pstCurNode = NULL;
WORD_NODE_S *pstNextNode = NULL;
for (ulLoopCnt = 0; ulLoopCnt < NHASH; ulLoopCnt++)
{
pstCurNode = gstWordNodePtrArray[ulLoopCnt].pstNext;
while (NULL != pstCurNode)
{
pstNextNode = pstCurNode->pstNext;
free(pstCurNode);
pstCurNode = pstNextNode;
}
}
return;
}