代码拷贝资料:
//#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;
}