一、线性方程(一次方程)
线性方程的一般形式为:ax + b = 0
(a ≠ 0
)。
求解目标是找到 x
的值:x = -b / a
。
函数设计思路
- 输入参数 :系数
a
和b
(double
类型,支持浮点数)。 - 输出结果 :方程的解
x
(double
类型)。 - 错误处理 :若
a = 0
,方程无解或有无穷多解,需返回错误提示。
代码实现
cs
#include <stdio.h>
// 函数声明:求解线性方程 ax + b = 0
double solve_linear_equation(double a, double b, int *error);
int main() {
double a, b, x;
int error;
// 示例 1:正常情况(a ≠ 0)
a = 2.0;
b = 4.0;
x = solve_linear_equation(a, b, &error);
if (error == 0) {
printf("方程 %.2fx + %.2f = 0 的解为 x = %.2f\n", a, b, x);
} else {
printf("方程无解或参数错误!\n");
}
// 示例 2:a = 0(无解)
a = 0.0;
b = 5.0;
x = solve_linear_equation(a, b, &error);
if (error == 0) {
printf("方程 %.2fx + %.2f = 0 的解为 x = %.2f\n", a, b, x);
} else {
printf("方程无解或参数错误!\n");
}
return 0;
}
// 函数定义:求解线性方程 ax + b = 0
double solve_linear_equation(double a, double b, int *error) {
*error = 0; // 初始化错误码为 0(无错误)
if (a == 0) {
*error = 1; // 错误码 1:a 为 0,方程无解
return 0; // 返回任意值(无意义)
}
return -b / a; // 解为 x = -b/a
}
输出结果
方程 2.00x + 4.00 = 0 的解为 x = -2.00
方程无解或参数错误!
二、二次方程
二次方程的一般形式为:ax² + bx + c = 0
(a ≠ 0
)。
求解目标是找到实根或复根,核心是计算判别式 Δ = b² - 4ac
。
函数设计思路
- 输入参数 :系数
a
、b
、c
(double
类型)。 - 输出结果 :
- 若
Δ > 0
:两个不同实根x1
和x2
。 - 若
Δ = 0
:一个实根(重根)x
。 - 若
Δ < 0
:两个共轭复根(实部real
,虚部imag
)。
- 若
- 错误处理 :若
a = 0
,方程退化为线性方程,需提示错误。
代码实现(使用结构体返回结果)
cs
#include <stdio.h>
#include <math.h> // 用于 sqrt 函数
// 定义结构体存储二次方程的根
typedef struct {
int type; // 根的类型:0=无实根(复根),1=重根,2=两个不同实根
double x1; // 实根 1(或复根实部)
double x2; // 实根 2(或复根虚部,仅当 type=0 时有效)
} QuadraticRoots;
// 函数声明:求解二次方程 ax² + bx + c = 0
QuadraticRoots solve_quadratic_equation(double a, double b, double c, int *error);
int main() {
double a, b, c;
int error;
QuadraticRoots roots;
// 示例 1:两个不同实根(Δ > 0)
a = 1.0;
b = -3.0;
c = 2.0;
roots = solve_quadratic_equation(a, b, c, &error);
if (error == 0) {
if (roots.type == 2) {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的根为 x1=%.2f, x2=%.2f\n",
a, b, c, roots.x1, roots.x2);
} else if (roots.type == 1) {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的重根为 x=%.2f\n",
a, b, c, roots.x1);
} else {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的复根为 %.2f ± %.2fi\n",
a, b, c, roots.x1, roots.x2);
}
} else {
printf("方程参数错误!\n");
}
// 示例 2:复根(Δ < 0)
a = 1.0;
b = 2.0;
c = 5.0;
roots = solve_quadratic_equation(a, b, c, &error);
if (error == 0) {
if (roots.type == 2) {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的根为 x1=%.2f, x2=%.2f\n",
a, b, c, roots.x1, roots.x2);
} else if (roots.type == 1) {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的重根为 x=%.2f\n",
a, b, c, roots.x1);
} else {
printf("方程 %.2fx² + %.2fx + %.2f = 0 的复根为 %.2f ± %.2fi\n",
a, b, c, roots.x1, roots.x2);
}
} else {
printf("方程参数错误!\n");
}
return 0;
}
// 函数定义:求解二次方程 ax² + bx + c = 0
QuadraticRoots solve_quadratic_equation(double a, double b, double c, int *error) {
QuadraticRoots result;
*error = 0; // 初始化错误码为 0(无错误)
if (a == 0) {
*error = 1; // 错误码 1:a 为 0,方程退化为线性方程
return result;
}
double delta = b * b - 4 * a * c; // 计算判别式 Δ
if (delta > 0) {
// 两个不同实根
result.type = 2;
result.x1 = (-b + sqrt(delta)) / (2 * a);
result.x2 = (-b - sqrt(delta)) / (2 * a);
} else if (delta == 0) {
// 重根
result.type = 1;
result.x1 = -b / (2 * a);
result.x2 = result.x1; // 虚部无意义,设为实部值
} else {
// 复根(Δ < 0)
result.type = 0;
result.x1 = -b / (2 * a); // 实部
result.x2 = sqrt(-delta) / (2 * a); // 虚部(取绝对值)
}
return result;
}
输出结果
方程 1.00x² + -3.00x + 2.00 = 0 的根为 x1=2.00, x2=1.00
方程 1.00x² + 2.00x + 5.00 = 0 的复根为 -1.00 ± 2.00i