Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Complex {
- double re, im;
- public:
- // Complex(double, double);
- Complex(double Re = 0.0, double Im = 0.0) {
- re = Re;
- im = Im;
- }
- double getRe() {
- return re;
- }
- double getIm() {
- return im;
- }
- void setRe(double a) {
- re = a;
- }
- void setIm(double a) {
- im = a;
- }
- bool operator == (const Complex &);
- bool operator != (const Complex &);
- Complex operator + (const Complex &);
- Complex operator - (const Complex &);
- Complex operator * (const Complex &);
- Complex operator / (const Complex &);
- friend ostream & operator << (ostream & os,const Complex & a);
- friend istream & operator >> (istream & is, Complex & a);
- };
- bool Complex::operator ==(const Complex & x) {
- return (re == x.re && im == x.im);
- }
- bool Complex::operator !=(const Complex & x) {
- return !(re == x.re && im == x.im);
- }
- Complex Complex::operator + (const Complex & x) {
- Complex res;
- res.re = re + x.re;
- res.im = im + x.im;
- return res;
- }
- Complex Complex::operator - (const Complex & x) {
- Complex res;
- res.re = re - x.re;
- res.im = im - x.im;
- return res;
- }
- Complex Complex::operator * (const Complex & x) {
- Complex res;
- res.re = re * x.re - im * x.im;
- res.im = re * x.im + x.re * im;
- return res;
- }
- Complex Complex::operator / (const Complex & x) {
- Complex res;
- res.re = (re * x.re + im * x.im) / (im * im + x.im * x.im);
- res.im = (im * x.re - re * x.im) / (im * im + x.im * x.im);
- return res;
- }
- ostream & operator << (ostream & os, const Complex & a) {
- os << a.re;
- if (a.im)
- os << showpos << a.im << noshowpos << "i";
- os << endl;
- return os;
- }
- istream & operator >> (istream & is, Complex & a) {
- is >> a.re >> a.im;
- return is;
- }
- int main() {
- Complex a, b;
- cin >> a;
- cin >> b;
- cout << a;
- a = a + b;
- cout << a;
- a = a / b;
- cout << a;
- a.setIm(5);
- a.setRe(2);
- b.setIm(5);
- b.setRe(2);
- if (a!=b) cout << "NO";
- else cout <<"YES";
- b = a * b;
- cout << b;
- b = b / a;
- if (a==b) cout << "YES";
- else cout <<"NO";
- cout << endl << a.getRe() << " " << a.getIm() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement