sd = new TreeSet<>();
String ss = "";
for (String s : arr) {
for (int i = 0; i < s.length(); i++) {
sd.add(s.charAt(i));
}
}
for (char c : sd) {
ss += c;
}
return ss;
}
//准备工作
// 获取S中每一个状态经过zifu生达到的状态
public static String getzifu(String s, String zifu) {
int i = s.length();
String s1 = "";
for (int j = 0; j < i; j++) {
char c = s.charAt(j);
String ss = String.valueOf(c);
for (String sss : zifuhu) {
if (sss.startsWith(ss) && sss.charAt(1) == zifu.charAt(0)) {
s1 += sss.charAt(2);
}
}
}
return s1;
}
//NFA中的每一个状态经过空弧后到达的状态
public static void KongJi() {
for (String s : nfazhuangtai) {
String sss = "";
tranji.add(s);
Get_ztj(s);
for (String ss : tranji)
sss += ss;
kongji.put(s, sss); // 某一字符经过空弧到达的状态集
tranji.clear();
}
}
// 对每一个状态遍历空弧找到等状态 在110行中使用
private static SortedSettranji = new TreeSet<>();
public static void Get_ztj(String zt) {
for (String s : konghu) {
if (s.startsWith(zt)) {
tranji.add(s.substring(2));
Get_ztj(s.substring(2));
}
}
}
public static void Get_DfaFirst(String cszt) {
for (String s : konghu) {
if (s.startsWith(cszt)) {
dfachutai += s.substring(2);
Get_DfaFirst(s.substring(2));
}
}
}
public static void GetDfaFirst() {
dfachutai = chushizhuangtai;
Get_DfaFirst(chushizhuangtai);
}
public static void wjGet_Nfa(){
File file = new File("E:/ceshi.txt");
String s =null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while((s=br.readLine())!=null){
char c = s.charAt(0);
switch (c) {
case '0':
chushizhuangtai=s.substring(1);
break;
case '1':
jieshuzhuangtai.add(s.substring(1));
break;
case '2':
zifuji.add(s.substring(1));
break;
case '3':
nfazhuangtai.add(s.substring(1));
break;
case '4':
if(s.charAt(2)=='#')
konghu.add(s.substring(1));
else
zifuhu.add(s.substring(1));
break;
default:
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void Get_Nfa() {
System.out.print("输入初始状态:");
Scanner s = new Scanner(System.in);
chushizhuangtai = s.next(); // 初始状态
System.out.println("输入结束状态:");
Scanner sc5 = new Scanner(System.in);
while (true) {
String jieshu = sc5.next();
if (jieshu.equals("over"))
break;
else {
jieshuzhuangtai.add(jieshu);
}
}
System.out.println("输入字符集:"); // 字符集
Scanner s1 = new Scanner(System.in);
while (true) {
String ss1 = s1.next();
if (ss1.equals("over")) {
break;
} else {
zifuji.add(ss1);
}
}
System.out.println("输入NFA状态集:");
Scanner s2 = new Scanner(System.in);
while (true) {
String ss2 = s2.next();
if (ss2.equals("over")) {
break;
} else {
nfazhuangtai.add(ss2);
}
}
System.out.println("输入函数集:"); // 函数集
Scanner sc = new Scanner(System.in);
while (true) {
String nfa = sc.next();
if (nfa.equals("over"))
break;
else if (nfa.charAt(1) == '#')
konghu.add(nfa);
else
zifuhu.add(nfa);
}
}
//这个本来是主函数,读者可自己修改
public static void zhixing() {
wjGet_Nfa();
GetDfaFirst();
KongJi();
xhdfahanshu(dfachutai);
// new Tuxing(dfahanshu,a4);
System.out.println(konghu);
System.out.println(zifuhu);
System.out.println(dfachutai);
System.out.println(kongji);
System.out.println(dfahanshu);
}
}
如果觉得《java nfa dfa_Java NFA转DFA》对你有帮助,请点赞、收藏,并留下你的观点哦!