求解一元二次方程.c

Explanation:
Seeking roots of the equation a
x
x+b
x+c .With three functions, please when bb-4ac root greater than zero, or equal to zero and less than zero, and output the result.**

//  date:2020/3/4
//  author:xiezhg5
#include <stdio.h>
#include <math.h>                      //调用sqrt函数 
float x1,x2,disc,p,q;
int main(void)
{
	//函数声明,分大于0,小于0,等于0三种情况 
	void dayu_zero(float,float);
	void dengyu_zero(float,float);
	void xiaoyu_zero(float,float);
	float a,b,c;
	//一些提示的信息 
	printf("请输入a,b,c:\n");
	scanf("%f %f %f",&a,&b,&c);
	printf("方程:%.2f*x*x+%.2f*x+%.2f=0\n",a,b,c);
	disc=b*b-4*a*c;
	printf("根:\n");
	//if--else选择结构 
	if(disc>0)
	{
		dayu_zero(a,b);              //调用dayu_zero函数 
		printf("x1=%f\n",x1);
		printf("x2=%f\n",x2);
	}
	else if(disc==0)
	{
		dengyu_zero(a,b);           //调用dengyu_zero函数 
		printf("x1=%f\n",x1);
		printf("x2=%f\n",x2);
	}
	else
	{
		xiaoyu_zero(a,b);           //调用xiaoyu_zero函数 
		printf("x1=%f+%fi\n",p,q);
		printf("x2=%f-%fi\n",p,q); 
	}
}


void dayu_zero(float a,float b)          //函数名(数据类型 变量名) 
{
	//求根公式 
	x1=(-b+sqrt(disc))/(2*a);
	x2=(-b-sqrt(disc))/(2*a);
}

void dengyu_zero(float a,float b)
{
	//求根公式 
	x1=x2=(-b)/(2*a);
}

void xiaoyu_zero(float a,float b)
{
	//求根公式(虚数) 
	p=-b/(2*a);
	q=sqrt(-disc)/(2*a);
}
发布了30 篇原创文章 · 获赞 10 · 访问量 297

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104649423
今日推荐