用函数实现方程函数解题

一、线性方程(一次方程)​

线性方程的一般形式为:ax + b = 0a ≠ 0)。

求解目标是找到 x 的值:x = -b / a

函数设计思路
  • 输入参数 :系数 abdouble 类型,支持浮点数)。
  • 输出结果 :方程的解 xdouble 类型)。
  • 错误处理 :若 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 = 0a ≠ 0)。

求解目标是找到实根或复根,核心是计算判别式 Δ = b² - 4ac

函数设计思路
  • 输入参数 :系数 abcdouble 类型)。
  • 输出结果
    • Δ > 0:两个不同实根 x1x2
    • Δ = 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

相关推荐
mwq301235 分钟前
从傅里叶时钟到混合尺度:解构 RoPE 位置编码的演进之路
人工智能
高工智能汽车9 分钟前
“融资热潮”来临!商用车自动驾驶拐点已至?
人工智能·机器学习·自动驾驶
CoovallyAIHub18 分钟前
终结AI偏见!Sony AI发布Nature论文与FHIBE数据集,重塑公平性评估基准
深度学习·算法·计算机视觉
Mintopia18 分钟前
🧠 自监督学习在 WebAIGC 中的技术突破与应用前景
前端·人工智能·aigc
7澄121 分钟前
深入解析 LeetCode 1572:矩阵对角线元素的和 —— 从问题本质到高效实现
java·算法·leetcode·矩阵·intellij-idea
Mintopia21 分钟前
🧭 传统 Web 开发最好的 AI 助手框架排行榜(2025版)
前端·人工智能·aigc
ALex_zry23 分钟前
c20 字符串处理优化可选方案
算法
Juchecar25 分钟前
“提升效率”论正确吗?上限及上限之后
人工智能
阳光明媚sunny25 分钟前
分糖果算法题
java·算法
卡提西亚28 分钟前
一本通网站1125题:矩阵乘法
c++·算法·矩阵·编程题·一本通