通过VS使用C#语言实现在函数private void button3_Clickobject sender EventArgs e实现读入NFA文件该函数可以把选中的NFA文件的数据显示到容器listView1中该容器被分为3列初始状态、接受符号、到达状态。的基础上实现函数private void button7_Clickobject sender EventArgs e的NFA转DFA功能不
以下是实现NFA转DFA的代码:
private void button7_Click(object sender, EventArgs e)
{
//获取NFA数据
string[] lines = File.ReadAllLines(openFileDialog1.FileName);
int startState = int.Parse(lines[0].Split(':')[1]);
string[] endStates = lines[1].Split(':')[1].Split(';');
string[] symbols = lines[2].Split(':')[1].Split(';');
Dictionary<int, Dictionary<string, List
//转换NFA为DFA
Dictionary<string, List<int>> dfaStartState = new Dictionary<string, List<int>>();
dfaStartState["" + startState] = EpsilonClosure(new List<int>() { startState }, nfa);
Dictionary<string, List<int>> dfa = new Dictionary<string, List<int>>();
Queue<string> queue = new Queue<string>();
queue.Enqueue("" + startState);
while (queue.Count > 0)
{
string currentState = queue.Dequeue();
foreach (string symbol in symbols)
{
List<int> toStates = EpsilonClosure(Move(dfaStartState[currentState], symbol, nfa), nfa);
if (toStates.Count > 0)
{
string toState = String.Join(",", toStates);
if (!dfa.ContainsKey(currentState))
{
dfa[currentState] = new List<int>();
}
dfa[currentState].Add(toState);
if (!dfaStartState.ContainsKey(toState))
{
dfaStartState[toState] = toStates;
queue.Enqueue(toState);
}
}
}
}
//显示转换后的结果
listView2.Clear();
listView2.Columns.Add("初始状态");
listView2.Columns.Add("接受符号");
listView2.Columns.Add("到达状态");
foreach (string currentState in dfa.Keys)
{
foreach (string symbol in symbols)
{
if (symbol != "#")
{
string toState = "";
if (dfa.ContainsKey(currentState) && dfa[currentState].Count > 0)
{
toState = String.Join(",", dfa[currentState]);
}
listView2.Items.Add(new ListViewItem(new string[] { currentState, symbol, toState }));
}
}
}
foreach (string endState in endStates)
{
foreach (string currentState in dfa.Keys)
{
if (dfaStartState.ContainsKey(currentState) && dfaStartState[currentState].Contains(int.Parse(endState)))
{
listView2.Items.Add(new ListViewItem(new string[] { currentState, "", "" }));
}
}
}
}
private List
private List
原文地址: https://www.cveoy.top/t/topic/cizU 著作权归作者所有。请勿转载和采集!