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 openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "NFA文件|*.nfa";
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fileName = openFileDialog.FileName;
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string line;
            int i = 0;
            while ((line = sr.ReadLine()) != null)
            {
                string[] str = line.Split(',');
                ListViewItem item = new ListViewItem();
                item.Text = Convert.ToString(i + 1);
                item.SubItems.Add(str[0]);
                item.SubItems.Add(str[1]);
                item.SubItems.Add(str[2]);
                listView1.Items.Add(item);
                i++;
            }
            sr.Close();
        }
    }

    private void button5_Click(object sender, EventArgs e)//保存NFA文件
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "NFA文件|*.nfa";
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fileName = saveFileDialog.FileName;
            StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
            foreach (ListViewItem item in listView1.Items)
            {
                string str = item.SubItems[1].Text + "," + item.SubItems[2].Text + "," + item.SubItems[3].Text;
                sw.WriteLine(str);
            }
            sw.Close();
        }
    }

    private void button6_Click(object sender, EventArgs e)//读入DFA文件显示在listView2容器中
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        openFileDialog.Filter = "DFA文件|*.dfa";
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fileName = openFileDialog.FileName;
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string line;
            int i = 0;
            while ((line = sr.ReadLine()) != null)
            {
                string[] str = line.Split(',');
                ListViewItem item = new ListViewItem();
                item.Text = Convert.ToString(i + 1);
                item.SubItems.Add(str[0]);
                item.SubItems.Add(str[1]);
                listView2.Items.Add(item);
                i++;
            }
            sr.Close();
        }
    }

    private void button8_Click(object sender, EventArgs e)//保存DFA文件
    {
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        saveFileDialog.Filter = "DFA文件|*.dfa";
        if (saveFileDialog.ShowDialog() == DialogResult.OK)
        {
            string fileName = saveFileDialog.FileName;
            StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
            foreach (ListViewItem item in listView2.Items)
            {
                string str = item.SubItems[1].Text + "," + item.SubItems[2].Text;
                sw.WriteLine(str);
            }
            sw.Close();
        }
    }

    private void button7_Click(object sender, EventArgs e)//生成DFA文件显示在listView2容器中
    {
        // 1.确定字母表
        // 2.对初始状态进行 e-closure 运算,得到 DFA 起始状
        // 3.开始子集构造
        // 标记已经加入到 DFA 中的状态
        // 待处理的状态集合队列

        //判断markedSet中是否已经存在U
        // 生成新状态
        // 判断一个状态集合是否为终止状态
    
        // 获取某个状态集合通过 symbol 转移的结果
    
        // e-closure 运算
         // e-closure 运算
    }


    private void button9_Click(object sender, EventArgs e)//生成MFA文件显示在listView3容器中
    {

    }

}

}

NFA to DFA Converter: A C# Implementation with GUI

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

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