C# 正则表达式 NFA 转 DFA 工具
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection.Emit; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace byyljxfzxt { public partial class Form3 : Form { public Form3() { InitializeComponent(); }
public bool rep(char x, string s)//判断是否重复
{
int i = 0;
if (s.Length == 0)
{
return true;
}
while (i < s.Length)
{
if (s[i] == x)
return false;
i++;
}
return true;
}
string s3 = "";
public int countap(string s)//统计字母个数
{
int i = 0;
while (i < s.Length)
{
if (s[i] <= 'z' && s[i] >= 'a')
{
if (rep(s[i], s3))
s3 = s3 + s[i];
}
i++;
}
return s3.Length;
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem item = new ListViewItem();
item.Text = Convert.ToString(listView1.Items.Count + 1);
item.SubItems.Add("0");
item.SubItems.Add("1");
item.SubItems.Add("2");
listView1.Items.Add(item);
}
private void button1_Click(object sender, EventArgs e)
{
string s = richTextBox1.Text;
int i, count = 0;//count用来标记(和)
countap(s);
if (s == "")//如果输入为空白
{
MessageBox.Show("正确的输入");
return;
}
if (s[0] == '|' || s[0] == '*' || s[s.Length - 1] == '|' || s[s.Length - 1] == '(')//不能以|、*开头,也不能以|、(结尾
{
MessageBox.Show("错误的输入");
return;
}
for (i = 0; i < s.Length; i++)//做循环判断
{
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || s[i] == '*' || s[i] == '|' || s[i] == '(' || s[i] == ')')
//当前为大写(小写)字母或者*、|、(、)
{
switch (s[i])
{
case '(':
if ((i + 1) < s.Length)
{
if (s[i + 1] == '*' || s[i + 1] == '|')//()内开头也不可以是闭包
{
MessageBox.Show("错误的输入");
return;
}
if (s[i + 1] == ')')
{
MessageBox.Show("错误的输入");
return;
}
}
count++;
break;
case '|':
if ((i + 1) < s.Length)
{
if (s[i + 1] == '*' || s[i + 1] == '|' || s[i + 1] == ')')
{
MessageBox.Show("错误的输入");
return;
}
}
break;
case '*':
if ((i + 1) < s.Length)
{
if (s[i + 1] == '*')
{
MessageBox.Show("错误的输入");
return;
}
}
break;
case ')':
count--;
break;
}
}
else
{
MessageBox.Show("错误的输入");
return;
}
if (count < 0)//只要出现)数量大于(时,一定是错误情况
{
MessageBox.Show("错误的输入");
return;
}
}
if (count != 0)//最终循环结束时,count=0才是括号匹配
{
MessageBox.Show("错误的输入");
return;
}
//CreateNFA.Enabled = true;
button4.Enabled = true;
MessageBox.Show("正确的输入");
}
private void button2_Click(object sender, EventArgs e)
{
richTextBox1.Text = " ";
}
private void button3_Click(object sender, EventArgs e)//读入NFA文件显示在listView1容器中
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(filePath);
listView1.Items.Clear();
foreach (string line in lines)
{
string[] parts = line.Split(',');
ListViewItem item = new ListViewItem(parts[0]);
item.SubItems.Add(parts[1]);
item.SubItems.Add(parts[2]);
item.SubItems.Add(parts[3]);
listView1.Items.Add(item);
}
}
}
private void button5_Click(object sender, EventArgs e)//保存NFA文件
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog1.FileName;
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (ListViewItem item in listView1.Items)
{
string line = item.Text + "," + item.SubItems[1].Text + "," + item.SubItems[2].Text + "," + item.SubItems[3].Text;
writer.WriteLine(line);
}
}
}
}
private void button6_Click(object sender, EventArgs e)//读入DFA文件显示在listView2容器中
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog1.FileName;
string[] lines = File.ReadAllLines(filePath);
listView2.Items.Clear();
foreach (string line in lines)
{
string[] parts = line.Split(',');
ListViewItem item = new ListViewItem(parts[0]);
item.SubItems.Add(parts[1]);
item.SubItems.Add(parts[2]);
listView2.Items.Add(item);
}
}
}
private void button8_Click(object sender, EventArgs e)//保存DFA文件
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog1.FileName;
using (StreamWriter writer = new StreamWriter(filePath))
{
foreach (ListViewItem item in listView2.Items)
{
string line = item.Text + "," + item.SubItems[1].Text + "," + item.SubItems[2].Text;
writer.WriteLine(line);
}
}
}
}
private void button7_Click(object sender, EventArgs e)//生成DFA文件显示在listView2容器中
{
listView2.Items.Clear();
//生成DFA代码
}
private void button9_Click(object sender, EventArgs e)//生成MFA文件显示在listView3容器中
{
listView3.Items.Clear();
//生成MFA代码
}
}
}
原文地址: https://www.cveoy.top/t/topic/kq62 著作权归作者所有。请勿转载和采集!