当前位置:网站首页>函数:求方程的根
函数:求方程的根
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
边栏推荐
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- 攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
- DVWA (5th week)
- 《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
- “人生若只如初见”——RISC-V
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- Constants, variables, and operators of SystemVerilog usage
- How to earn the first pot of gold in CSDN (we are all creators)
- Ucos-iii learning records (11) - task management
- 循环队列(C语言)
猜你喜欢

Record an API interface SQL injection practice

Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class

Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class

Circular queue (C language)

List and data frame of R language experiment III

Database monitoring SQL execution

记一次edu,SQL注入实战

Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class

《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案

Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
随机推荐
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques
JDBC事务、批处理以及连接池(超详细)
MSF generate payload Encyclopedia
搭建域环境(win)
Detailed explanation of network foundation
Detailed explanation of network foundation routing
记一次edu,SQL注入实战
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
Constants, variables, and operators of SystemVerilog usage
Only 40% of the articles are original? Here comes the modification method
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
xray与burp联动 挖掘
SQL注入
XSS unexpected event
XSS之冷门事件
浅谈漏洞发现思路
JDBC read this article is enough
Intranet information collection of Intranet penetration (3)