当前位置:网站首页>求方程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)两个共轭的复根
边栏推荐
猜你喜欢
VS Code指定扩展安装位置
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
反卷积通俗详细解析与nn.ConvTranspose2d重要参数解释
Guide de signature du Code Appx
XML configuration file parsing and modeling
ISP、IAP、ICP、JTAG、SWD的编程特点
Google colab loads Google drive (Google drive is used in Google colab)
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
随机推荐
STM32 product introduction
Guid主键
Introduction to uboot
ArcGIS operation: batch modify attribute table
Postman interface test IV
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
Google colab loads Google drive (Google drive is used in Google colab)
Interface test
Study summary of postgraduate entrance examination in July
电表远程抄表拉合闸操作命令指令
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
Es classes and objects, prototypes
Pdf document signature Guide
Factorial implementation of large integer classes
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
JMeter loop controller and CSV data file settings are used together
CONDA creates virtual environment offline
学习记录——高精度加法和乘法
Word自动生成目录的方法