标题:谁能帮我把下面这个程序用go语言实现一下啊,感谢感谢
取消只看楼主
yang158
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2019-3-12
结帖率:16.67%
 问题点数:0 回复次数:0 
谁能帮我把下面这个程序用go语言实现一下啊,感谢感谢
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()
搜索更多相关主题的帖子: temp print inStr if 输入 
2020-10-25 12:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-503622-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.098972 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved