#pragma once
#include <map>
#include <string>
#include <Windows.h>
using namespace std;
typedef map<string, string> MAP_WORD;
class CPraseWord
{
public:
CPraseWord(void);
~CPraseWord(void);
public:
BOOL ParseWord(const char* lpszFile);
BOOL GetWordInfo(const char* lpszWord, string& strInfo);
private:
MAP_WORD* GetMap(const char ch);
BOOL AddToMap(const char* lpszStr, const int nLine, const int nWord);
private:
MAP_WORD m_mapWord[27];
};
#include "stdafx.h"
#include "PraseWord.h"
#include "macro.h"
#define MAX_WORD_LEN 100
CPraseWord::CPraseWord(void)
{
}
CPraseWord::~CPraseWord(void)
{
}
BOOL CPraseWord::ParseWord(const char* lpszFile)
{
ASSERT_RETURN(lpszFile, FALSE);
FILE* fp = fopen(lpszFile, "r");
int nWord = 1;
int nLine = 1;
char ch = '\0';
BOOL bLastSp = FALSE;
int nCharPos = 0;
char szWord[MAX_WORD_LEN] = {0};
while ( EOF != (ch = fgetc(fp)) )
{
switch (ch)
{
case '
':
nLine++;
nWord = 1;
bLastSp = FALSE;
nCharPos = 0;
AddToMap(szWord, nLine, nWord);
break;
case ' ':
case '\r':
case ',':
case ':':
case '#':
case '!':
case '+':
case '-':
case '*':
case '&':
case '(':
case ')':
case '@':
case '$':
bLastSp = TRUE;
nCharPos = 0;
AddToMap(szWord, nLine, nWord);
break;
default:
if (bLastSp)
{
nWord++;
nCharPos = 0;
memset(szWord, 0, sizeof(szWord));
}
szWord[nCharPos++] = ch;
bLastSp = FALSE;
break;
}
}
if (NULL != fp)
{
fclose(fp);
fp = NULL;
}
return TRUE;
}
BOOL CPraseWord::AddToMap(const char* lpszStr, const int nLine, const int nWord)
{
ASSERT_RETURN(lpszStr, FALSE);
MAP_WORD* pMap = GetMap(lpszStr[0]);
if (NULL == pMap)
{
return FALSE;
}
char szPlace[100] = {0};
::_snprintf(szPlace, sizeof(szPlace), "{%d, %d}", nLine, nWord);
MAP_WORD::iterator it = pMap->find(lpszStr);
if (pMap->end() == it)
{
(*pMap)[lpszStr] = szPlace;
}
else
{
it->second += ",";
it->second += szPlace;
}
return TRUE;
}
BOOL CPraseWord::GetWordInfo(const char* lpszWord, string& strInfo)
{
ASSERT_RETURN(lpszWord, FALSE);
MAP_WORD* pMap = GetMap(lpszWord[0]);
if (NULL == pMap)
{
return FALSE;
}
MAP_WORD::iterator it = pMap->find(lpszWord);
if (pMap->end() == it)
{
return FALSE;
}
strInfo = it->second;
return TRUE;
}
MAP_WORD* CPraseWord::GetMap(const char ch)
{
int nSub = ch - 'a';
if ( (nSub >= 0) && (nSub < 26) )
{
return &m_mapWord[nSub];
}
nSub = ch - 'A';
if ( (nSub >= 0) && (nSub < 26) )
{
return &m_mapWord[nSub];
}
if (0 == ch)
{
return NULL;
}
return &m_mapWord[26];
}
// w_m.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "PraseWord.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <time.h>
using namespace std;
size_t time_stat(bool bBegin = true);
int main(int argc, char* argv[])
{
CPraseWord pd;
char* lpszFile = "./kjv.txt";
if (argc >= 2)
{
lpszFile = argv[1];
}
//DWORD dwStart = GetTickCount();
time_stat(true);
if (!pd.ParseWord(lpszFile))
{
cout << "prase file failed..." << endl;
return -1;
}
cout << "parase file cost time:" << time_stat(false) << endl;
//DWORD dwEnd = GetTickCount();
//cout << "parase file cost time:" << dwEnd - dwStart << endl;
while (TRUE)
{
char szWord[100] = {0};
cout << "please input find parse word:";
cin >> szWord;
string str;
//dwStart = GetTickCount();
time_stat(true);
pd.GetWordInfo(szWord, str);
//cout << "search cost time:" << time_stat(false) << endl;
size_t nCostTime = time_stat(false);
//dwEnd = GetTickCount();
//cout << "search cost time:" << dwEnd - dwStart << endl;
if (str.empty())
{
cout << "can't find " << szWord << endl;
}
else
{
cout << str << endl;
}
cout << endl << endl;
cout << "search cost time:" << nCostTime << endl;
cout << "search total cost time:" << time_stat(false) << endl;
//dwEnd = GetTickCount();
//cout << "search total cost time:" << dwEnd - dwStart << endl;
}
return 0;
}
int gettimeofday(struct timeval *tv, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm.tm_isdst= -1;
clock = mktime(&tm);
tv->tv_sec = (long)clock;
tv->tv_usec = wtm.wMilliseconds * 1000;
return 0;
}
size_t time_stat(bool bBegin)
{
static struct timeval st_time_begin, st_time_end;
static char szInfo[128];
if (bBegin)
{
gettimeofday(&st_time_begin, NULL);
return 0;
}
else
{
gettimeofday(&st_time_end, NULL);
return (st_time_end.tv_sec - st_time_begin.tv_sec) * 1000
+ (st_time_end.tv_usec - st_time_begin.tv_usec) / 1000;
}
}