C语言一元二次方程的写法
# include <stdio.h>
# include <math.h>
int main (void){
// 把三个系数保存到变量当中
int a = 1;
int b = 2;
int c = 3;
// 定义一个变量来判断到底有几个解
double solution;
solution = b*b - 4*a*c;
// 定义两个变量来装解
double x1;
double x2;
// 通过判断 solution的值来判断有几个解
if(solution > 0)
{
x1 = (-b + sqrt(solution)) / (2*a);
x2 = (-b - sqrt(solution)) / (2*a);
printf("该方程有两个解,x1 = %f x2 = %f\n" ,x1,x2);
} else if(solution == 0)
{
x1 = (-b + sqrt(solution)) / 2*a;
x2 = x1;
printf("该方程有一个解,x1 = x2 = %f\n" ,x1);
}else if(solution < 0)
{
printf("该方程无解\n");
}
return 0;
}