当前位置:网站首页>函数:求方程的根
函数:求方程的根
2022-07-06 09:24:00 【|光|】
要求
编写程序,求方程ax2+bx+c=0的根,用三个函数分别求b2-4ac大于0,等于0和小于0时的根。
代码
#include<stdio.h>
#include<math.h>
#define EPSILON 1E-6
//计算并输出两个不等实根
void fun1(double a,double b,double c)
{
double x1,x2;
// 请在下面添加代码
// ********** Begin **********
int s = b*b-4*a*c;
if(s>0)
{
x1 = ((0-b)+sqrt(s))/(2*a);
x2 = ((0-b)-sqrt(s))/(2*a);
}
// ********** End **********
printf("方程有两个不等实根:%.2f,%.2f\n",x1,x2);
}
//计算并输出两个相等实根
void fun2(double a,double b,double c)
{
double x1,x2;
// 请在下面添加代码
// ********** Begin **********
int s = b*b-4*a*c;
if(s==0)
{
x1 = (0-b)/(2*a);
x2 = (0-b)/(2*a);
}
// ********** End **********
printf("方程有两个相等实根:%.2lf,%.2lf\n",x1,x2);
}
//计算并输出两个共轭复根
void fun3(double a,double b,double c)
{
double p,q;
// 请在下面添加代码
// ********** Begin **********
int s = b*b-4*a*c;
if(s<0)
{
p = (0-b)/(2*a);
q = sqrt(0-s)/(2*a);
}
// ********** End **********
printf("方程有两个共轭复根:%.2lf+%.2lfi,%.2lf-%.2lfi\n",p,q,p,q);
}
//计算方程的根
void fun(double a,double b,double c)
{
double delta=b*b-4*a*c;
if(fabs(delta)>EPSILON)
{
if(delta>0)
fun1(a,b,c);
else
fun3(a,b,c);
}
else
fun2(a,b,c);
}
测试
测试输入
3 6 8
输出
方程有两个共轭复根:-1.00+1.29i,-1.00-1.29i
边栏推荐
- [paper reproduction] cyclegan (based on pytorch framework) {unfinished}
- 《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
- Sentinel overall workflow
- Constants, variables, and operators of SystemVerilog usage
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- 网络层—简单的arp断网
- Network layer - simple ARP disconnection
- 循环队列(C语言)
- Intranet information collection of Intranet penetration (I)
- A complete collection of papers on text recognition
猜你喜欢

xray與burp聯動 挖掘

《统计学》第八版贾俊平第三章课后习题及答案总结

Library management system

Solutions to common problems in database development such as MySQL

Detailed explanation of network foundation routing

内网渗透之内网信息收集(四)

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案

网络基础详解

Database monitoring SQL execution

Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
随机推荐
Detailed explanation of network foundation
Intel oneapi - opening a new era of heterogeneity
Detailed explanation of three ways of HTTP caching
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
浅谈漏洞发现思路
ES全文索引
Web vulnerability - File Inclusion Vulnerability of file operation
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
网络层—简单的arp断网
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
Intranet information collection of Intranet penetration (2)
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
JDBC看这篇就够了
2022华中杯数学建模思路
[issue 18] share a Netease go experience
captcha-killer验证码识别插件
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
“人生若只如初见”——RISC-V
Hackmyvm target series (3) -visions
《统计学》第八版贾俊平第五章概率与概率分布