用函数实现方程函数解题

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

线性方程的一般形式为: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

相关推荐
voipmaker3 分钟前
AI 赋能安防系统演进:从硬件单品到场景化融合调度架构
人工智能·ai·架构
做cv的小昊8 分钟前
【World Model】π0.5:a Vision-Language-Action Model with Open-World Generalization
人工智能·算法·机器学习·大模型·多模态·vla·世界模型
光锥智能16 分钟前
具身智能数据,众包时代来了?
人工智能
大嘴皮猴儿18 分钟前
2026年9月行业洞察:适合亚马逊跨境卖家的免费自动图片翻译平台
大数据·人工智能·跨境电商ai跨境翻译网站哪个好·ai图片翻译网站推荐·ai图片翻译工具哪个好
莪_幻尘19 分钟前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
2601_9521963619 分钟前
生物统计学专业校招面试,生存分析和缺失数据处理怎么复习?
人工智能
لا معنى له35 分钟前
World-In-World: World Models in a Closed-Loop World
算法
秦先生在广东42 分钟前
Ego-Lite:代码驱动与空间隔离重塑 AI 代理的浏览器交互范式
人工智能
天国梦43 分钟前
2026年北京机器狗选型指南:从痛点、技术到实战的完整评测
大数据·人工智能
秦先生在广东1 小时前
Orca:并行 AI 代理管理的开源 ADE 深度解析
人工智能