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
广告