C++学习笔记总结练习:复数类complex的实现

news/2024/6/18 21:35:40 标签: c++, 学习, 笔记

C++中的复数类是一种用于表示和操作复数的自定义数据类型。复数由实部和虚部组成,可以表示为a + bi的形式,其中a是实部,b是虚部,i是虚数单位。

下面是一个简单的复数类的实现示例:

#include <iostream>

class Complex {
private:
    double real; // 实部
    double imaginary; // 虚部

public:
    // 默认构造函数
    Complex() {
        real = 0.0;
        imaginary = 0.0;
    }

    // 带参构造函数
    Complex(double r, double i) {
        real = r;
        imaginary = i;
    }

    // 获取实部
    double getReal() const {
        return real;
    }

    // 获取虚部
    double getImaginary() const {
        return imaginary;
    }

    // 重载加法运算符
    Complex operator+(const Complex& other) const {
        Complex result;
        result.real = real + other.real;
        result.imaginary = imaginary + other.imaginary;
        return result;
    }

    // 重载减法运算符
    Complex operator-(const Complex& other) const {
        Complex result;
        result.real = real - other.real;
        result.imaginary = imaginary - other.imaginary;
        return result;
    }

    // 重载乘法运算符
    Complex operator*(const Complex& other) const {
        Complex result;
        result.real = real * other.real - imaginary * other.imaginary;
        result.imaginary = real * other.imaginary + imaginary * other.real;
        return result;
    }

    // 重载输出运算符
    friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
        os << c.real << " + " << c.imaginary << "i";
        return os;
    }
};

int main() {
    Complex c1(2.0, 3.0);
    Complex c2(1.0, 2.0);

    Complex sum = c1 + c2;
    Complex difference = c1 - c2;
    Complex product = c1 * c2;

    std::cout << "c1: " << c1 << std::endl;
    std::cout << "c2: " << c2 << std::endl;
    std::cout << "Sum: " << sum << std::endl;
    std::cout << "Difference: " << difference << std::endl;
    std::cout << "Product: " << product << std::endl;

    return 0;
}

在上面的示例中,Complex类封装了两个私有成员变量real和imaginary,分别表示复数的实部和虚部。它提供了默认构造函数和带参构造函数来初始化复数对象。

Complex类还重载了加法运算符、减法运算符和乘法运算符,使得可以方便地进行复数的加减乘操作。此外,还重载了输出运算符<<,以便能够直接输出复数对象的内容。

在main函数中,我们创建了两个Complex对象c1和c2,并使用重载的加法运算符、减法运算符和乘法运算符进行复数的加减乘操作。最后,我们输出了这些操作的结果。

需要注意的是,这只是一个简单的复数类的实现示例,实际的复数类可以根据需求进行扩展和优化

实例——complex的实现

#include <iostream>
using namespace std;
class complex{
public:
    complex(double real = 0.0, double imag = 0.0): m_real(real), m_imag(imag){ };
public:
    friend complex operator+(const complex & A, const complex & B);
    friend complex operator-(const complex & A, const complex & B);
    friend complex operator*(const complex & A, const complex & B);
    friend complex operator/(const complex & A, const complex & B);
    friend istream & operator>>(istream & in, complex & A);
    friend ostream & operator<<(ostream & out, complex & A);
private:
    double m_real;  //实部
    double m_imag;  //虚部
};
//重载加法运算符
complex operator+(const complex & A, const complex &B){
    complex C;
    C.m_real = A.m_real + B.m_real;
    C.m_imag = A.m_imag + B.m_imag;
    return C;
}
//重载减法运算符
complex operator-(const complex & A, const complex &B){
    complex C;
    C.m_real = A.m_real - B.m_real;
    C.m_imag = A.m_imag - B.m_imag;
    return C;
}
//重载乘法运算符
complex operator*(const complex & A, const complex &B){
    complex C;
    C.m_real = A.m_real * B.m_real - A.m_imag * B.m_imag;
    C.m_imag = A.m_imag * B.m_real + A.m_real * B.m_imag;
    return C;
}
//重载除法运算符
complex operator/(const complex & A, const complex & B){
    complex C;
    double square = A.m_real * A.m_real + A.m_imag * A.m_imag;
    C.m_real = (A.m_real * B.m_real + A.m_imag * B.m_imag)/square;
    C.m_imag = (A.m_imag * B.m_real - A.m_real * B.m_imag)/square;
    return C;
}
//重载输入运算符
istream & operator>>(istream & in, complex & A){
    in >> A.m_real >> A.m_imag;
    return in;
}
//重载输出运算符
ostream & operator<<(ostream & out, complex & A){
    out << A.m_real <<" + "<< A.m_imag <<" i ";;
    return out;
}
int main(){
    complex c1, c2, c3;
    cin>>c1>>c2;
    c3 = c1 + c2;
    cout<<"c1 + c2 = "<<c3<<endl;
    c3 = c1 - c2;
    cout<<"c1 - c2 = "<<c3<<endl;
    c3 = c1 * c2;
    cout<<"c1 * c2 = "<<c3<<endl;
    c3 = c1 / c2;
    cout<<"c1 / c2 = "<<c3<<endl;
    return 0;
}

