#2
golang2020-12-26 17:47
程序代码: package main import ( "fmt" "strings" ) type State struct { listEdge [][]string // 状态集 S []string // 初态 Z []string // 终态 StaInf } type StaInf interface { isZ(ch string) bool input() isNextState(s, ch string) string judeDFA() } // 判断是否是终态集 func (st *State) isZ(ch string) bool { if st.Z[0] == ch || st.Z[1] == ch { return true } else { return false } } func (st *State) input() { var strS string fmt.Println("请输入开始符:") _, _ = fmt.Scanln(&strS) st.S = strings.Split(strS,"") var strZ string fmt.Println("请输入终态集(终集符组成的一个字符串):") _, _ = fmt.Scanln(&strZ) st.Z = strings.Split(strZ,",") fmt.Println("请输入正规文法以exit结尾:") fmt.Println("example:S,aZ") var inStr string for { _, _ = fmt.Scanln(&inStr) if inStr == "exit" { break } inStrSlice := strings.Split(inStr, ",") // 读取第一个状态集 s := inStrSlice[0] instr1 := strings.Split(inStrSlice[1], "") for i := 0; i < len(instr1); i++ { if len(instr1) == 2 { c := instr1[0] n := instr1[1] list := []string{s, c, n} st.listEdge = append(st.listEdge, list) } else if len(instr1) == 1 { c := instr1[0] list := []string{s, c, st.Z[0]} st.listEdge = append(st.listEdge, list) } } } } // 转换函数 func (st *State) isNextState(s, ch string) string { for _, value := range st.listEdge { if s == value[0] && ch == value[1] { return value[2] } } return "" } func (st *State) judeDFA() { fmt.Println("请输入开始符:") var inStr string for { _, _ = fmt.Scanln(&inStr) if strings.Contains(inStr, "#") { fmt.Println("程序已退出,欢迎下次使用!") return } temp := st.S[0] for i := 0; i < len(inStr); i++ { instrSlice := strings.Split(inStr, "") if instrSlice[i] == "a" { temp = st.isNextState(temp, "a") } else if instrSlice[i] == "b" { temp = st.isNextState(temp, "b") } else { break } } if st.isZ(temp) { fmt.Println("此字符串“属于”该文法!") } else { fmt.Println("此字符串“不属于”该文法!") } fmt.Println("再次判断请输入字符串(退出程序输入#):") } } func main() { var state State state.input() state.judeDFA() } |
class DFA():
def __init__(self):
# 状态集
self.listEdge = []
# 初态
self.S = []
# 终态
self.Z = []
# 判断是否是终态集
def isZ(self, ch):
for i in range(0, len(self.Z)):
if self.Z[0] == ch or self.Z[1] == ch:
return True
else:
return False
# 输入
def input(self):
self.S = input("请输入开始符:")
self.Z = input("请输入终态集(终集符组成的一个字符串):")
self.Z = self.Z.split(",")
print("请输入正规文法以exit结尾:")
print("example:S,aZ")
while (True):
list = []
inStr = input()
if inStr == 'exit':
break
inStr = inStr.split(',')
# 读取第一个状态集
s = inStr[0]
for i in range(0, len(inStr[1])):
# ch,ns
if len(inStr[1]) == 2:
c = inStr[1][0]
n = inStr[1][1]
list = [s, c, n]
self.listEdge.append(list)
elif len(inStr[1]) == 1:
c = inStr[1][0]
list = [s, c, self.Z[0]]
self.listEdge.append(list)
# 转换函数
def isNextState(self, s, ch):
for i in range(0, len(self.listEdge)):
if s == self.listEdge[i][0] and ch == self.listEdge[i][1]:
return self.listEdge[i][2]
return
def judgeDFA(self):
print("请输入要判断的字符串:")
while (True):
# 获取字母表
str = input()
if '#' in str:
print("程序已退出,欢迎下次使用!")
return
temp = self.S[0]
for i in range(0, len(str)):
if str[i] is 'a':
temp = self.isNextState(temp, 'a')
elif str[i] is 'b':
temp = self.isNextState(temp, 'b')
else:
break
if self.isZ(temp):
print("此字符串“属于”该文法!")
else:
print("此字符串“不属于”该文法!")
print("再次判断请输入字符串(退出程序输入#):")
if __name__ == '__main__':
DFA = DFA()
DFA.input()
DFA.judgeDFA()