如何在类属性上使用算术运算?
我想写一个可以解决算术运算的代码。这些类mult
和plus
是为了解决类似的问题:如何在类属性上使用算术运算?
e1 = mult(const(3),const(4))
e1 = plus(const(3),const(4)
并返回结果(分别为12和7)。
现在从讲师的分配要我们写一个能解决程序类似这样的问题:
e1 = mult(plus(const(3),const(4)),const(5)) # should print 35
e2 = plus(mult(const(3),const(4)),const(5)) # should print 12
我试图解决在使用列表中的问题,以便常量存储在列表(即store_const
),那么类mult(const(n),const(n))
和plus(const(n),const(n))
将解决日问题并返回结果。我的问题是为每个类mult
/plus
,我创建了添加或乘以store_const[0]
和[1]
的方法eval
,但我每次运行该程序:E1返回12和e2回报7
我想这是因为mult
/plus
只需要store_const[0] = 3
和store_const[1] = 4
,并省略store_const[2] = 5
。
所以,我怎么能解决这个问题:
e1 = mult(plus(const(3),const(4)),const(5))
其中plus
的结果存储为新const(n)
,然后mult
类能够执行来产生35K
这里是我的代码:
class expr(object): #ignore class
pass
class const(expr):
store_const = [] #list to store constants
def __init__(self, n): #initial constructor
self.n = n
const.store_const.append(self.n) #store constants in list
class binOp(expr): #sublcass of expr
def __init__(self, expr1, expr2): #initial constructor arguments
self.expr1 = expr1
self.expr2 = expr2
class mult(binOp):
def __int__(self, *args):
super(mult, self).__init__(*args) #inheriting the superclass constructor arguments
def eval(self):
self.expr1 = const.store_const[0]
self.expr2 = const.store_const[1]
self.result = self.expr1 * self.expr2
return self.result
class plus(binOp):
def __init__(self, *args):
super(plus, self).__init__(*args) #inheriting the superclass constructor arguments
def eval(self):
self.expr1 = const.store_const[0] #assigning the 1st elem of list to expr1
self.expr2 = const.store_const[1] #assigning the 2nd elem of list to expr2
self.result1 = self.expr1 + self.expr2
return self.result1
#input
e1 = mult(plus(const(3), const(4)),const(5))
print e1.eval()
e2 = plus(mult(const(3), const(4)),const(5))
print e2.eval()
#output
12
7
如果您给const类添加一个,则不需要将常量存储在外部集合中方法。此外,您的eval方法将不得不递归调用eval
的左侧和右侧参数。
class expr(object): #ignore class
pass
class const(expr):
store_const = [] #list to store constants
def __init__(self, n): #initial constructor
self.n = n
def eval(self):
return self.n
class binOp(expr): #sublcass of expr
def __init__(self, expr1, expr2): #initial constructor arguments
self.expr1 = expr1
self.expr2 = expr2
class mult(binOp):
def __int__(self, *args):
super(mult, self).__init__(*args) #inheriting the superclass constructor arguments
def eval(self):
return self.expr1.eval() * self.expr2.eval()
class plus(binOp):
def __init__(self, *args):
super(plus, self).__init__(*args) #inheriting the superclass constructor arguments
def eval(self):
return self.expr1.eval() + self.expr2.eval()
#input
e1 = mult(plus(const(3), const(4)),const(5))
print e1.eval()
e2 = plus(mult(const(3), const(4)),const(5))
print e2.eval()
结果:
35
17
我试图解决在使用列表中的问题,以便常量存储在列表
我认为这是因为你怀疑你的问题:
我想这是b ecause MULT /加仅采取store_const [0] = 3和store_const [1] = 4和遗漏了store_const [2] = 5
由于这是明显的分配,而你是几乎没有,我只会给你提示:
你不需要存储常量。你可以用.n
访问它们:
>>> x = const(3)
>>> y = const(4)
>>> x.n
3
>>> y.n
4
在你eval
方法,你必须“猜测”什么是类表达式1和表达式2和呼叫.eval
,如果它是mult
或plus
和.n
如果它是一个const
。 在伪代码:
if expr1 is a const:
new_expr1 = expr1.n
else:
new_expr1 = expr1.eval()
if expr2 is a const:
new_expr2 = expr2.n
else:
new_expr2 = expr2.eval()
result = new_expr1 + new_expr2 #or * in the mult class
但你必须做的,对于2个EXPR(1和2),并有一个更好的办法:因为你可能会想,如果const
有eval
方法返回.n
,然后你不必“猜测”,并且可以做self.expr1.eval()+self.expr2.eval()
(或*
为mult
)。
self.expr1.eval()
将为.n
返回一个常数或mult/plus操作的结果。
谢谢。我明白我出错的地方。 – 2014-10-30 15:11:13
噢好吧,我现在明白了,因为我也在考虑如何将expr1和expr2直接分配给const(n)。 – 2014-10-30 15:04:54
如果我想用str方法打印常量,即打印str(e1)=(3 + 4)* 5和打印str(e2)=(3 * 4)+5,该怎么办?因为使用你的方法str(e1)= 7 * 5和str(e2)= 12 + 5 – 2014-10-30 15:45:55
每个类都应该有一个'__repr__'方法返回它的字符串表示。确保你的binOp类在参数上调用'repr',而不是'eval'。 – Kevin 2014-10-30 15:47:53