记录我用C语言解的第一个方程~

就是很简单的ax^2+bx+c=0

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

void fun(float a, float b, float c)
{
	float x1,x2,d;
	d=b*b-4*a*c;
	if(d<0)
	{
		printf("no root!\n");
	}
	if(d==0)
	{
		x1=x2=(-b)/(2*a);
		printf("root is %f\n",x1);
	}
	if(d>0)
	{
		x1=((-b)+sqrt(d))/(2*a);
		x2=((-b)-sqrt(d))/(2*a);
		printf("one root is %f\nanother is %f\n",x1,x2);
	}
}

void main()
{
	float a,b,c;
	scanf("%f%f%f", &a, &b, &c);
	fun(a,b,c);
}

表示次方可以用pow(),例如b^2=pow(b,2)

猜你喜欢

转载自blog.csdn.net/Mayouhpevrday/article/details/82814161