C# 正则表达式验证器:NFA、DFA 和 MFA 生成器
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容器中
{
}
private void button5_Click(object sender, EventArgs e)//保存NFA文件
{
}
private void button6_Click(object sender, EventArgs e)//读入DFA文件显示在listView2容器中
{
}
private void button8_Click(object sender, EventArgs e)//保存DFA文件
{
}
private void button7_Click(object sender, EventArgs e)//生成DFA文件显示在listView2容器中
{
}
private void button9_Click(object sender, EventArgs e)//生成MFA文件显示在listView3容器中
{
}
}
}
// 统计字符串s中不同的字母个数并返回该值 private 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; }
// 判断字符x是否在字符串s中出现过,若出现过返回false,否则返回true 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; }
原文地址: https://www.cveoy.top/t/topic/kq3P 著作权归作者所有。请勿转载和采集!