2012-07-09 216 views
-1

問題:我有一個解決二次方程的程序。該方案只提供真正的解決方案。我如何執行程序的質量測試?你需要問我一些額外的輸入參數嗎?二次方程

+2

歡迎來到Stack Overflow。這是不是很清楚你在這裏問什麼。如果你花一些時間使其更清楚明確,你的問題將有更好的回答機會。 – FatalError 2012-07-09 03:50:57

回答

0

創建測試用例,並根據測試用例中的預期結果(外部計算)檢查程序的結果。

測試用例可以覆蓋幾個普通情況,以及特殊情況,例如係數爲0時,或者判別式爲< 0 = 0,接近0.當您比較結果時,請確保處理比較正確(因爲結果是浮點數)。

0
# "quadratic-rb.rb" Code by RRB, dated April 2014. email [email protected] 


class Quadratic 

    def input 
    print "Enter the value of a: " 
    $a = gets.to_f 

    print "Enter the value of b: " 
    $b = gets.to_f 

    print "Enter the value of c: " 
    $c = gets.to_f 
end 


def announcement #Method to display Equation 
puts "The formula is " + $a.to_s + "x^2 + " + $b.to_s + "x + " + $c.to_s + "=0" 
end 


def result #Method to solve the equation and display answer 

    if ($b**2-4*$a*$c)>0 
    x1=(((Math.sqrt($b**2-4*$a*$c))-($b))/(2*$a)) 
    x2=(-(((Math.sqrt($b**2-4*$a*$c))-($b))/(2*$a))) 
    puts "The values of x1 and x2 are " +x1.to_s + " and " + x2.to_s 

else 
    puts "x1 and x2 are imaginary numbers" 
    end 

end 


Quadratic_solver = Quadratic.new 
    Quadratic_solver.input 
    Quadratic_solver.announcement 
    Quadratic_solver.result 
end