【无标题】


#include <iostream>
#include <cmath>

using namespace std;

class Complex {
public:
    double real, imag; // 实部和虚部都是实数范围

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

    Complex operator+(const Complex& c) const {
        return Complex(real + c.real, imag + c.imag);
    }

    Complex operator-(const Complex& c) const {
        return Complex(real - c.real, imag - c.imag);
    }

    Complex operator*(const Complex& c) const {
        return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
    }

    Complex operator/(const Complex& c) const {
        return *this * (c.conjugate()); // 注意要先求共轭再相乘,否则会出现除以零的情况
    }

    Complex operator^(int n) const {
        Complex result(*this);
        for (int i = 0; i < n; i++) result = result * result; // 实现幂次方运算
        return result;
    }

    Complex operator%(const double p) const { // 实现取模运算
        return *this * (Complex(1.0, 0.0) % p); // 先求出模长p的共轭复数,再与当前复数相乘即可得到模余数
    }

    double arg() const { // 实现辐角运算
        return atan2(imag, real); // 采用反正切函数计算辐角,注意要使用复数的辐角定义,即π/2表示实轴的反向延长线与复平面的交点处的角度。
    }

    Complex conjugate() const { // 实现共轭运算
        return Complex(real, -imag); // 直接将实部和虚部取相反数即可得到共轭复数。
    }
};

int main() {
    int choice;
    double num1, num2;
    cin >> choice >> num1 >> num2;

    if (choice == 1) { // 如果是加法操作,则直接输出结果并返回。
        Complex result = Complex(num1, num2);
        cout << result << " + " << num1 << "i + " << num2 << "i = " << result + num1 << " + " << num2 << "i" << endl;
        return 0;
    } else if (choice == 2) { // 如果是减法操作,则先求出共轭复数,然后再进行减法运算。注意要先求出共轭复数再相减。
        Complex a, b, c;
        cin >> a >> b;
        c = a + b;
        c = c.conjugate(); // 先求出共轭复数c'=a+bi的共轭复数c'=(a-bi)。然后再用c'减去b即可得到结果c=a-bi。注意这里不能直接用c-b,因为两个复数相减时需要先进行共轭操作。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值