用c++语言求方程ax2+bx+c=0的根,用三个函数分别求b2-4ac大于0、等于0和小于0时

2025-04-04 18:13:49
推荐回答(1个)
回答1:

代码拷贝资料:

//#include "stdafx.h"//If the vc++6.0, with this line.

#include

#include "math.h"

using namespace std;

void get2root(double a,double b,double d){

double t=sqrt(d);

cout << "x1 = " << (-b+t)/a/2 << '\t';

cout << "x2 = " << (-b-t)/a/2 << '\n';

}

void get1root(double a,double b){

cout << "x1 = x2 = " << -b/a/2 << endl;

}

void get2vir(double a,double b,double d){

double t=sqrt(-d)/a/2,v=-b/a/2;

cout << "x1 = " << v << '+' << t << "i\t";

cout << "x2 = " << v << '-' << t << "i\n";

}


int main(int argc,char *argv[]){

double a,b,c,dlt;

cout << "Please enter a, b, c(R:)...\n";

cin >> a >> b >> c;

if((dlt=b*b-4*a*c)>0)

get2root(a,b,dlt);

else if(dlt==0)

get1root(a,b);

else

get2vir(a,b,dlt);

return 0;

}