[c]代码库
/*求一元二次方程的解*/
#include"stdio.h"
#include"math.h"
void main()
{
int m;
do
{
m=1;
float a,b,c,delta,x1,x2,realpt,imagpt;
printf ( "\n【求一元二次方程的解】\n" );
printf ( "请输入一元二次方程的三个参数。\n" );
scanf ( "%f%f%f",&a,&b,&c );
if ( a!=0 )
{
delta=b*b-4*a*c; //求一元二次方程的△
if ( delta>0 )
{
x1= ( -b+sqrt ( delta ) /2*a );
x2= ( +b+sqrt ( delta ) /2*a );
printf ( "方程的两个不等实根为:x1=%8.4f x2=%8.4f\n",x1,x2 );
}
else if ( delta==0 )
{
x1=x2=-b/ ( 2*b );
printf ( "方程的两个相等实根为:x1=x2=%8.4f\n",x1 );
}
else
{
delta=-delta;
realpt=-b/ ( 2*a );
imagpt=sqrt ( delta );
printf ( "方程的两个共轭根为:\n" );
printf ( "x1=%8.4f+%8.4fi\n",realpt,imagpt );
printf ( "x2=%8.4f-%8.4fi\n",realpt,imagpt );
}
}
else
printf ( "不是一元二次方程!\n" );
}
while ( m==1 );
}
中级程序员
by: 写c加加的小菜鸟 发表于:2017-02-19 19:20:32 顶(0) | 踩(0) 回复
不错,居然连共轭复数都考虑到了
回复评论