当前位置:网站首页>函数:求方程的根
函数:求方程的根
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
边栏推荐
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- 【指针】求二维数组中最大元素的值
- 循环队列(C语言)
- 《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
- Intranet information collection of Intranet penetration (3)
- 链队实现(C语言)
- 【指针】统计一字符串在另一个字符串中出现的次数
- 《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
- [issue 18] share a Netease go experience
- Load balancing ribbon of microservices
猜你喜欢

四元数---基本概念(转载)
![[paper reproduction] cyclegan (based on pytorch framework) {unfinished}](/img/16/43d8929d1a37c1c68e959d5854e18c.jpg)
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}

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

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

Xray and Burp linked Mining

Interpretation of iterator related "itertools" module usage

Data mining - a discussion on sample imbalance in classification problems

Solutions to common problems in database development such as MySQL

Binary search tree concept

《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
随机推荐
. Net6: develop modern 3D industrial software based on WPF (2)
循环队列(C语言)
《统计学》第八版贾俊平第三章课后习题及答案总结
Record once, modify password logic vulnerability actual combat
Spot gold prices rose amid volatility, and the rise in U.S. prices is likely to become the key to the future
Network technology related topics
Xray and burp linkage mining
captcha-killer验证码识别插件
记一次api接口SQL注入实战
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
How to earn the first pot of gold in CSDN (we are all creators)
Circular queue (C language)
【指针】求二维数组中最大元素的值
【指针】求解最后留下的人
Data mining - a discussion on sample imbalance in classification problems
Which is more advantageous in short-term or long-term spot gold investment?
Load balancing ribbon of microservices
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Sentinel overall workflow
On the idea of vulnerability discovery