Kotlin程式查詢二次方程的所有根


在本文中,我們將瞭解如何在Kotlin中計算二次方程的根。二次方程是二次代數表示式,換句話說,它有兩個結果,即實數和虛數。

以下是相同內容的演示

假設我們的輸入是 -

a = 1, b = 2, c = 3

期望的輸出將是 -

The roots of the quadratic equation are
root1 = -1.00+1.41i
root2 = -1.00-1.41i

演算法

  • 步驟 1 - 開始

  • 步驟 2 - 宣告六個值:inputA、inputB、inputC、root1、root2、myResult

  • 步驟 3 - 定義值

  • 步驟 4 - 計算 ( inputB * inputB - 4.0 * inputA * inputC) 並將結果賦值給myDeterminant

  • 步驟 5 - 在for迴圈中,檢查myDeterminant變數的值是否大於0,如果為真,則使用求根公式 root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) 和 root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) 查詢值,並將其賦值給變數。

  • 步驟 6 - 如果myDeterminant的值等於0,則將(-inputB / (2 * inputA))賦值給兩個根的值。

  • 步驟 7 - 如果myDeterminant的值小於0,則計算 val realPart = -inputB / (2 * inputA)

val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA)
  • 步驟 8 - 顯示結果

  • 步驟 9 - 停止

示例 1

在此示例中,我們將找到二次方程的所有根。首先,宣告輸入變數 -

val inputA = 1.0
val inputB = 2.0
val inputC = 3.0

現在,設定根和結果的變數 -

val root1: Double
val root2: Double
val myResult: String

使用公式計算判別式 -

b² - 4ac

應用上述公式獲取判別式 -

val myDeterminant = inputB * inputB - 4.0 * inputA * inputC

使用if…elseif…else顯示二次方程的所有根 -

if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) }

現在讓我們看看完整的示例 -

fun main() { val inputA = 1.0 val inputB = 2.0 val inputC = 3.0 println("The input values are defined as $inputA, $inputB and $inputC ") val root1: Double val root2: Double val myResult: String val myDeterminant = inputB * inputB - 4.0 * inputA * inputC println("The roots of the quadratic equation are: ") if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) } println(myResult) }

輸出

The input values are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are:
root1 = -1.00+1.41i and root2 = -1.00-1.41i

示例 2

在此示例中,我們將使用自定義函式查詢二次方程的所有根 -

fun main() { val inputA = 1.0 val inputB = 2.0 val inputC = 3.0 println("The input values are defined as $inputA, $inputB and $inputC ") getRoots(inputA, inputB, inputC) } fun getRoots(inputA: Double, inputB: Double, inputC: Double) { val root1: Double val root2: Double val myResult: String val myDeterminant = inputB * inputB - 4.0 * inputA * inputC println("The roots of the quadratic equation are: ") if (myDeterminant > 0) { root1 = (-inputB + Math.sqrt(myDeterminant)) / (2 * inputA) root2 = (-inputB - Math.sqrt(myDeterminant)) / (2 * inputA) myResult = "root1 = %.2f and root2 = %.2f".format(root1, root2) } else if (myDeterminant == 0.0) { root2 = -inputB / (2 * inputA) root1 = root2 myResult = "root1 = root2 = %.2f;".format(root1) } else { val realPart = -inputB / (2 * inputA) val imaginaryPart = Math.sqrt(-myDeterminant) / (2 * inputA) myResult = "root1 = %.2f+%.2fi and root2 = %.2f-%.2fi".format(realPart, imaginaryPart, realPart, imaginaryPart) } println(myResult) }

輸出

The input values are defined as 1.0, 2.0 and 3.0
The roots of the quadratic equation are:
root1 = -1.00+1.41i and root2 = -1.00-1.41i

更新於: 2022年10月13日

684 次檢視

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告