当前位置:网站首页>求方程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)两个共轭的复根

边栏推荐
- The new activity of "the arrival of twelve constellations and goddesses" was launched
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- JMeter installation
- Prototype object in ES6
- Remote meter reading, switching on and off operation command
- 【acwing】786. Number k
- Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
- Es classes and objects, prototypes
- Methods of adding centerlines and centerlines in SolidWorks drawings
- ORM model -- creation and query of data records
猜你喜欢

【剑指Offer】42. 栈的压入、弹出序列

fiddler-AutoResponder

【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法

Guide de signature du Code Appx

浅谈日志中的返回格式封装格式处理,异常处理

The physical meaning of imaginary number J

ORM模型--数据记录的创建操作,查询操作

ES6中的原型对象

Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is

High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
随机推荐
STM32 Basics - memory mapping
【华为机试真题详解】高矮个子排队
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Some test points about coupon test
Postman interface test VII
. Net configuration system
Programming features of ISP, IAP, ICP, JTAG and SWD
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
The request object parses the request body and request header parameters
LeetCode 练习——113. 路径总和 II
Apprentissage avancé des fonctions en es6
【acwing】789. Range of numbers (binary basis)
STM32 product introduction
Postman interface test IV
Win10 installation vs2015
Inno setup packaging and signing Guide
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
Smart city construction based on GIS 3D visualization technology
Fiddler simulates the interface test
Vs code specifies the extension installation location