Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> //
- #include <string>
- #include <math.h>
- using namespace std; //
- class Complex{
- protected:
- double real;
- double img;
- public:
- Complex(double real,double img)
- {
- this->real = real;
- this->img = img;
- }
- Complex()
- {
- this->real = 0.0;
- this->img = 0.0;
- }
- Complex operator+(const Complex& rhs)
- {
- Complex result;
- result.real = this->real + rhs.real;
- result.img = this->img + rhs.img;
- return result;
- }
- Complex operator+(double realNum)
- {
- Complex result;
- result.real = this->real + realNum;
- result.img = this->img;
- return result;
- }
- Complex operator-(const Complex& rhs)
- {
- Complex result;
- result.real = this->real - rhs.real;
- result.img = this->img - rhs.img;
- return result;
- }
- Complex operator-(double rhs)
- {
- Complex result;
- result.real = this->real - rhs;
- result.img = this->img;
- return result;
- }
- Complex operator*(const Complex& rhs)
- {
- Complex result;
- result.real = this->real * rhs.real * 1.0 - this->img * rhs.img * 1.0;
- result.img = this->real * rhs.img * 1.0 + this->img * rhs.real * 1.0;
- return result;
- }
- Complex operator * (double rhs)
- {
- Complex result;
- result.real = this->real * rhs * 1.0;
- result.img = this->img * rhs * 1.0;
- return result;
- }
- Complex operator / (const Complex& rhs)
- {
- if (rhs.real == 0 && rhs.img == 0)
- {
- throw ("Error! Divide by zero\n");
- }
- Complex result;
- result.real = this->real * rhs.real * 1.0 - this->img * rhs.img * 1.0;
- result.real = 1.0 * result.real / (rhs.real * rhs.real + rhs.img * rhs.img);
- result.img = this->real * rhs.img * 1.0 + this->img * rhs.real * 1.0;
- result.img = 1.0 * result.img / (rhs.real * rhs.real + rhs.img * rhs.img);
- return result;
- }
- Complex operator / (double rhs)
- {
- Complex result;
- if (rhs == 0)
- {
- throw ("Error! Divide by zero\n");
- }
- result.real = 1.0 * this->real / rhs;
- result.img = 1.0 * this->img / rhs;
- return result;
- }
- friend ostream& operator<<(ostream& os, const Complex& rhs)
- {
- if (rhs.img > 0) os << rhs.real << " +" << rhs.img << "i";
- else os << rhs.real << " -" << -rhs.img << "i";
- return os;
- }
- friend istream& operator>>(istream& in,Complex& rhs)
- {
- cout << "Nhap phan thuc \n";
- in >> rhs.real;
- cout << "Nhap phan ao\n";
- in >> rhs.img;
- return in;
- }
- friend void ptb2(double a, double b, double c);
- };
- void ptb2(double a = 0, double b = 0, double c = 0)
- {
- if (a == 0)
- {
- if (b == 0)
- {
- if (c != 0) cout << "Phuong trinh vo nghiem\n";
- else cout << "Phuong trinh co mot nghiem x = 0\n";
- return;
- }
- else
- {
- double res = -1.0*c/b;
- cout << "Phuong trinh co mot nghiem la x = " << res << endl;
- return;
- }
- }
- double delta = b*b - 4 * a *c;
- if (delta == 0)
- {
- double res = -1.0*b/(2*a);
- cout << "Phuong trinh co mot nghiem la x = " << res << endl;
- return;
- }
- if (delta > 0)
- {
- double res1 = -b + sqrt(delta);
- res1 = 1.0*res1/(2*a);
- double res2 = -b - sqrt(delta);
- res2 = 1.0*res2/(2/a);
- cout << "Phuong trinh co hai nghiem la x1 = " << res1 << ", x2 = " << res2 << endl;
- return;
- }
- if (delta < 0)
- {
- Complex res1;
- res1.real = -b/(2*a);
- res1.img = sqrt(-delta)/(2*a);
- Complex res2;
- res2.real = res1.real;
- res2.img = -res1.img;
- cout << "Phuong trinh co hai nghiem la x1 = " << res1 << ", x2 = " << res2 << endl;
- // cout << "Phuong trinh co hai nghiem ao la x1 = " << res1.real << " +" << res1.img << "i"
- // << ", x2 = " << res2.real << " +" << res2.img << endl;
- return;
- }
- }
- int main ()
- {
- ptb2(1,2,2);
- }
Add Comment
Please, Sign In to add comment