求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
时间: 2023-06-05 17:47:12 浏览: 409
当 b^2 - 4ac > 0 时,方程的根为:
x1 = (-b + sqrt(b^2 - 4ac)) / 2a
x2 = (-b - sqrt(b^2 - 4ac)) / 2a
当 b^2 - 4ac = 0 时,方程的根为:
x1 = x2 = -b / 2a
当 b^2 - 4ac < 0 时,方程的根为:
x1 = (-b + sqrt(b^2 - 4ac)) / 2a + i * sqrt(abs(b^2 - 4ac)) / 2a
x2 = (-b - sqrt(b^2 - 4ac)) / 2a + i * sqrt(abs(b^2 - 4ac)) / 2a
在主函数中,可以使用输入函数获取 a、b、c 的值,然后根据 b^2 - 4ac 的值判断使用哪个函数求根,最后输出结果。
相关问题
用c++代码写求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
以下是C++代码实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
void roots_positive(double a, double b, double c) {
double root1 = (-b + sqrt(b*b - 4*a*c)) / (2*a);
double root2 = (-b - sqrt(b*b - 4*a*c)) / (2*a);
cout << "The roots are real and different." << endl;
cout << "Root 1: " << root1 << endl;
cout << "Root 2: " << root2 << endl;
}
void roots_zero(double a, double b, double c) {
double root = -b / (2*a);
cout << "The roots are real and equal." << endl;
cout << "Root: " << root << endl;
}
void roots_negative(double a, double b, double c) {
double realPart = -b / (2*a);
double imagPart = sqrt(-b*b + 4*a*c) / (2*a);
cout << "The roots are complex and different." << endl;
cout << "Root 1: " << realPart << " + " << imagPart << "i" << endl;
cout << "Root 2: " << realPart << " - " << imagPart << "i" << endl;
}
int main() {
double a, b, c;
cout << "Enter the coefficients of the quadratic equation: " << endl;
cin >> a >> b >> c;
double d = b*b - 4*a*c;
if (d > 0) {
roots_positive(a, b, c);
} else if (d == 0) {
roots_zero(a, b, c);
} else {
roots_negative(a, b, c);
}
return 0;
}
```
在这个程序中,我们定义了三个函数,分别计算当 $b^2-4ac$ 大于0、等于0、和小于0时的根,并输出结果。主函数中,我们从用户输入获取 $a$、$b$、$c$ 的值,计算 $b^2-4ac$,并根据 $b^2-4ac$ 的值调用不同的函数来计算和输出根。
求方程ax^2+bx+c=0的根,用三个函数分别求当:b^2-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a,b,c的值
以下是代码实现:
```python
import math
def solve_quadratic_greater(a, b, c):
delta = b**2 - 4*a*c
x1 = (-b + math.sqrt(delta)) / (2*a)
x2 = (-b - math.sqrt(delta)) / (2*a)
return x1, x2
def solve_quadratic_equal(a, b, c):
x = -b / (2*a)
return x
def solve_quadratic_less(a, b, c):
real = -b / (2*a)
imaginary = math.sqrt(abs(b**2 - 4*a*c)) / (2*a)
x1 = complex(real, imaginary)
x2 = complex(real, -imaginary)
return x1, x2
a = float(input("请输入a的值:"))
b = float(input("请输入b的值:"))
c = float(input("请输入c的值:"))
if b**2 - 4*a*c > 0:
x1, x2 = solve_quadratic_greater(a, b, c)
print("方程的两个实根分别为:", x1, x2)
elif b**2 - 4*a*c == 0:
x = solve_quadratic_equal(a, b, c)
print("方程的唯一实根为:", x)
else:
x1, x2 = solve_quadratic_less(a, b, c)
print("方程的两个复根分别为:", x1, x2)
```
输入a、b、c的值后,程序会根据$b^2-4ac$的值来选择不同的函数求解,输出对应的结果。
例如,当输入$a=1,b=2,c=1$时,$b^2-4ac=0$,程序输出:
```
方程的唯一实根为: -1.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)