2016-04-14 71 views
1

對於我們的作業分配,我們被要求計算二次方程,然後以ax^2 + bx + c = 0的格式打印出來。使用條件打印二次方程

我們打印了以下條件:

  • 如果是負的,把一個減號在前面
  • 如果B或C爲負,加號應該被轉換爲一個減號
  • 如果b爲0,如果c是0不顯示該術語
  • ,不顯示該術語
  • 如果a或b是1時,不顯示它

我幾乎有它的工作,但遇到了一些呃逆。

-x^2 + x + 1.0 = 0 
2.0x^2 – 3.0x – 1.0 = 0 
x^2 – 25.0 = 0 
1.2x^2 + 2.3x + 5.6 = 0 
9.0x^2 - x + 81.0 = 0 

然而,礦井正在返回:

-x^2 + x + 1.0 = 0 
2.0x^2 - 3.0x - 1.0 = 0 
x^2 + 25.0 = 0 
1.2x^2 + 2.3x + 5.6 = 0 
9.0x^2 - 1.0x + 81.0 = 0 

有誰看到我搞亂了作爲

QuadraticEquation(a=-1,b=1,c=1) 
QuadraticEquation(a=2,b=-3,c=-1) 
QuadraticEquation(a=1,b=0,c=25) 
QuadraticEquation(a=1.2,b=2.3,c=5.6) 
QuadraticEquation(a=9.0,b=-1,c=81.0) 

以下功能上面應該返回?

from math import sqrt 


class QuadraticEquation(object): 
    def __init__(self, a, b, c): 
     self.__a = float(a) 
     if self.__a == 0.0: 
      raise ValueError("Coefficient 'a' cannot be 0 in a quadratic equation.") 
     self.__b = float(b) 
     self.__c = float(c) 

    @property 
    def a(self): 
     return self.__a 

    @property 
    def b(self): 
     return self.__b 

    @property 
    def c(self): 
     return self.__c 

    def __str__(self): 
     a = self.__a 
     b = self.__b 
     c = self.__c 

     # a 
     if a < 0: 
      a = '-x^2' 
     elif a == 1: 
      a = 'x^2' 
     else: 
      a = '%sx^2' % a 

     # b 
     if b < 0: 
      b = ' - %sx' % (b * -1) 
     elif b == 0: 
      b = '' 
     elif b == 1: 
      b = ' + x' 
     else: 
      b = ' + %sx' % b 

     # c 
     if c < 0: 
      c = ' - %s' % (c * -1) 
     elif c == 0: 
      c = '' 
     else: 
      c = ' + %s' % c 

     return a + b + c + ' = 0' 


if __name__ == '__main__': 
    equation1 = QuadraticEquation(a=-1,b=1,c=1) 
    equation2 = QuadraticEquation(a=2,b=-3,c=-1) 
    equation3 = QuadraticEquation(a=1,b=0,c=25) 
    equation4 = QuadraticEquation(a=1.2,b=2.3,c=5.6) 
    equation5 = QuadraticEquation(a=9.0,b=-1,c=81.0) 
    print(equation1) # -x^2 + x + 1.0 = 0 
    print(equation2) # 2.0x^2 – 3.0x – 1.0 = 0 
    print(equation3) # x^2 – 25.0 = 0 
    print(equation4) # 1.2x^2 + 2.3x + 5.6 = 0 
    print(equation5) # 9.0x^2 - x + 81.0 = 0 
+1

由於'25'不是負數,看起來像'QuadraticEquation(a = 1,b = 0,c = 25)'正確返回'x^2 + 25.0 = 0',所以唯一的問題是'QuadraticEquation a = 9.0,b = -1,c = 81.0)'正在預先考慮'1.0',對嗎? – spiffman

+0

你的輸出是一樣的嗎? 1.0x和x是相同的。 – TheLazyScripter

+0

@spiffman這是正確的 – jape

回答

2

所以你的問題輸出「 - 1.0x」而不是「-x」? 你應該改正這部分。您當前的代碼適用於b = + 1,但不適用於b = -1,因爲這種情況在「b < 0」條件下處理。

我認爲更好的解決方案是使用新的變量輸出。試試這個:

# a 
    a_out = '' 
    if a != 0: 
     if a < 0: 
      a_out += '-' 
     if abs(b) != 1: 
      a_out += '%.1fx^2' % abs(a) 
     else: 
      a_out += 'x^2' 

    # b 
    b_out = '' 
    if b != 0: 
     if b < 0: 
      b_out += ' - ' 
     else: 
      b_out += ' + ' 
     if abs(b) != 1: 
      b_out += '%.1fx' % abs(b) 
     else: 
      b_out += 'x' 

    # c 
    c_out = '' 
    if c != 0: 
     if c < 0: 
      c_out = ' - %.1f' % (-c) 
     else: 
      c_out = ' + %.1f' % c 

    return a_out + b_out + c_out + ' = 0' 
1

夫婦的問題,因爲它是我的功課不會給你答案,但指出問題:)

# a 
    if a < 0: 
     a = '-x^2' 
    elif a == 1: 
     a = 'x^2' 
    else: 
     a = '%sx^2' % a 

這裏,如果a任何負數 ,第一個表達式將是-x^2。這意味着即使你有一個= -20,結果將是-x^2

這裏有三種情況,a是負數,a是1,否則。但實際上有4種情況:a是負數且不等於-1,a是負數且等於-1,a是正數且等於1,或a是正數。