Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #define EPS 1e-6
- using namespace std;
- class Comp {
- float re, im;
- public:
- Comp() {}
- Comp& operator = (const Comp &x) {
- re = x.re; im = x.im;
- return *this;
- }
- Comp &operator -= (const Comp &x) {
- re -= x.re; im -= x.im;
- return *this;
- }
- friend Comp operator + (const Comp &a, const Comp &b);
- bool operator == (const Comp &x) {
- return (this -> re == x.re && this -> im == x.im);
- }
- void set(char c);
- void get(char c);
- };
- int main() {
- Comp a, b, c;
- a.set('a');
- b.set('b');
- cout << endl << "Operator -=" << endl;
- cout << "before:" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "after a -= b:" << endl;
- a -= b;
- a.get('a');
- b.get('b');
- cout << endl << "Operator +" << endl;
- a.get('a');
- b.get('b');
- c = a + b;
- cout << endl << "after c = a + b:" << endl;
- c.get('c');
- cout << endl << "Operator ==" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "after a == b:" << endl;
- if (a == b) {
- cout << "true";
- } else {
- cout << "false";
- }
- a = b;
- cout << endl << endl << "In a assigned b:" << endl;
- cout << endl << "Operator ==" << endl;
- a.get('a');
- b.get('b');
- cout << endl << "after a == b:" << endl;
- if (a == b) {
- cout << "true";
- } else {
- cout << "false";
- }
- return 0;
- }
- Comp operator + (const Comp &a, const Comp &b) {
- Comp c;
- c.re = a.re + b.re; c.im = a.im + b.im;
- return c;
- }
- bool isZero(double x) { return fabs(x) > EPS; }
- void Comp :: set(char c) {
- cout << "Enter comp number " << c << ":" << endl;
- cout << "\tre: ";
- cin >> re;
- cout << "\tim: ";
- cin >> im;
- return;
- }
- void Comp :: get(char c) {
- cout << "Comp number " << c << ": ";
- if (!isZero(re)) {
- cout << re;
- }
- if (!isZero(im)) {
- if (!isZero(re) && im > 0) {
- cout << " + ";
- }
- if (im < 0) {
- cout << " - ";
- }
- cout << abs(im) << "i";
- }
- cout << endl;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement