求一个二元方程的根,输入方程的系数A,B,C;
(1)
采用子函数判定方程根的个数;
(2)
若方程有根时,求出相应的根;
(3)
若方程无根时,则继续改变方程的系数,直到有解输出为止;
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2;
int
d;
int sort(float a,float b,float c);//子函数的申明
printf("pls input the a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
d=sort(a,b,c);
do
{
if(d==0)
{
printf("the function has noelement\n");
scanf("%f%f%f",&a,&b,&c);
d=sort(a,b,c);
}
}
while(4*a*c-b*b>0);
if(d==1)
{
printf("the function has twoelement\n");
x1=(-b+sqrt(b*b-4*a*c))/2*a;
printf("x1=x2=%f\n",x1);
}
if(d==2)
{
printf("the function has twoelement\n");
x1=(-b+sqrt(b*b-4*a*c))/2*a;
x2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("x1=%f x2=%f\n",x1,x2);
}
}
int sort(float a,float b,float c)//子函数的定义
{
int flag;
if(b*b-4*a*c>0)
flag=2;
else if(b*b-4*a*c==0)
flag=1;
else flag=0;
return(flag);
}
有志于深圳大学通信工程考研的,看一看吧!很简单的,所以大家不要担心。