当前位置:网站首页>求方程ax^2+bx+c=0的根(C语言)
求方程ax^2+bx+c=0的根(C语言)
2022-07-07 08:12:00 【璞玉牧之】
一、题目
求方程ax2+bx+c=0的根,用3个函数分别求当:bx2-4ac大于0、等于0和小于0时的根并输出结果。从主函数输入a,b,c的值。
二、代码及结果
1.代码
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float x1,x2,t,p,q;
int main()
{
void Greater_than_Zero(float a,float b);
void Equal_to_Zero(float a,float b);
void Smaller_than_zero(float a,float b);
float a,b,c;
printf("请输入a,b,c的值:\n");
scanf("%f,%f,%f",&a,&b,&c);
printf("解方程:%4.2f*x*x+%4.2f*x+%4.2f=0\n",a,b,c);
t=b*b-4*a*c;
if(t>0)
{
Greater_than_Zero(a,b);
printf("x1=%f\nx2=%f\n",x1,x2);
}
else if(t==0)
{
Equal_to_Zero(a,b);
printf("x1=x2=%f\n",x1);
}
else
{
Smaller_than_zero(a,b);
printf("x1=%f+%fi\nx2=%f-%fi\n",p,q,p,q);
}
return 0;
}
void Greater_than_Zero(float a,float b)
{
x1=(-b+sqrt(t))/(2*a);
x2=(-b+sqrt(t))/(2*a);
}
void Equal_to_Zero(float a,float b)
{
x1=x2=(-b)/(2*a);
}
void Smaller_than_zero(float a,float b)
{
p=-b/(2*a);
q=sqrt(-t)/(2*a);
}
2.运行结果
(1)两个不等的实根
(2)两个相等的实根
(3)两个共轭的复根
边栏推荐
- [higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
- SQLyog数据库怎么取消自动保存更改
- 【acwing】789. Range of numbers (binary basis)
- Guid主键
- XML配置文件解析与建模
- @Transcation的配置,使用,原理注意事项:
- 虚数j的物理意义
- JMeter loop controller and CSV data file settings are used together
- 每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
- Win10 installation vs2015
猜你喜欢
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
[ORM framework]
【二开】【JeecgBoot】修改分页参数
【剑指Offer】42. 栈的压入、弹出序列
Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
高数_第1章空间解析几何与向量代数_向量的数量积
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
JMeter loop controller and CSV data file settings are used together
Serial communication relay Modbus communication host computer debugging software tool project development case
Pdf document signature Guide
随机推荐
【ORM框架】
SQLyog数据库怎么取消自动保存更改
mysql插入数据创建触发器填充uuid字段值
Study summary of postgraduate entrance examination in July
Web3.0 series distributed storage IPFs
Guide de signature du Code Appx
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
ISP、IAP、ICP、JTAG、SWD的编程特点
【二开】【JeecgBoot】修改分页参数
The landing practice of ByteDance kitex in SEMA e-commerce scene
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
嵌入式背景知识-芯片
Vs code specifies the extension installation location
STM32基础知识—内存映射
IPv4套接字地址结构
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Leetcode exercise - 113 Path sum II
根据设备信息进行页面跳转至移动端页面或者PC端页面
Postman interface test V
EasyExcel读取写入简单使用