实部为什么为零?
怎样使输入 3 4 5 时输出实部为小数而不是0?#include<iostream>
#include <math.h>
using namespace std;
void dayn(int a,int b,int c)
{
float m,x1,x2;
m=sqrt(b*b-4*a*c);
x1=(-b+m)/(2*a);
x2=(-b-m)/(2*a);
cout<<"方程的两个实根为"<<endl<<"x1="<<x1<<endl<<"x2="<<x2<<endl;
}
void deyn(int a,int b,int c)
{
float x;
x=(-b)/(2*a);
cout<<"方程只有一个根,它为"<<x<<endl;
}
void xyn(int a,int b,int c)
{
float disc,p,q;
disc=b*b-4*a*c;
p=-b/(2*a);
q=sqrt(-disc)/(2*a);
cout<<"方程有两个虚根,它们为"<<endl<<"x1="<<p<<"+"<<q<<"i"<<endl;
cout<<"x2="<<p<<"-"<<q<<"i"<<endl;
}
main()
{
float a,b,c,disc;
cout<<"请输入一元二次方程的三个系数"<<endl;
cin>>a>>b>>c;
disc=b*b-4*a*c;
if(disc>0)dayn(a,b,c);
else if(disc==0)deyn(a,b,c);
else xyn(a,b,c);
}