C# 实现 DFA 文件保存与读取功能
// 保存 DFA 文件 private void button8_Click(object sender, EventArgs e) { // 创建一个保存文件对话框 SaveFileDialog sfd = new SaveFileDialog(); // 设置默认文件名 sfd.FileName = 'DFA.txt'; // 设置文件类型 sfd.Filter = '文本文件(.txt)|.txt'; // 设置默认路径 sfd.InitialDirectory = Application.StartupPath; // 打开保存文件对话框,获取用户选择的文件名 if (sfd.ShowDialog() == DialogResult.OK) { // 创建文件流 FileStream fs = new FileStream(sfd.FileName, FileMode.Create); // 创建写入器 StreamWriter sw = new StreamWriter(fs); // 写入开始符 sw.WriteLine('开始符:' + startState + ';'); // 写入终结符 sw.Write('终结符:'); foreach (int endState in endStates) { sw.Write(endState.ToString() + ';'); } sw.WriteLine(); // 写入最大状态数 sw.WriteLine('最大状态数:' + maxState.ToString()); // 写入符号集 sw.Write('符号集:'); foreach (char symbol in symbols) { sw.Write(symbol.ToString() + ';'); } sw.WriteLine(); // 写入转换表 for (int i = 0; i <= maxState; i++) { for (int j = 0; j < symbols.Length; j++) { int nextState = dfaTable[i, j]; if (nextState != -1) { sw.WriteLine(i.ToString() + '\t' + symbols[j].ToString() + '\t' + nextState.ToString()); } } } // 关闭写入器和文件流 sw.Close(); fs.Close(); MessageBox.Show('DFA 文件保存成功!'); } }
// 读取 NFA 文件 private void button6_Click(object sender, EventArgs e) { // 创建一个打开文件对话框 OpenFileDialog ofd = new OpenFileDialog(); // 设置文件类型 ofd.Filter = '文本文件(.txt)|.txt'; // 设置默认路径 ofd.InitialDirectory = Application.StartupPath; // 打开打开文件对话框,获取用户选择的文件名 if (ofd.ShowDialog() == DialogResult.OK) { // 创建文件流 FileStream fs = new FileStream(ofd.FileName, FileMode.Open); // 创建读取器 StreamReader sr = new StreamReader(fs); // 读取开始符 string line = sr.ReadLine(); startState = int.Parse(line.Substring(line.IndexOf(':') + 1, line.IndexOf(';') - line.IndexOf(':') - 1)); // 读取终结符 line = sr.ReadLine(); string[] endStatesStr = line.Substring(line.IndexOf(':') + 1, line.IndexOf(';') - line.IndexOf(':') - 1).Split(';'); foreach (string endStateStr in endStatesStr) { endStates.Add(int.Parse(endStateStr)); } // 读取最大状态数 line = sr.ReadLine(); maxState = int.Parse(line.Substring(line.IndexOf(':') + 1, line.IndexOf(';') - line.IndexOf(':') - 1)); // 读取符号集 line = sr.ReadLine(); string[] symbolsStr = line.Substring(line.IndexOf(':') + 1, line.IndexOf(';') - line.IndexOf(':') - 1).Split(';'); symbols = new char[symbolsStr.Length]; for (int i = 0; i < symbolsStr.Length; i++) { symbols[i] = char.Parse(symbolsStr[i]); } // 初始化转换表 dfaTable = new int[maxState + 1, symbols.Length]; for (int i = 0; i <= maxState; i++) { for (int j = 0; j < symbols.Length; j++) { dfaTable[i, j] = -1; } } // 读取转换表 while ((line = sr.ReadLine()) != null) { string[] parts = line.Split('\t'); int currentState = int.Parse(parts[0]); char symbol = char.Parse(parts[1]); int nextState = int.Parse(parts[2]); int symbolIndex = Array.IndexOf(symbols, symbol); dfaTable[currentState, symbolIndex] = nextState; } // 关闭读取器和文件流 sr.Close(); fs.Close(); // 显示分析结果 listView2.Items.Clear(); for (int i = 0; i <= maxState; i++) { for (int j = 0; j < symbols.Length; j++) { int nextState = dfaTable[i, j]; if (nextState != -1) { ListViewItem item = new ListViewItem(i.ToString()); item.SubItems.Add(symbols[j].ToString()); item.SubItems.Add(nextState.ToString()); listView2.Items.Add(item); } } } MessageBox.Show('NFA 文件读取成功!'); } }
原文地址: https://www.cveoy.top/t/topic/nsfJ 著作权归作者所有。请勿转载和采集!