private void button7_Click(object sender, EventArgs e) { // 获取NFA数据 lines = listView1.Items.Cast() .Select(item => item.SubItems.Cast<ListViewItem.ListViewSubItem>() .Select(subItem => subItem.Text)) .Select(subItems => string.Join("\t", subItems)) .ToArray(); startSymbol = lines[0].Split(':')[1].Trim(); endSymbol = lines[1].Split(':')[1].Trim(); symbolSet = Enumerable.Range('a', 'z' - 'a' + 1) .Select(c => ((char)c).ToString()) .ToArray(); endStates = lines.Skip(2) .GroupBy(line => line.Split('\t')[0]) .Select(group => group.Key) .ToArray();

// 生成DFA数据
var dfaStates = new List<HashSet<string>> { new HashSet<string> { startSymbol } };
var unmarkedStates = new Queue<HashSet<string>>();
unmarkedStates.Enqueue(dfaStates[0]);
while (unmarkedStates.Count > 0)
{
    var currentState = unmarkedStates.Dequeue();
    var transitions = new Dictionary<string, HashSet<string>>();
    foreach (var symbol in symbolSet)
    {
        var nextStates = new HashSet<string>();
        foreach (var state in currentState)
        {
            var matchingLines = lines.Where(line =>
                line.Split('\t')[0] == state && line.Split('\t')[1] == symbol);
            foreach (var matchingLine in matchingLines)
            {
                var nextState = matchingLine.Split('\t')[2];
                nextStates.Add(nextState);
            }
        }
        if (nextStates.Count > 0)
        {
            transitions.Add(symbol, nextStates);
            if (!dfaStates.Any(set => set.SetEquals(nextStates)))
            {
                dfaStates.Add(nextStates);
                unmarkedStates.Enqueue(nextStates);
            }
        }
    }
    var dfaStateIndex = dfaStates.IndexOf(currentState);
    var isEndState = currentState.Any(state => endStates.Contains(state));
    var dfaLine = $"{dfaStateIndex}\t{string.Join(",", transitions.Select(pair =>
        $"{pair.Key}:{dfaStates.IndexOf(pair.Value)}"))}\t{(isEndState ? "1" : "0")}";
    var listViewItem = new ListViewItem(dfaLine.Split('\t'));
    listView2.Items.Add(listViewItem);
}
private void button7_Clickobject sender EventArgs e函数在VS软件中利用c#语言实现根据NFA数据生成DFA文件数据功能NFA数据显示listView1容器中生成DFA数据显示listView2容器中两个容器已经被分为起始状态、接受符号、到达状态三列。NFA数据格式类似如下开始符7终结符261	a	23	b	45	#	35	#	14	#	6DFA数

原文地址: https://www.cveoy.top/t/topic/chDp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录