2014-10-30 23 views
-1

我想寫一個可以解決算術運算的代碼。這些類multplus是爲了解決類似的問題:如何在類屬性上使用算術運算?

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] = 3store_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 

回答

1

如果您給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 
+0

噢好吧,我現在明白了,因爲我也在考慮如何將expr1和expr2直接分配給const(n)。 – 2014-10-30 15:04:54

+0

如果我想用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

+0

每個類都應該有一個'__repr__'方法返回它的字符串表示。確保你的binOp類在參數上調用'repr',而不是'eval'。 – Kevin 2014-10-30 15:47:53

0

我試圖解決在使用列表中的問題,以便常量存儲在列表

我認爲這是因爲你懷疑你的問題:

我想這是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,如果它是multplus.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),並有一個更好的辦法:因爲你可能會想,如果consteval方法返回.n,然後你不必「猜測」,並且可以做self.expr1.eval()+self.expr2.eval()(或*mult)。

self.expr1.eval()將爲.n返回一個常數或mult/plus操作的結果。

+0

謝謝。我明白我出錯的地方。 – 2014-10-30 15:11:13