今天研究下truth table,算法鸽一天(真不是我懒,相信我QAQ
代码整体框架基本是借照这位老哥写的真值表-Python实现
不得不说这位老哥对递归吃的很透彻,意想不到的用法,其他代码的写法也给我很多启发
感谢这位老哥的分享

我对实现代码有些地方做了些小优化,不过整体区别不大

本文的记录意义大过教学意义,注释写的很少,想看详细些的可以去那位老哥的文章看,他写的注释多

def change_prefix_form(truth_formula):              # 将字符串式转换为后缀式operators_list = ["!", "&", "|", "*", "+"]operators_stack = []final_stack = []for i in truth_formula:if i not in operators_list and i not in ["(", ")"]:final_stack.append(i)elif i == "(":operators_stack.append(i)elif i == ")":pop_value = operators_stack.pop()while pop_value != "(" and len(operators_stack) >= 1:final_stack.append(pop_value)pop_value = operators_stack.pop()elif i in operators_list:if len(operators_stack) == 0:operators_stack.append(i)else:while operators_stack \and operators_stack[-1] in operators_list \and operators_list.index(operators_stack[-1]) <= operators_list.index(i):final_stack.append(operators_stack.pop())operators_stack.append(i)while operators_stack:final_stack.append(operators_stack.pop())return "".join(final_stack)def find_all_element(prefix_string):            # 找到字符串中所有不重复的元素,不包含运算符和括号list1 = []for i in prefix_string:if i not in ["!", "&", "|", "*", "+", "(", ")"] and i not in list1:list1.append(i)return list1def create_dict(diff_element):                  # 建立字典,类似于:{“p”: None, "q": None}return {i: None for i in diff_element if i not in ["!", "&", "|", "*", "+", "(", ")"]}def main_judge(temp_list):                      # 进行判断,and, or, not, If then, Equivalent topointer = 0while len(temp_list) > 1:if temp_list[pointer] in ["!", "&", "|", "*", "+"]:if temp_list[pointer] == "!":  # nottemp_list[pointer - 1] = 1 if int(temp_list[pointer - 1]) == 0 else 0temp_list[pointer:pointer + 1] = []pointer = 0continueelif temp_list[pointer] == "&":  # andtemp_list[pointer] = 1 if int(temp_list[pointer - 2]) == 1 and int(temp_list[pointer - 1]) == 1 else 0temp_list[pointer - 2:pointer] = []pointer = 0continueelif temp_list[pointer] == "|":  # ortemp_list[pointer] = 0 if int(temp_list[pointer - 2]) == 0 and int(temp_list[pointer - 1]) == 0 else 1temp_list[pointer - 2:pointer] = []pointer = 0continueelif temp_list[pointer] == "*":  # If thentemp_list[pointer] = 0 if int(temp_list[pointer - 2]) == 1 and int(temp_list[pointer - 1]) == 0 else 1temp_list[pointer - 2:pointer] = []pointer = 0continueelif temp_list[pointer] == "+":  # Equivalent totemp_list[pointer] = 1 if int(temp_list[pointer - 2]) == int(temp_list[pointer - 1]) else 0temp_list[pointer - 2:pointer] = []pointer = 0continuepointer += 1print(temp_list[0])def enumerate_all(index, element_dict, element_list, prefix_string):      # 进行循环,来得到truth table的初始值if index == len(element_dict):print("\t".join(list(element_dict.values())), end="\t")temp_list = [element_dict[i] if i in element_dict else i for i in prefix_string]  # ["0", "0", "&"]...main_judge(temp_list)returnelement_dict[element_list[index]] = "0"enumerate_all(index + 1, element_dict, element_list, prefix_string)element_dict[element_list[index]] = "1"enumerate_all(index + 1, element_dict, element_list, prefix_string)if __name__ == "__main__":"""!:not &:and|:or*:If then+:Equivalent to"""# 例子1: p&q|r*q&!s|r# 例子2: s|(p&q)# (p*q)&!r  p&q|r*q&!s|roriginal_string = input("pls enter the truth formula: ")print("-" * (len(original_string) - 1) * 5)# 1.转换原表达式为后缀形式prefix_string = change_prefix_form(original_string)# 2.拿到所有不同的元素   类似p, q, s...element_list = find_all_element(prefix_string)# 3.创建元素字典element_dict = create_dict(element_list)# 4.打印表头print('\t'.join(element_list) + "\t" + original_string)print("-" * (len(original_string)-1)*5)# 5.枚举每一个元素enumerate_all(0, element_dict, element_list, prefix_string)

TruthTable真值表---python相关推荐

  1. 两片8-3优先编码器转化为16-4线优先编码器真值表--python实现

    假设有0号和1号编码器 0号输入端从I00'到I07' 1号编码器输入端I11'到I17' 输出为Z3'~Z0' 0号编码器控制1号编码器 当0号编码器无输入信号时,1号编码器开始工作 实现代码: # ...

  2. 真正的自由是节制欲望自律简朴地去生活

    卢梭说:"人生而自由,却无往不在枷锁中."自由无价,但人总是不免困于世俗.困于心.困于情,有执着和偏见,更有痛苦.徘徊与挣扎.每个人都在寻找自由的出口,期望抵达心灵的港湾. 生命的 ...

  3. python实现真值表

    python实现真值表 import copydef boo(x,leng,listboo,array): #递归实现布尔真值全排列if x==leng:for i in range (0,leng) ...

  4. 使用python输出真值表

    首先创建一个类,利用python自身的优势对表达式进行解析 # &为合取,v为析取,~为非,>为条件联结词,*为双条件联结词 class Variable:def __init__(se ...

  5. python - 根据表达式打印真值表

    输入逻辑表达式,输出真值表,支持六个基本逻辑运算 最终效果:     输入合适公式(没有考虑优先级,只根据括号优先),输出时会提取其中的元素(比如这里有A B C),并打印真值表.  算法思路: 求值 ...

  6. python 中的真值表

    逻辑术语 and :与 or:或 not :非 !=:不等于 ==:等于 =:大于等于 <=:小于等于 True:真 False:假 not 真假 not False True not True ...

  7. 一个 零差评的 Python 内置库

    前言 最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了. 很多人都致力于把Python代码写得更 ...

  8. 计算机编程书籍-笨办法学Python 3:基础篇+进阶篇

    编辑推荐: 适读人群 :本书适合所有已经开始使用Python的技术人员,包括初级开发人员和已经升级到Python 3.6版本以上的经验丰富的Python程序员. "笨办法学"系列, ...

  9. python学习笔记之运算符

    目录 前言 软件环境 身份运算符 算术运算符 比较运算符 位移运算符 自变运算符 位运算符 逻辑运算符 成员关系运算符 Python真值表 最后 前言 在前面的博文介绍了Python的数据结构之后,接 ...

最新文章

  1. mysql删除表中的唯一索引吗_Mysql 使用sql删除同表中重复数据并加唯一索引
  2. 基于VMwareWorkstation技术预览版2012上的WinServer8测试版安装
  3. ASP.NET Core 入门教程 2、使用ASP.NET Core MVC框架构建Web应用
  4. 语言用加法实现加饭运算_「编程之美」用C语言实现状态机(超实用)
  5. JS关键字和保留字汇总
  6. 【几何/数学】概念的理解 —— (非)刚体变换((non-)rigid transformation)
  7. 组合数学在软件领域的运用
  8. android制作镜像,android镜像制作方法
  9. MQTT网关是什么?
  10. 微信小程序自动检测新版本并静默更新,及热启动和冷启动
  11. numeric mysql_MYSQL的DECIMAL和NUMERIC类型
  12. python入门教材 52pj_PJzhang:python基础入门的7个疗程-five
  13. C语言拯救者 (操作符--6)
  14. html计算圆周长,【实用性程序】弧微分计算圆周长
  15. 是谁浇了李彦宏一头水?
  16. 【R语言】——火山图绘制
  17. 自己实现Dubbo Invoker
  18. centos下遇见unzip命令错误及解决
  19. python 处理url 参数_python模块 furl 使得操纵URL简单化,去除网址中参数
  20. c++数组求和函数 accumulate()

热门文章

  1. PIC 单片机的振荡器及程序烧写
  2. 使用R语言做极大似然估计实例
  3. 《趣谈网络协议》学习笔记 DAY02
  4. Flink整合Prometheus Pushgetway讲解与实战操作
  5. Qt相关一个编译错误:'staticMetaObject' is not a member of ‘XXXClass’
  6. Nagios bacula
  7. informix笔记
  8. Python视频剪辑基础教程:MoviePy VideoClip详解
  9. 正青春,加油吧青年人!
  10. 树莓派安装opencv报错“opencv2/xfeatures2d/cuda.hpp”,解决办法