通宵实验结束了..........终于结束了



奋战一晚上,把这学期的C#实验全部搞定了。刚刚提交完实验,从实验室出来时间只有一种隔世为人的感觉………….

几个实验里最难的是一个文档分析的程序,可以进行文档的关键词搜索,并支持通过对Key Word添加逻辑符号进行搜索的方式。做完了这个突然有一种编搜索引擎的冲动 >_< 呵呵…………..

其实现在想起来挺笨的,最多的时间花在了对表达式的解析上,应该使用数据结构中的模式匹配来做比较简单,虽然用到了栈结构,但毕竟是现成的拿来就能用。最后我还是用了比较复杂的递归算法,现在是能满足实验要求了,但在复杂度上还有很多问题。贴在这里留个纪念吧…………….

class
OPTs


{

public
static
ArrayList ReadFile(String input)


{

//读文件并返回字典


ArrayList text = new
ArrayList();


String text_line;

int totalLength = 0;

StreamReader freader = File.OpenText(input);

Console.WriteLine("Reading {0} – please wait !", input);

while ((text_line = freader.ReadLine()) != null)

{

if (text_line.Length == 0)

continue;

text.Add(text_line);

totalLength += text_line.Length;

}

freader.Close();

Console.WriteLine("Reading {0} – OK, done !", input);

Console.WriteLine("\nThe text file {0}\n contains {1} lines in {2} bytes.\n", input, text.Count, totalLength);

Console.WriteLine("entering text into dictionary – please wait !");

return text;

}

public
static
void GetQuery(/String input/)


{

//导入字典


ArrayList dictionary = new
ArrayList();


String input = "F:/alice_emma.txt";

dictionary = ReadFile(input); //读文件


Console.WriteLine("entering text into dictionary – OK, done !");

//取得要求


String TheQuery;

do

{ //循环区的需求式


Console.Write("\nPlease enter a query or ‘q’ to Quit : ");

TheQuery = Console.ReadLine();

TheQuery.ToLower();

if (!TheQuery.Equals("q"))

analyse(TheQuery, dictionary);

}

while (!TheQuery.Equals("q"));

}

public
static
void analyse(String TheQuery, ArrayList dictionary)


{

//分析需求,调整顺序,显示查询结果


String[] queryelement = TheQuery.Split(‘ ‘);

Query result; //结果Query

String[] queryelement_turn = new
String[queryelement.Length];


for (int a = 0; a < queryelement.Length; a++) // 为后面的递归分析做准备,反向需求式


queryelement_turn[queryelement.Length - 1 - a] = queryelement[a];

for (int a = 0; a < queryelement.Length; a++)

{

if (queryelement_turn[a] == "!")

{

queryelement_turn[a] = queryelement_turn[a - 1];

queryelement_turn[a - 1] = "!";

}

if (queryelement_turn[a] == "(")

queryelement_turn[a] = ")";

else
if (queryelement_turn[a] == ")")


queryelement_turn[a] = "(";

}

result = done(queryelement_turn, dictionary, 0);

result.eval(); //Query计算结果


result.display(); //Query显示


Console.WriteLine("");

foreach(Object i in result.Solution) //显示句子


Console.WriteLine("[" + ( (int)i + 1 ) + "]: " + dictionary[ (int)i ] );

}

public
static
Query done(String[] queryelement, ArrayList dictionary, int now) //
递归分析需求式


{

Query result = null;

int i = now;

switch (queryelement[i])

{ //依据不同的判断结果构造NEW语句


case
"!":


if (i + 1 == queryelement.Length - 1 || queryelement[i + 2] == ")")

result = new
NotQuery(new
NameQuery(queryelement[i + 1], dictionary), dictionary);


else
if (queryelement[i + 1] == "(")


{

if (searchbracketmatch(queryelement, i) == (queryelement.Length - 1))

result = new
NotQuery(done(queryelement, dictionary, i + 1), dictionary);


else
if (queryelement[searchbracketmatch(queryelement, i) + 1] == "||")


result = new
OrQuery(new
NotQuery(done(queryelement, dictionary, i + 1), dictionary)


, done(queryelement, dictionary, searchbracketmatch(queryelement, i) + 2), dictionary);

else
if (queryelement[searchbracketmatch(queryelement, i) + 1] == "&&")


result = new
AndQuery(new
NotQuery(done(queryelement, dictionary, i + 1), dictionary)


, done(queryelement, dictionary, searchbracketmatch(queryelement, i) + 2), dictionary);

}

else
if (queryelement[i + 2] == "||")


result = new
OrQuery(new
NotQuery(new
NameQuery(queryelement[i + 1],dictionary), dictionary)


, done(queryelement, dictionary, i + 3), dictionary);

else
if (queryelement[i + 2] == "&&")


result = new
AndQuery(new
NotQuery(new
NameQuery(queryelement[i + 1], dictionary), dictionary)


, done(queryelement, dictionary, i + 3), dictionary);

break;

case
"(":


if (searchbracketmatch(queryelement, i) == (queryelement.Length - 1))

result = done(queryelement, dictionary, i + 1);

else
if (queryelement[searchbracketmatch(queryelement, i) + 1] == "||")


result = new
OrQuery(done(queryelement, dictionary, i + 1)


, done(queryelement, dictionary, searchbracketmatch(queryelement, i) + 2), dictionary);

else
if (queryelement[searchbracketmatch(queryelement, i) + 1] == "&&")


result = new
AndQuery(done(queryelement, dictionary, i + 1)


, done(queryelement, dictionary, searchbracketmatch(queryelement, i) + 2), dictionary);

break;

default:

if (i == (queryelement.Length - 1) || queryelement[i + 1] == ")")

result = new
NameQuery(queryelement[i], dictionary);


else
if (queryelement[i + 1] == "||")


result = new
OrQuery(new
NameQuery(queryelement[i], dictionary)


, done(queryelement, dictionary, i + 2), dictionary);

else
if (queryelement[i + 1] == "&&")


result = new
AndQuery(new
NameQuery(queryelement[i], dictionary)


, done(queryelement, dictionary, i + 2), dictionary);

break;

}

return result; //返回生成的Query

}

public
static
int searchbracketmatch(String[] queryelement, int now)


{ //查询括号匹配函数


int leftbracket = 1;

int rightbracket = 0;

int i = 0;

for (i = now + 1; i < queryelement.Length; i++)

{

if (queryelement[i] == "(")

leftbracket++;

else
if (queryelement[i] == ")")


rightbracket++;

if (leftbracket == rightbracket)

break;

}

return i; //返回对应括号在数组中的位置


}

}

现在看递归的部分简直像是rubbish啊,臃肿又难以理解。不过这个程序让我第一次体会到了面向对象编程的巨大优势,那就是多态。在程序中我不需要知道那些Objects具体是什么类型,只要用它们的基类来指向它们,并应用基类中定义的methods,不用管那么多,反正编译器回自己判断我调用的是什么类型,它是否重载了我所调用的method………….