当前位置:网站首页>求方程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)两个共轭的复根
边栏推荐
- Win10 installation vs2015
- Or in SQL, what scenarios will lead to full table scanning
- Postman interface test IV
- 电表远程抄表拉合闸操作命令指令
- 【acwing】786. 第k个数
- ES6中的函數進階學習
- Phpcms realizes PC website access to wechat native payment
- Google colab loads Google drive (Google drive is used in Google colab)
- Why is the reflection efficiency low?
- ORM -- grouping query, aggregation query, query set queryset object properties
猜你喜欢
Word自动生成目录的方法
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
Bean operation domain and life cycle
ISP、IAP、ICP、JTAG、SWD的编程特点
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
字符串格式化
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
IO模型复习
电表远程抄表拉合闸操作命令指令
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
随机推荐
ORM -- database addition, deletion, modification and query operation logic
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
Why is the reflection efficiency low?
【ORM框架】
IPv4 socket address structure
When there are pointer variable members in the custom type, the return value and parameters of the assignment operator overload must be reference types
【acwing】786. 第k个数
Postman interface test VI
Some test points about coupon test
[second on] [jeecgboot] modify paging parameters
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
高数_第1章空间解析几何与向量代数_向量的数量积
Postman interface test IV
[sword finger offer] 42 Stack push in and pop-up sequence
The new activity of "the arrival of twelve constellations and goddesses" was launched
. Net configuration system
Bean operation domain and life cycle
Word自动生成目录的方法
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
Programming features of ISP, IAP, ICP, JTAG and SWD