python表达式求值求根公式_python 求值表达式解析

采用中缀转后缀的算法。

注意我输入的格式。

#注意格式

def suffix(st):

listopt=[" "]

listnum=[" "]

for i in range(0,len(st)):

if(differ(st[i])==1): #判断,对运算符操作

if(len(listopt)):

if(dictopt[st[i]] > dictopt[listopt[len(listopt)-1]]):#优先级比栈顶高,入栈

if st[i]==")":

while(1):

tmp=listopt.pop()

if tmp=="(":

break

else:

listnum.append(tmp)

listnum.append(" ")

else:

listopt.append(st[i])

else: #如果st[i]优先级比栈顶低,opt栈中依次放到num中,然后再把st[i]入opt栈

if st[i]=="(": #优先级低于栈顶元素的,可能是 加减乘除,也可能是"("。如果碰到 "("则 直接入栈

listopt.append(st[i])

else:

while(dictopt[st[i]]

tmp=listopt.pop()

listnum.append(tmp)

listnum.append(" ") #运算符之间加空格,否则print cnt_string:“ 1.2 5 6 ** 57 14 - + ”

listopt.append(st[i])

else: #非运算符的操作,依次入num栈

listnum.append(st[i])

while(len(listopt)): #opt栈 依次放到 num栈

listnum.append(" ") #运算符前面加空格,否则print cnt_string:“ 1.2 5 6 * * 57 14-+ ”

listnum.append(listopt.pop())

return listnum

#判断是运算符还是操作数:

def differ(elem):

if elem=="+" or elem=="-" or elem=="*" or elem=="/" or elem=="(" or elem==")":

return 1

else:

return 0

#整理字符串,列表,去除不必要的空格:

def order(st):

suffix_list=[]

tmp_list=suffix(st)

#print suffix_list

last_string="".join(tmp_list)

#print last_string

cnt_string=last_string.replace(" "," ")

#print cnt_string

cnt_string=cnt_string[1:len(cnt_string)-1] #空格去头去尾

cnt_list_tmp=cnt_string.split(" ")

for i in cnt_list_tmp:

if i!="":

suffix_list.append(i)

print suffix_list

return suffix_list

#实现类似switch-case 功能:

def calc(type,x,y):

calculation = {"+":lambda x,y:( eval(x) + eval(y)),

"*":lambda x,y:( eval(x) * eval(y)),

"-":lambda x,y:( eval(x) - eval(y)),

"/":lambda x,y:( eval(x) / eval(y))

}

return calculation[type](x,y)

#usage :result1 = calc('+',3,6)

#计算:

def count(suffix_list):

tmp_list=[]

for i in suffix_list:

if not differ(i):

tmp_list.append(i)

else:

tmp1=tmp_list.pop()

tmp2=tmp_list.pop()

tmp3=calc(i,str(tmp2),str(tmp1))

tmp_list.append(tmp3)

return tmp_list[0]

#main

dictopt={"+":1,"-":1,"*":2,"/":2," ":0,"(":-1,")":9} #优先级

st="1.2 - ( 5 * 6 + 7 + 8 ) * 4"#待求表达式

suffix_list=order(st)

answer=count(suffix_list)

print answer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python表达式求值是指根据给定的Python表达式计算出结果的过程。它涉及解析表达式,按照运算符的优先级和结合性进行计算,并返回最终的结果。 在上述引用中,提到了一种方法来编程演示如何一步一步地计算Python表达式。这种方法涉及使用ast模块解析表达式为抽象语法树(AST),然后逐步求值AST节点。首先,使用ast.parse()将字符串解析为AST。然后,找到一个首先要求值的节点,并使用eval(compile(node, '', 'eval'))对其求值求值结果可以转换回AST节点,并用结果节点替换当前节点。接着,使用codegen.to_source从修改后的AST生成修改后的代码字符串,并继续相同的过程,直到树中只有一个节点。 这种方法可以逐步展示Python表达式求值过程。它可以用于教学目的,帮助学生理解表达式求值方式。然而,需要注意的是,这种方法可能无法处理某些复杂的行为,比如列表理解。在处理这些复杂情况时,可能需要采用其他方法或工具。 总结:Python表达式求值是根据给定的表达式计算结果的过程。使用ast模块可以解析表达式为AST,并通过逐步求值AST节点来展示求值过程。然而,对于复杂的行为,可能需要其他方法或工具来处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [逐步跟踪Python表达式求值](https://blog.csdn.net/weixin_30982943/article/details/112956371)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值