#include using namespace std;

class Complex { private: double real; double imag;

public: Complex(double r, double i) { real = r; imag = i; }

Complex add(Complex c) {
    double r = real + c.real;
    double i = imag + c.imag;
    return Complex(r, i);
}

Complex subtract(Complex c) {
    double r = real - c.real;
    double i = imag - c.imag;
    return Complex(r, i);
}

void display() {
    cout << real << ' + ' << imag << 'i' << endl;
}

};

int main() { Complex c1(3, 4); Complex c2(1, 2);

Complex sum = c1.add(c2);
Complex diff = c1.subtract(c2);

cout << "Sum: ";
sum.display();

cout << "Difference: ";
diff.display();

return 0;

}


原文地址: https://www.cveoy.top/t/topic/o8NT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录