枚举的参数带入

博客提到枚举对象能够带入多个参数,但目前来看这样做暂无明显意义。

枚举对象可以带入多个参数。。。。。虽然到目前为止,这样没有什么意思。。。。。




operators = ['!','&', '|', '*', '+'] # 按照优先级从高到低的顺序 def postfix(elements): global operators stack = list() output = list() for ele in elements: if ele not in operators and ele not in ('(',')'): # output.append(ele) # '(' 在运算符中优先级最小,其唯一出栈条件时遇到右括号 elif ele == '(': stack.append(ele) # 若是 ) ,则出栈直到遇到 ( ,这里注意: ) 遇到的第一个 ( 一定是匹配的一对 elif ele == ')': val = stack.pop() while val !='(': output.append(val) if stack: val = stack.pop() else: break elif ele in ('&', '|', '*', '+', '!'): # 遇到运算符,比较优先级 if len(stack) == 0: stack.append(ele) continue # 比较该运算符与栈顶运算符的优先级,遇到比该运算符优先级大于或等于的则将其弹出,最后将该运算符压栈 while (ele == '!' and stack[-1] =='!') or \ (ele == '&' and stack[-1] in ('!', '&')) or \ (ele == '|' and stack[-1] in ('!', '&', '|')) or \ (ele == '*' and stack[-1] in ('!', '&', '|', '*')) or \ (ele == '+' and stack[-1] in ('!', '&', '|', '*', '+')): val = stack.pop() output.append(val) if not stack: break stack.append(ele) while stack: # 当表达式完全处理完之后,把栈中的运算符一一出栈,FILO,转化成后缀表表达式 output.append(stack.pop()) return ''.join(output) def logicalOp(tmp): idx = 0 # 当前命题元素的位置 while len(tmp) > 1: # 当最后命题仅为真值后,退出循环 if tmp[idx] in operators: if tmp[idx] == '!': # 非 # 这里把命题变元进行转换,根据后缀表达式,一定满足idx的前1位或前2位是真值而不是运算符 tmp[idx - 1] = 1 if int(tmp[idx - 1]) == 0 else 0 tmp[idx:idx + 1] = [] # 子命题结果对原命题覆盖 # 每次从头开始对命题处理 idx = 0 continue elif tmp[idx] == '&': # 合取 tmp[idx] = 1 if int(tmp[idx - 1]) == 1 and int(tmp[idx - 2]) == 1 else 0 tmp[idx - 2:idx] = [] idx = 0 continue elif tmp[idx] == '|': # 析取 tmp[idx] = 0 if int(tmp[idx - 1]) == 0 and int(tmp[idx - 2]) == 0 else 1 tmp[idx - 2:idx] = [] idx = 0 continue elif tmp[idx] == '*': # 则 tmp[idx] = 0 if int(tmp[idx - 2]) == 1 and int(tmp[idx - 1]) == 0 else 1 tmp[idx - 2:idx] = [] idx = 0 continue elif tmp[idx] == '+': # 当且仅当 tmp[idx] = 1 if int(tmp[idx - 2]) == int(tmp[idx - 1]) else 0 tmp[idx - 2:idx] = [] idx = 0 continue idx += 1 print(tmp[0]) def e(idx): global expr if idx == len(enum): # 递归终止条件为枚举完全部的变元 print('\t'.join(list(enum.values())),end='\t') # 打印出枚举情况 tmp = ' '.join(expr) # tmp为对命题处理带入真值的中间量 for ele in expr: if ele in enum: tmp = tmp.replace(ele, enum[ele]) tmp = tmp.split(' ') # 转化成list,由于字符串内部不能修改 logicalOp(tmp) return enum[var[idx]] = '0' # 枚举False,最后在转换为int类,使得可使用字符串替换,把0,1代入 e(idx+1) # 接着对下一位变元枚举 enum[var[idx]] = '1' # 枚举True e(idx+1) if __name__=='__main__': """ Attention : 定义: !:非 (单命题变元) &:合取 |:析取 *:单条件(则) +:双条件(当且仅当) """ print('please input the problem\tExample: (p*q)&!r p&q|r*q&!s|r') inp = input('>>>') expr = postfix(inp) # expr为生成的后缀表达式 var = list() # var为命题变元 for item in expr: # 找出变元且不能重复 if item not in operators and \ item not in var and \ item not in ('(', ')'): var.append(item) # 对变元枚举TF字典 enum = {}.fromkeys(var) # 打印表头 print('\t'.join(var),end='\t') print(inp) # 从第一个变元枚举 e(0) 将以上代码转化为C语言代码
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值