2017-02-28 73 views
0

好的男孩和女孩在这里我们去。首先,我有几个问题。由于我的计划很大,我只会分阶段提出问题,这个问题是第一个问题。我创建了一个为postfix逻辑表达式生成真值表的程序。下面是允许运营商和他们的逻辑等价物:Python字典使用真值表逻辑输入

Operators: 
=    Logical Equivalence (≡ or ↔) 
`->` or `<=`  Logical Implication (→) 
+    Disjunction (∨), AKA “or” 
*    Conjunction (∧), AKA “and” 
`~` or `!`  Negation (¬), AKA “not” 

下面是输入和输出的一些例子:

input 
p True = 

output 
p  p True = 
False False 
True True 

input 
p ! 

output 
p  p ! 
False True 
True False 

input 
p q = 

output 
p  q  p q = 
False False True 
False True False 
True False False 
True True True 

好,我真的不知道哪里开始,但我不要求任何人为我写这个程序。我知道我需要使用Python字典来编写代码,它将键匹配到相应的命题。但是,我怎么知道哪些投入了钥匙,哪些投入了价值?此外,在的情况下:

`->` or `<=`  Logical Implication (→) 

`~` or `!`  Negation (¬), AKA “not” 

如何分配2个不同的输入能在Python字典中使用?我希望这不是太混乱,我在python非常noob,任何帮助表示赞赏。谢谢!

UPDATE 确定这里是我现在的代码:

propositions = { 
    '=' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True) : False, 
       (False, False): True, 
       }), 
    '->' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True): True, 
       (False, False): True, 
       }), 
    '+' : (2, {(True, True): True, 
       (True, False): True, 
       (False, True): True, 
       (False, False): False, 
       }), 
    '*' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True): False, 
       (False, False): False, 
       }), 
    '!' : (1, {True: False, 
       False: True})} 

prop = sys.stdin.readline() 
prop = prop.split() 
prop = prop[::-1] 
for x in prop: 

我相信我成功逆转字符串,并删除了所有的空格,但我还是有点困惑通过它迭代。

SECOND这里UPDATE是我的代码:

propositions = { 
    '=' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True) : False, 
       (False, False): True, 
       }), 
    '->' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True): True, 
       (False, False): True, 
       }), 
    '+' : (2, {(True, True): True, 
       (True, False): True, 
       (False, True): True, 
       (False, False): False, 
       }), 
    '*' : (2, {(True, True): True, 
       (True, False): False, 
       (False, True): False, 
       (False, False): False, 
       }), 
    '!' : (1, {True: False, 
       False: True})} 

prop = sys.stdin.readline() 
prop = prop.strip().split() 
prop = reversed(prop) 
def evaluate(): 
    token = next(prop) 
    try: 
     nargs, table = propositions[token] 
    except KeyError: 
     if token.lower() in ('true', '1'): 
      return True 
     elif token.lower() in ('false', '0'): 
      return False 
     else: 
      return token 
    return table[tuple(evaluate() for i in range(nargs))] 

回答

2

你必须从外部分辨率的顺序,以建立您的类型的字典到内:

master_dict = { 
    '=': (2, {(True, True): True, 
      (True, False): False, 
      ... 
      }), 
    ... 
    '!': (1, {True: False, 
      False: True})} 

的数字表示操作多少操作数取。

解析输入从右到左读取它。

使用递归函数,从右侧消耗一个标记。

(1)如果令牌是操作符(即字典中的键),则从主字典中检索相应的值。

首先存储的数字是操作员所采用的参数数量。所以你的函数现在必须多次调用自己的次数。一定要跟踪哪些令牌已被读取。这样做的一个简单方法是使用列表迭代器,它会将每个元素精确地吐出一次,所以不会导致索引错误。一旦你有了所有的参数,你就可以应用你刚才检索的真值表,读出结果并返回。 (2)如果令牌不是读者,你的函数必须返回它。

prop = sys.stdin.readline() 

def solve_no_var(prop): 
    rev_iter = reversed(prop) 
    def evaluate(): 
     token = next(rev_iter) 
     try: 
      nargs, table = propositions[token] 
     except KeyError: 
      if token.lower() in ('true', '1'): 
       return True 
      elif token.lower() in ('false', '0'): 
       return False 
      else: 
       return token 
     return table[tuple(evaluate() for i in range(nargs))] 
    return evaluate() 

def solve(prop): 
    prop = prop.strip().split() 
    variables = list(set(prop) - set(propositions) 
     - {'True', 'TRUE', 'true', '1', 'False', 'FALSE', 'false', '0'}) 
    lookup = {v: [j for j, p in enumerate(prop) if p == v] for v in variables} 
    N = len(variables) 
    print((N*" {:6} ").format(*variables), 'result') 
    for p in itertools.product(("True", "False"), repeat=N): 
     prop_nv = prop.copy() 
     for v, b in zip (variables, p): 
      for j in lookup[v]: 
       prop_nv[j] = b 
     res = solve_no_var(prop_nv) 
     print(((N+1)*" {:6} ").format(*(p + (res,)))) 

solve(prop) 
+0

非常感谢! – Coder117

+0

再次。我相信我已经正确建立了我的词典,现在我该如何阅读输入内容?我知道我需要使用堆栈。 – Coder117

+0

@ T.Dog更新了帖子。应该让你开始。 –