C# 使用ListView构建DFA并进行字符串分析
private void button4_Click(object sender, EventArgs e) { // 创建DFA对象 DFA dfa = new DFA();
// 读取listView1中的数据并生成DFA
foreach (ListViewItem item in listView1.Items)
{
// 获取起始状态、接受符号和到达状态
int startState = int.Parse(item.SubItems[0].Text);
string symbol = item.SubItems[1].Text;
int endState = int.Parse(item.SubItems[2].Text);
// 添加转移函数
dfa.AddTransition(startState, symbol, endState);
// 添加终止状态
if (!dfa.AcceptStates.Contains(endState) && symbol == '#')
{
dfa.AcceptStates.Add(endState);
}
}
// 分析字符串
List<string> strings = new List<string> { 'aab', 'ab', 'abb', 'aabb' };
foreach (string s in strings)
{
bool accepted = dfa.Accepts(s);
// 添加分析结果到listView2中
ListViewItem resultItem = new ListViewItem(new string[] { s, accepted ? 'Accepted' : 'Rejected' });
listView2.Items.Add(resultItem);
}
}
原文地址: https://www.cveoy.top/t/topic/nsfy 著作权归作者所有。请勿转载和采集!