解一元二次方程

Source code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<math.h>
using namespace std;
class equation{
private:
int a,b,c,d;
float x1,x2;
public:
void setdata();
void display();
};
void equation::setdata(){
cin>>a>>b>>c;
}
void equation::display(){
d=b*b-4*a*c;
if(d>=0){
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<"x1="<<x1<<endl<<"x2="<<x2<<endl;
}
if(d<0){
cout<<"x1="<<(-b)/(2*a)<<"+"<<sqrt(-d)/(2*a)<<"i"<<endl;
cout<<"x2="<<(-b)/(2*a)<<"-"<<sqrt(-d)/(2*a)<<"i"<<endl;
}

}
int main(){
equation e;
e.setdata();
e.display();
return 0;
}