private void button4_Clickobject sender EventArgs e函数实现读取容器listView1中起始状态、、接受符号、到达状态三列数据生成DFA并把分析结果显示在listView2容器中该容器以经被分为起始状态、接受符号、到达状态三列。listView1中数据大致如下:1 a 23 b 45 # 35 # 14 # 62 # 67 # 56 # 8
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/cg1d 著作权归作者所有。请勿转载和采集!