http://www.niftyadmin.cn/n/4943646.html

相关文章

信息与通信工程面试准备——信号与系统|10:23

8月16日 23:21 目录 ​编辑 1. 调制的作用 2. 放大器与振荡器的作用和区别 工作原理 输出信号 应用 反馈方式 设计复杂度 装置性质 3. 信号与系统&#xff1a;三大变换之间的关系&#xff1f; 4. 无码间串扰的条件 5. 冲激函数的作用&#xff1f; 研究的意义&…

1049. 最后一块石头的重量 II ● 494. 目标和 ● 474.一和零

1049. 最后一块石头的重量 II class Solution { public:int lastStoneWeightII(vector<int>& stones) {vector<int>dp(15001,0);int sum0;for(int i0;i<stones.size();i)sumstones[i];int targetsum/2;for(int i0;i<stones.size();i){for(int jtarget;j…

开发测试框架一 - 创建springboot工程及基础操作

一、创建及运行方式 1. 从官网导入&#xff1a; 注意&#xff1a;由于我的java版本是1.8&#xff1b;所以选中了spring2.7.14&#xff1b;如果你的java版本是9及以上&#xff0c;选中spring3相关的同时Java 版本也要对应起来 2. 创建第一个get请求 创建Controller package及…

强化学习A3C算法

强化学习A3C算法 效果&#xff1a; a3c.py import matplotlib from matplotlib import pyplot as plt matplotlib.rcParams[font.size] 18 matplotlib.rcParams[figure.titlesize] 18 matplotlib.rcParams[figure.figsize] [9, 7] matplotlib.rcParams[font.family]…

7. CSS(四)

目录 一、浮动 &#xff08;一&#xff09;传统网页布局的三种方式 &#xff08;二&#xff09;标准流&#xff08;普通流/文档流&#xff09; &#xff08;三&#xff09;为什么需要浮动&#xff1f; &#xff08;四&#xff09;什么是浮动 &#xff08;五&#xff09;浮…

Programming abstractions in C阅读笔记: p114-p117

《Programming Abstractions in C》学习第48天&#xff0c;p114-p117&#xff0c;​总结如下&#xff1a; 一、技术总结 主要通过random number介绍了随机数的相关用法&#xff0c;interface​示例(random.h)​&#xff0c;client program示例(craps.c)。 #include <stdio…

form表单的entype属性选取

一、上传文件时entype属性值怎么选取&#xff1f; 上传文件的话必须指定form的enctype&#xff08;encode type&#xff0c;编码类型&#xff09;属性为multipart/form-data&#xff0c;表示表单数据有多部分组成&#xff0c;既有文本又有文件等二进制数据&#xff0c;指定…

NPM与外部服务的集成(下)

目录 1、撤消访问令牌 2、在CI/CD工作流中使用私有包 2.1 创建新的访问令牌 持续整合 持续部署 交互式工作流 CIDR白名单 2.2 将令牌设置为CI/CD服务器上的环境变量 2.3 创建并签入特定于项目的.npmrc文件 2.4 令牌安全 3、Docker和私有模块 3.1 背景&#xff1a;运…