c语言用指针求一元二次方程的根
时间: 2024-06-13 14:09:24 浏览: 15
以下是用指针求解一元二次方程的根的C语言代码:
```c
#include <stdio.h>
#include <math.h>
void solve(double a, double b, double c, double *x1, double *x2);
int main() {
double a, b, c, x1, x2;
printf("请输入一元二次方程的系数a, b, c:\n");
scanf("%lf %lf %lf", &a, &b, &c);
solve(a, b, c, &x1, &x2);
printf("方程的根为:x1 = %lf, x2 = %lf\n", x1, x2);
return 0;
}
void solve(double a, double b, double c, double *x1, double *x2) {
double delta = b * b - 4 * a * c;
if (delta >= 0) {
*x1 = (-b + sqrt(delta)) / (2 * a);
*x2 = (-b - sqrt(delta)) / (2 * a);
} else {
printf("方程无实数解!\n");
}
}
```
相关问题
c语言中使用函数求一元二次方程根
以下是使用函数求解一元二次方程根的C语言代码,其中包括了三个函数分别用于计算当b^2-4ac大于0、等于0、和小于0时的根,并在主函数中进行了有效性检测和菜单的实现:
```c
#include <stdio.h>
#include <math.h>
// 计算当b^2-4ac大于0时的根
void greater_than_zero(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);
printf("x1 = %.2f, x2 = %.2f\n", x1, x2);
}
// 计算当b^2-4ac等于0时的根
void equal_to_zero(float a, float b, float c) {
float x;
x = -b / (2 * a);
printf("x = %.2f\n", x);
}
// 计算当b^2-4ac小于0时的根
void less_than_zero(float a, float b, float c) {
float realPart, imagPart;
realPart = -b / (2 * a);
imagPart = sqrt(-b * b + 4 * a * c) / (2 * a);
printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n", realPart, imagPart, realPart, imagPart);
}
int main() {
float a, b, c;
int choice;
do {
printf("请输入一元二次方程的三个系数a、b、c:\n");
scanf("%f%f%f", &a, &b, &c);
if (b * b - 4 * a * c > 0) {
greater_than_zero(a, b, c);
} else if (b * b - 4 * a * c == 0) {
equal_to_zero(a, b, c);
} else {
less_than_zero(a, b, c);
}
printf("是否继续?(1表示继续,0表示退出)\n");
scanf("%d", &choice);
} while (choice == 1);
return 0;
}
```
用C语言编码:求一元二次方程的实数根
```c
#include <stdio.h>
#include <math.h>
int main()
{
float a, b, c, x1, x2, delta;
printf("请输入一元二次方程的系数:\n");
printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);
delta = b * b - 4 * a * c;
if (delta > 0) {
x1 = (-b + sqrt(delta)) / (2 * a);
x2 = (-b - sqrt(delta)) / (2 * a);
printf("方程有两个实根:x1 = %.2f,x2 = %.2f\n", x1, x2);
} else if (delta == 0) {
x1 = -b / (2 * a);
printf("方程有一个实根:x = %.2f\n", x1);
} else {
printf("方程无实根!\n");
}
return 0;
}
```
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)