資源簡介
經典的字符串處理問題 KWIC c++版,實現了管道風格,調用返回風格還有OOP風格

代碼片段和文件信息
#include?“CallReturnstyle.h“
#include?
#include?
#include?
using?namespace?std;
extern?FILE?*g_std;
extern?FILE?*g_foutput;
//KWIC鏈表頭
KWICItem *g_listhead?=?NULL;
/*
?* IsNoisyCh - ?判斷是否為噪音字符
?* @ch ?要判斷的字符
?*
?* 判斷@ch字符是否為噪音字符
?*/
bool?IsNoisyCh(char?ch)
{
if(ch?==?‘?‘?||?ch?==?‘\t‘?
||?ch?==?‘\n‘?||?ch?==?‘\0‘
||?ch?==?‘‘?||?ch?==?‘!‘?
||?ch?==?‘?‘?||?ch?==?‘;‘)
return?true;
else?
return?false;
}
/**
?*??????GetNextLine????-???獲取文件中的下一行字符串
?*??????@source: ???源文件指針
?* @line: ???把所獲取的字符串保存到line中
?*??????
?* 獲取@source文件中的下一行字符串到@line
?*
?*/
bool?GetNextLine(FILE?*sourcechar?line[])
{
//如果文件指針無效或字符串指針無效則返回
if(source?==?NULL?||?line?==?NULL)
{
return?false;
}
//如果文件指針有效則讀取一行字符到line中
int ch; //臨時字符
int n; //已保存到的字符數
n?=?0;
//循環讀取一個字符直到遇到換行符
while((ch?=?fgetc(source))?!=?EOF?&&?ch?!=?‘\n‘)
{
line[n++]?=?ch; //保存字符
}
line[n]?=?‘\0‘; //給字符串打結
if(ch?==?EOF)
return?false;
else?
return?true;
}
/*
?*
?* LoadFromFile - 從文件中讀取每一行字符串到鏈表中
?* @source 文件指針
?*
?*
?*
?*/
void?LoadFromFile(char?filename[])
{
FILE *source?=?fopen(filename“r“);
if(source?==?NULL)
return?;
int ch; //臨時字符
int n?=?0; //已保存到的字符數
KWICItem *temp?=?NULL;
KWICItem *g_listpos?=?NULL;
if(g_listhead?==?NULL)
{
g_listhead?=?temp?=?new?KWICItem;
memset(temp->lines0MAX_LINES*MAX_COUNT);
temp->linecount?=?1;
temp->next?=?NULL;
g_listpos?=?temp;
}
//循環讀取一個字符直到遇到換行符
while((ch?=?fgetc(source))?!=?EOF?)
{
if(ch?!=?‘\n‘)
{
temp->lines[0][n++]?=?ch; //保存字符
}
else
{
temp->lines[0][n]?=?‘\0‘; //給字符串打結
g_listpos->next?=?temp;
g_listpos?=?temp;
temp?=?new?KWICItem;
memset(temp->lines0MAX_LINES*MAX_COUNT);
temp->linecount?=?1;
temp->next?=?NULL;
n=0;
}
}
g_listpos->next?=?temp;
g_listpos?=?temp;
g_listpos->lines[0][n]?=?‘\0‘;
g_listpos->next?=?NULL;
if(temp)
{
// delete?temp;
// temp?=?NULL;
}
fclose(source);
}
void?PreProcess(char?line[])
{
int?len?=?strlen(line);
//除去后面的噪音字符
int?j?=?len?-?1;
while(j>0?&&?IsNoisyCh(j))?j--;
line[j]?=?‘\0‘;
//除去前面的噪音字符
int?startindex?=?0;
//查找從開始位置到第一個不是噪音字符的位置
while(IsNoisyCh(line[startindex]))?startindex++;
j?=?0;
while(line[j+startindex]!=‘\0‘)
{
line[j]?=?line[j+startindex];
j++;
}
line[j]?=?‘\0‘;
//除去中間多余的噪音字符
j?=?0;
char?*temp?=?(char*)malloc(len?+?1);
for(int?i=0;?line[i]!=‘\0‘;?i++)
{
if(!IsNoisyCh(temp[j-1])?&&?((IsNoisyCh(line[i])?&&?IsNoisyCh(line[i+1]))?
||?(IsNoisyCh(line[i])?&&?!IsNoisyCh(line[i+1]))))
{
temp[j++]?=?‘?‘;
}
else?if(!IsNoisyCh(line[i]))
{
temp[j++]?=?line[i];
}
}
temp[j]?=?‘\0‘;
strcpy(linetemp);
free(temp);
//下面的算法是不用其他的空間,不過這樣就要進行許多移位操作,時間代價比較高
/*int?i?=?0;
while(line[i]!=‘\0‘)
{
if(IsNoi
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????196462??2009-04-06?20:28??KWIC\EXE\CallReturnst
?????文件?????360448??2009-04-06?20:16??KWIC\EXE\KWIC.exe
?????文件?????200037??2009-04-06?20:31??KWIC\EXE\oopst
?????文件?????196462??2009-04-06?20:25??KWIC\EXE\pipest
?????文件??????16351??2009-04-01?23:07??KWIC\EXE\TDP.txt
?????文件???????1055??2009-04-06?20:53??KWIC\EXE\運行時間統計.txt
?????文件???????6299??2009-04-06?20:11??KWIC\SRC\KWIC\CallReturnst
?????文件???????1043??2009-04-06?20:00??KWIC\SRC\KWIC\CallReturnst
?????文件???????2903??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\BuildLog.htm
?????文件??????22587??2009-04-06?20:11??KWIC\SRC\KWIC\Debug\CallReturnst
?????文件?????196462??2009-04-06?20:28??KWIC\SRC\KWIC\Debug\CallReturnst
?????文件????2405372??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\KWIC.ilk
?????文件??????92074??2009-04-06?20:00??KWIC\SRC\KWIC\Debug\KWIC.obj
?????文件????3342336??2009-04-04?00:42??KWIC\SRC\KWIC\Debug\KWIC.pch
?????文件????2173952??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\KWIC.pdb
?????文件?????716393??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\OOPst
?????文件?????200037??2009-04-06?20:31??KWIC\SRC\KWIC\Debug\oopst
?????文件??????12204??2009-04-06?20:00??KWIC\SRC\KWIC\Debug\Pipest
?????文件?????196462??2009-04-06?20:25??KWIC\SRC\KWIC\Debug\pipest
?????文件??????15178??2009-04-04?00:42??KWIC\SRC\KWIC\Debug\stdafx.obj
?????文件??????16351??2009-04-01?23:07??KWIC\SRC\KWIC\Debug\TDP.txt
?????文件?????461824??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\vc70.idb
?????文件?????217088??2009-04-06?20:16??KWIC\SRC\KWIC\Debug\vc70.pdb
?????文件???????1055??2009-04-06?20:53??KWIC\SRC\KWIC\Debug\運行時間統計.txt
?????文件???????4451??2009-04-06?12:50??KWIC\SRC\KWIC\KWIC.cpp
?????文件????????133??2009-04-02?23:45??KWIC\SRC\KWIC\KWIC.txt
?????文件???????4172??2009-04-04?00:42??KWIC\SRC\KWIC\KWIC.vcproj
?????文件???????4776??2009-04-06?20:16??KWIC\SRC\KWIC\OOPst
?????文件????????664??2009-04-06?19:56??KWIC\SRC\KWIC\OOPst
?????文件?????200037??2009-04-06?20:16??KWIC\SRC\KWIC\oopst
............此處省略17個文件信息
評論
共有 條評論