题目是写个词法分析器,开始理解错题目了,后来知道题目的意思觉得不是很难
目的是实现一个文本文件的内容分析,当然这个文本全是按一定的格式输出的
下面写上主要算法:
rid = 0;//记录ID
rtxtNew.Text = "|";
string tmptxt;
for (int i=0;i<rtxtOld.Lines.Length;i++)
{
tmptxt=rtxtOld.Lines[i];
if (tmptxt.Trim() != "" || tmptxt.Trim() != "Change History:")//去掉不需要部分
{
if (tmptxt.IndexOf('/') == 2)//日期和作者
{
rtxtNew.Text += "/n/n|Change ID:/t/t" + rid + "/n";
rid++;
int t;
string wttmp;
t = tmptxt.IndexOf(',');
wttmp = tmptxt.Substring(0, t);
rtxtNew.Text += "|Changed Date:/t/t" + wttmp + "/n";
wttmp = tmptxt.Substring(t + 2);
rtxtNew.Text += "|Changed By:/t/t" + wttmp + "/n";
continue;
}
else if (tmptxt.IndexOf('*') != -1)//介绍
{
int t;
string wttmp;
t = tmptxt.IndexOf('*');
wttmp = tmptxt.Substring(t + 3);
rtxtNew.Text += "|Summary:/t/t" + wttmp.Trim();
continue;
}
else if (tmptxt.IndexOf(':') != -1)//字段
{
rtxtNew.Text += "/n|" + tmptxt.Substring(0, tmptxt.IndexOf(':') + 1).Trim() + "/t/t" + tmptxt.Substring(tmptxt.IndexOf(':') + 1).Trim();
}
else if (tmptxt.IndexOf(':') == -1 && tmptxt.IndexOf('/') == -1 && tmptxt.IndexOf('*') == -1)
{
rtxtNew.Text += tmptxt.Trim();
continue;
}
}
else
continue;
}