当前位置:网站首页>函数:求方程的根
函数:求方程的根
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
边栏推荐
- XSS之冷门事件
- 四元数---基本概念(转载)
- Mathematical modeling idea of 2022 central China Cup
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
- 《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
- SQL injection
- Hackmyvm target series (4) -vulny
- 攻防世界MISC练习区(SimpleRAR、base64stego、功夫再高也怕菜刀)
- Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
猜你喜欢

《统计学》第八版贾俊平第五章概率与概率分布

SystemVerilog discusses loop loop structure and built-in loop variable I

Library management system

"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?

网络层—简单的arp断网

记一次api接口SQL注入实战

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

List and data frame of R language experiment III

Record an API interface SQL injection practice

Circular queue (C language)
随机推荐
Overview of LNMP architecture and construction of related services
List and data frame of R language experiment III
Sword finger offer 23 - print binary tree from top to bottom
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
New version of postman flows [introductory teaching chapter 01 send request]
Intranet information collection of Intranet penetration (5)
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
【指针】求解最后留下的人
Ucos-iii learning records (11) - task management
网络层—简单的arp断网
Markdown font color editing teaching
JDBC read this article is enough
AQS details
Database monitoring SQL execution
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
Hackmyvm target series (6) -videoclub
Data mining - a discussion on sample imbalance in classification problems
JDBC transactions, batch processing, and connection pooling (super detailed)
Intranet information collection of Intranet penetration (3)