7-5 复数类的操作

 

1、声明一个复数类Complex(类私有数据成员为double型的real和image)

2、定义构造函数,用于指定复数的实部与虚部。

3、定义取反成员函数,调用时能返回该复数的相反数(实部、虚部分别是原数的相反数)。

4、定义成员函数Print(),调用该函数时,以格式(real, image)输出当前对象。

5、编写加法友元函数,以复数对象c1,c2为参数,求两个复数对象相加之和。

6、主程序实现:

(1)读入两个实数,用于初始化对象c1。

(2)读入两个实数,用于初始化对象c2。

(3)计算c1与c2相加结果,并输出。

(4)计算c2的相反数与c1相加结果,并输出。

输入格式:

输入有两行:

第一行是复数c1的实部与虚部,以空格分隔;

第二行是复数c2的实部与虚部,以空格分隔。

输出格式:

输出共三行:

第一行是c1与c2之和;

第二行是c2的相反数与c1之和;

第三行是c2 。

输入样例:

在这里给出一组输入。例如:

2.5 3.7
4.2 6.5

输出样例:

在这里给出相应的输出。例如:

(6.7, 10.2)
(-1.7, -2.8)
(4.2, 6.5)
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex()
    {
        real = 0;
        image = 0;
    }
    Complex(double r, double i)
    {
        real = r;
        image = i;
    }
    void set_Complex();
    void qvfan()
    {
        real = -real;
        image = -image;
    }
    void Print()
    {
        cout << "(" << real << ", " << image << ")" << endl;
    }
    friend void jiafa(Complex c1, Complex c2);
private:
    double real;
    double image;
};
void Complex::set_Complex()
{
    cin >> real >> image;
}
void jiafa(Complex c1, Complex c2)
{
    cout << "(" << c1.real + c2.real << ", " << c1.image + c2.image << ")" << endl;
}
int main()
{
    double c11, c12, c21, c22;
    Complex c1, c2;
    
    c1.set_Complex();
    c2.set_Complex();
    jiafa(c1, c2);
    c2.qvfan();
    jiafa(c1, c2);
    c2.qvfan();
    c2.Print();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
复数类操作主要包括以下内容: 1. 实部虚部的获取和设置 2. 模长的计算 3. 复数加、减、乘、除运算 4. 复数的共轭和相反数 下面是一个简单的复数类实现: ```python class ComplexNumber: def __init__(self, real=0, imag=0): self.real = real self.imag = imag def get_real(self): return self.real def set_real(self, real): self.real = real def get_imag(self): return self.imag def set_imag(self, imag): self.imag = imag def modulus(self): return (self.real ** 2 + self.imag ** 2) ** 0.5 def __add__(self, other): return ComplexNumber(self.real + other.real, self.imag + other.imag) def __sub__(self, other): return ComplexNumber(self.real - other.real, self.imag - other.imag) def __mul__(self, other): return ComplexNumber(self.real * other.real - self.imag * other.imag, self.real * other.imag + self.imag * other.real) def __truediv__(self, other): denominator = other.real ** 2 + other.imag ** 2 return ComplexNumber((self.real * other.real + self.imag * other.imag) / denominator, (self.imag * other.real - self.real * other.imag) / denominator) def conjugate(self): return ComplexNumber(self.real, -self.imag) def opposite(self): return ComplexNumber(-self.real, -self.imag) ``` 这里我们用到了 Python 中的魔术方法,即以双下划线开头和结尾的方法,例如 `__add__`、`__sub__`、`__mul__` 和 `__truediv__` 分别表示复数的加、减、乘、除运算。这些方法可以让我们像使用内置类型一样使用自定义类型。 下面是一些使用示例: ```python # 创建复数对象 z1 = ComplexNumber(1, 2) z2 = ComplexNumber(3, 4) # 获取实部虚部 print(z1.get_real(), z1.get_imag()) # 1 2 # 设置实部虚部 z1.set_real(5) z1.set_imag(6) print(z1.get_real(), z1.get_imag()) # 5 6 # 计算模长 print(z1.modulus()) # 7.810249675906654 # 复数加、减、乘、除运算 print(z1 + z2) # (8+10j) print(z1 - z2) # (-2-2j) print(z1 * z2) # (-7+36j) print(z1 / z2) # (0.44+0.08j) # 复数的共轭和相反数 print(z1.conjugate()) # (5-6j) print(z1.opposite()) # (-5-6j) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值