zoukankan      html  css  js  c++  java
  • 数据结构:基于list实现二元表达式(python版)

      1 #!/usr/bin/env python
      2 # -*- coding:utf-8 -*-
      3 
      4 def make_sum(a, b):
      5     return ['+', a, b]
      6 
      7 def make_prod(a, b):
      8     return ['*', a, b]
      9 
     10 def make_diff(a, b):
     11     return ['-', a, b]
     12 
     13 def make_div(a, b):
     14     return ['/', a, b]
     15 
     16 def is_basic_exp(a):
     17     return not isinstance(a, list)
     18 
     19 def is_number(x):
     20     return (isinstance(x, int) or isinstance(x, float) or isinstance(x, complex))
     21 
     22 #表达式计算
     23 def eval_exp(e, values):
     24     if is_basic_exp(e):
     25         #如果e位于字典里则返回键对应的值,否则直接返回e
     26         if e in values.keys():
     27             return values[e]
     28         else:
     29             return e
     30     op, a, b = e[0], eval_exp(e[1], values), eval_exp(e[2], values)
     31     if op=='+':
     32         return eval_sum(a, b)
     33     elif op=='-':
     34         return eval_diff(a, b)
     35     elif op=='*':
     36         return eval_prod(a, b)
     37     elif op=='/':
     38         return eval_div(a, b)
     39     else:
     40         raise ValueError("Unknown operator:", op)
     41 
     42 #加法
     43 def eval_sum(a, b):
     44     if is_number(a) and is_number(b):
     45         return a+b
     46     if is_number(a) and a==0:
     47         return b
     48     if is_number(b) and b==0:
     49         return a
     50     return make_sum(a, b)
     51 
     52 #减法
     53 def eval_diff(a, b):
     54     if is_number(a) and is_number(b):
     55         return a - b
     56     if is_number(a) and a==0:
     57         return -b
     58     if is_number(b) and b==0:
     59         return a
     60     return make_diff(a, b)
     61 
     62 #乘法
     63 def eval_prod(a, b):
     64     if is_number(a) and is_number(b):
     65         return a * b
     66     if is_number(a) and a==0:
     67         return 0
     68     if is_number(b) and b==0:
     69         return 0
     70     return make_prod(a, b)
     71 
     72 #除法
     73 def eval_div(a, b):
     74     if is_number(a) and is_number(b):
     75         return a / b
     76     if is_number(a) and a==0:
     77         return 0
     78     if is_number(b) and b==1:
     79         return a
     80     if is_number(b) and b==0:
     81         raise ZeroDivisionError
     82     return make_div(a, b)
     83 
     84 #取出表达式里所有变量的集合
     85 var_list = []  #这里使用全局变量
     86 def varibles(exp):
     87     for i in range(1,3):
     88         if is_basic_exp(exp[i]):
     89             var_list.append(exp[i])
     90         else:
     91             varibles(exp[i])
     92     return var_list
     93 
     94 
     95 if __name__=='__main__':
     96     e1 = make_prod(make_sum('a',3), make_sum('b',make_sum(4,6)))
     97     print(e1)
     98     print("变量集合:",varibles(e1))
     99     values = {}
    100     values['a'] = 3
    101     values['b'] = 4
    102     print(values)
    103     print(eval_exp(e1, values))
  • 相关阅读:
    Android的数据存储
    Servlet第一天
    JavaScript高级程序设计读书笔记(3)
    Interesting Papers on Face Recognition
    Researchers Study Ear Biometrics
    IIS 发生意外错误 0x8ffe2740
    Father of fractal geometry, Benoit Mandelbrot has passed away
    Computer vision scientist David Mumford wins National Medal of Science
    Pattern Recognition Review Papers
    盒模型bug的解决方法
  • 原文地址:https://www.cnblogs.com/xautxuqiang/p/6125902.html
Copyright © 2011-2022 走看看