求方程ax2+bx+c=0的根,用三个函数分别求当b2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
时间: 2023-05-31 13:20:39 浏览: 160
### 回答1:
可以使用以下三个函数来求解二次方程ax2+bx+c=0的根:
1. 当b2-4ac大于0时,根为实数,可以使用求根公式:x1=(-b+√(b2-4ac))/2a,x2=(-b-√(b2-4ac))/2a
2. 当b2-4ac等于0时,根为重根,可以使用求根公式:x1=x2=-b/2a
3. 当b2-4ac小于0时,根为虚数,可以使用公式:x1=(-b+√(4ac-b2)i)/2a,x2=(-b-√(4ac-b2)i)/2a
以下是代码实现:
#include <iostream>
#include <cmath>
using namespace std;
void solve1(double a, double b, double c) {
double delta = b * b - 4 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
cout << "x1 = " << x1 << ", x2 = " << x2 << endl;
} else if (delta == 0) {
double x = -b / (2 * a);
cout << "x1 = x2 = " << x << endl;
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(4 * a * c - b * b) / (2 * a);
cout << "x1 = " << realPart << "+" << imaginaryPart << "i, x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
}
void solve2(double a, double b, double c) {
double delta = b * b - 4 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
cout << "x1 = " << x1 << ", x2 = " << x2 << endl;
} else if (delta == 0) {
double x = -b / (2 * a);
cout << "x1 = x2 = " << x << endl;
} else {
cout << "方程无实数根" << endl;
}
}
void solve3(double a, double b, double c) {
double delta = b * b - 4 * a * c;
if (delta > 0) {
double x1 = (-b + sqrt(delta)) / (2 * a);
double x2 = (-b - sqrt(delta)) / (2 * a);
cout << "x1 = " << x1 << ", x2 = " << x2 << endl;
} else if (delta == 0) {
double x = -b / (2 * a);
cout << "x1 = x2 = " << x << endl;
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(4 * a * c - b * b) / (2 * a);
cout << "x1 = " << realPart << "+" << imaginaryPart << "i, x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
}
int main() {
double a, b, c;
cout << "请输入a、b、c的值:";
cin >> a >> b >> c;
cout << "b2-4ac大于0时的根为:";
solve1(a, b, c);
cout << "b2-4ac等于0时的根为:";
solve2(a, b, c);
cout << "b2-4ac小于0时的根为:";
solve3(a, b, c);
return 0;
}
### 回答2:
该方程的根可用下列公式求得:
当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(4ac - b^2)i) / 2a
x2 = (-b - sqrt(4ac - b^2)i) / 2a
因此我们可以编写三个函数来分别求解这三种情况下的根,并输出结果:
1. 当b^2 - 4ac > 0 时,
```C++
void roots_1(float a, float b, float c)
{
float x1, x2;
x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
cout << "方程的两个根为:x1= " << x1 << ", x2= " << x2 << endl;
}
```
2. 当b^2 - 4ac = 0 时,
```C++
void roots_2(float a, float b, float c)
{
float x;
x = (-b) / (2 * a);
cout << "方程的两个根为:x1= x2= " << x << endl;
}
```
3. 当b^2 - 4ac < 0 时,
```C++
void roots_3(float a, float b, float c)
{
float x1_real, x2_real, x_imag;
x1_real = x2_real = (-b) / (2 * a);
x_imag = sqrt(4 * a * c - b * b) / (2 * a);
cout << "方程的两个根为:x1= " << x1_real << "+" << x_imag << "i, x2= " << x2_real << "-" << x_imag << "i" << endl;
}
```
在主函数中,我们可以读入a、b、c的值,并根据b^2 - 4ac的大小,调用相应的函数获得方程的根:
```C++
int main()
{
float a, b, c;
cout << "请输入二次方程 ax^2 + bx + c = 0 的系数 a、b、c\n";
cin >> a >> b >> c;
if (b * b - 4 * a * c > 0)
{
roots_1(a, b, c);
}
else if (b * b - 4 * a * c == 0)
{
roots_2(a, b, c);
}
else
{
roots_3(a, b, c);
}
return 0;
}
```
运行程序,输入a、b、c的值,就能得到方程的根。这样,我们就成功地编写了一个求解二次方程的程序。
### 回答3:
题目要求我们用三个函数分别求解该方程的根,对应于不同情况,其中:
1. 当b2-4ac大于0时,该方程有两个实根;
2. 当b2-4ac等于0时,该方程有一个实根;
3. 当b2-4ac小于0时,该方程有两个虚根;
下面就依次介绍如何用三个函数实现求解:
1. 当b2-4ac大于0时,该方程有两个实根,我们可以使用求根公式来求解,即
根1 = (-b + sqrt(b2-4ac)) / 2a
根2 = (-b - sqrt(b2-4ac)) / 2a
在程序中,我们可以写出如下的求解函数:
```python
import math
def solve_equation1(a, b, c):
delta = b * b - 4 * a * c
if delta < 0:
return None
x1 = (-b + math.sqrt(delta)) / (2 * a)
x2 = (-b - math.sqrt(delta)) / (2 * a)
return x1, x2
```
其中,delta是b2-4ac,如果delta小于0,那么方程无解,这时返回None。
2. 当b2-4ac等于0时,该方程有一个实根,我们也可以使用求根公式来求解,但是这时delta等于0,我们可以对求根公式进行简化,即
根1 = 根2 = -b / 2a
在程序中,我们可以写出如下的求解函数:
```python
def solve_equation2(a, b, c):
delta = b * b - 4 * a * c
if delta == 0:
x = -b / (2 * a)
return x
else:
return None
```
当delta等于0时,我们直接返回根1即可,否则返回None。
3. 当b2-4ac小于0时,该方程有两个虚根,我们可以使用复数来表示根,即
根1 = (-b + sqrt(delta)) / 2a + j * sqrt(-delta) / 2a
根2 = (-b - sqrt(delta)) / 2a - j * sqrt(-delta) / 2a
其中,j为虚数单位,sqrt(-delta)为复数的开平方,表示为一个实数加上一个虚数。
在程序中,我们可以写出如下的求解函数:
```python
def solve_equation3(a, b, c):
delta = b * b - 4 * a * c
if delta < 0:
real = -b / (2 * a)
imag = math.sqrt(-delta) / (2 * a)
x1 = complex(real, imag)
x2 = complex(real, -imag)
return x1, x2
else:
return None
```
当delta小于0时,我们求出实部和虚部,然后通过complex函数构建一个复数,最后返回根1和根2。
最后,我们可以在主函数中读入a、b、c的值,然后分别调用这三个函数,输出结果即可。
```python
if __name__ == "__main__":
a = float(input("请输入a的值:"))
b = float(input("请输入b的值:"))
c = float(input("请输入c的值:"))
roots = solve_equation1(a, b, c)
if roots:
print("方程的两个实根为:", roots[0], roots[1])
else:
roots = solve_equation2(a, b, c)
if roots:
print("方程的一个实根为:", roots)
else:
roots = solve_equation3(a, b, c)
if roots:
print("方程的两个虚根为:", roots[0], roots[1])
else:
print("方程无解!")
```
完整程序如下:
阅读全文