Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class complex
- {
- private:
- double re, im;
- public:
- complex() { re = 0; im = 0; }
- complex(double r, double i) { re = r; im = i; }
- complex(const complex &ob) { re = ob.re; im = ob.im; };
- complex& operator = (complex);
- complex operator + (complex);
- complex operator - (complex);
- complex operator * (complex&);
- complex operator [] (int);
- complex operator () (char);
- friend istream& operator >>(istream&, complex&);
- friend ostream& operator << (ostream&, const complex&);
- bool operator == (complex& com);
- bool operator != (complex& com);
- bool operator > (complex& com);
- bool operator < (complex& com);
- };
- bool complex::operator > (complex& com)
- {
- if (this->re > com.re)
- return 1;
- else if (this->re == com.re && this->im > com.im)
- return 1;
- else
- return 0;
- }
- bool complex::operator < (complex& com)
- {
- if (this->re < com.re)
- return 1;
- else if (this->re == com.re && this->im < com.im)
- return 1;
- else
- return 0;
- }
- bool complex::operator != (complex& com)
- {
- if (this->re != com.re || this->im != com.im)
- return 1;
- else
- return 0;
- }
- bool complex::operator==(complex& com)
- {
- if (this->re == com.re && this->im == com.im)
- return 1;
- else
- return 0;
- }
- complex complex::operator*(complex &com)
- {
- double i, j;
- i = re * com.re - im * com.im;
- j = re * com.im + com.re * im;
- re = i;
- im = j;
- return *this;
- }
- complex& complex::operator=(complex com)
- {
- this->re = com.re;
- this->im = com.im;
- return *this;
- }
- complex complex::operator+(complex com)
- {
- this->re = this->re + com.re;
- this->im = this->im + com.im;
- return *this;
- }
- complex complex::operator-(complex com)
- {
- this->re = this->re - com.re;
- this->im = this->im - com.im;
- return *this;
- }
- ostream& operator << (ostream& out, const complex& com)
- {
- if (com.im < 0)
- out << com.re << "+i(" << com.im << ")\n";
- else
- out << com.re << "+i" << com.im << "\n";
- return out;
- }
- istream& operator >> (istream& in, complex& com)
- {
- std::cout << "Введите действительную часть комплексного числа ";
- in >> com.re;
- std::cout << "Введите мнимую часть комплексного числа ";
- in >> com.im;
- return in;
- }
- float complex::operator[](int i) {
- if (i == 0) {
- return re;
- }
- else if (i == 1) {
- return im;
- }
- else {
- cout << "Оператор []";
- abort();
- }
- }
- float complex::operator()(char c) {
- if (c == 're') {
- return re;
- }
- else if (c == 'im') {
- return im;
- }
- else {
- cout << "Оператор ()";
- abort();
- }
- }
- int main()
- {
- setlocale(0, "rus");
- complex com;
- cin >> com;
- cout << com << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement