C语言:解一元二次方程

求方程ax^{2}+bx+c=0的根,a、b、c从键盘输入。

C语言程序如下:

#include <stdio.h>
#include <math.h>

int main()
{
	void two_equal_root(double m,double n);//Δ=0时 
    void two_real_root(double m,double n,double l);//Δ>0时
	void two_comlex_root(double m,double n,double l);//Δ<0时   
	double a,b,c,disc;
	printf("请从键盘依次输入a、b、c的值:");
	scanf("%lf%lf%lf",&a,&b,&c);
	if(fabs(a)<=1e-6){printf("此方程不是一元二次方程!");return 0;} 
	disc=b*b-4*a*c;
	if(fabs(disc)<=1e-6) two_equal_root(a,b);//Δ=0时 
	else
	{
		if(disc>1e-6) two_real_root(a,b,disc);//Δ>0时
		else two_comlex_root(a,b,disc);//Δ<0时 
	}
	return 0;
} 

void two_equal_root(double m,double n)//Δ=0时 
{
	double x;
	x=(-n)/(2*m);
	printf("有两个相等的根:x=%.2f",x); 
}

void two_real_root(double m,double n,double l)//Δ>0时 
{
	double x1,x2;
	x1=(-n+sqrt(l))/(2*m);
	x2=(-n-sqrt(l))/(2*m);
    printf("有两个不等的实根:x1=%.2f,x2=%.2f",x1,x2);
} 

void two_comlex_root(double m,double n,double l)//Δ<0时 
{
	double real_part,imag_part;
	real_part=(-n)/(2*m);
	imag_part=sqrt(-l)/(2*m);
    printf("有两个不等的复根:x1=%.2f+%.2fi,x2=%.2f-%.2fi",real_part,imag_part,real_part,imag_part);	
}

程序运行结果如下:

请各位大佬多多指正!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值