拷贝控制台打印出来的数组对象

问题:如何拷贝谷歌控制台打印出来的数组对象

右键需要拷贝的数组对象
选择 复制object
在编辑器中粘贴

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是复数类ComplexNum的声明和定义: ComplexNum.h ``` #pragma once class ComplexNum { public: ComplexNum(); // 默认构造函数 ComplexNum(double r, double i = 0); // 构造函数 ComplexNum(const ComplexNum& other); // 拷贝构造函数 ~ComplexNum(); // 析构函数 ComplexNum operator+(const ComplexNum& other) const; // 加法运算 ComplexNum operator-(const ComplexNum& other) const; // 减法运算 ComplexNum operator*(const ComplexNum& other) const; // 乘法运算 ComplexNum operator/(const ComplexNum& other) const; // 除法运算 friend std::ostream& operator<<(std::ostream& os, const ComplexNum& c); // 输出运算符重载 private: double real; // 实部 double imag; // 虚部 }; ``` ComplexNum.cpp ``` #include "ComplexNum.h" ComplexNum::ComplexNum() : real(0), imag(0) { } ComplexNum::ComplexNum(double r, double i) : real(r), imag(i) { } ComplexNum::ComplexNum(const ComplexNum& other) : real(other.real), imag(other.imag) { } ComplexNum::~ComplexNum() { } ComplexNum ComplexNum::operator+(const ComplexNum& other) const { return ComplexNum(real + other.real, imag + other.imag); } ComplexNum ComplexNum::operator-(const ComplexNum& other) const { return ComplexNum(real - other.real, imag - other.imag); } ComplexNum ComplexNum::operator*(const ComplexNum& other) const { double r = real * other.real - imag * other.imag; double i = real * other.imag + imag * other.real; return ComplexNum(r, i); } ComplexNum ComplexNum::operator/(const ComplexNum& other) const { double r = (real * other.real + imag * other.imag) / (other.real * other.real + other.imag * other.imag); double i = (imag * other.real - real * other.imag) / (other.real * other.real + other.imag * other.imag); return ComplexNum(r, i); } std::ostream& operator<<(std::ostream& os, const ComplexNum& c) { if (c.imag >= 0) { os << c.real << "+" << c.imag << "i"; } else { os << c.real << c.imag << "i"; } return os; } ``` 以下是矩阵类Matrix的声明和定义: Matrix.h ``` #pragma once #include "ComplexNum.h" class Matrix { public: Matrix(int rows, int cols); // 构造函数 Matrix(const Matrix& other); // 拷贝构造函数 ~Matrix(); // 析构函数 ComplexNum& operator()(int i, int j); // 重载括号运算符 const ComplexNum& operator()(int i, int j) const; // 重载括号运算符 Matrix operator+(const Matrix& other) const; // 加法运算 Matrix operator-(const Matrix& other) const; // 减法运算 Matrix operator*(const Matrix& other) const; // 乘法运算 void print() const; // 打印矩阵 private: int rows; // 行数 int cols; // 列数 ComplexNum* p; // 指向复数数组的指针 }; ``` Matrix.cpp ``` #include "Matrix.h" #include <iostream> Matrix::Matrix(int rows, int cols) : rows(rows), cols(cols) { p = new ComplexNum[rows * cols]; } Matrix::Matrix(const Matrix& other) : rows(other.rows), cols(other.cols) { p = new ComplexNum[rows * cols]; for (int i = 0; i < rows * cols; i++) { p[i] = other.p[i]; } } Matrix::~Matrix() { delete[] p; } ComplexNum& Matrix::operator()(int i, int j) { return p[i * cols + j]; } const ComplexNum& Matrix::operator()(int i, int j) const { return p[i * cols + j]; } Matrix Matrix::operator+(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { std::cerr << "矩阵加法运算错误:矩阵大小不一致" << std::endl; exit(1); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) + other(i, j); } } return result; } Matrix Matrix::operator-(const Matrix& other) const { if (rows != other.rows || cols != other.cols) { std::cerr << "矩阵减法运算错误:矩阵大小不一致" << std::endl; exit(1); } Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) - other(i, j); } } return result; } Matrix Matrix::operator*(const Matrix& other) const { if (cols != other.rows) { std::cerr << "矩阵乘法运算错误:矩阵大小不匹配" << std::endl; exit(1); } Matrix result(rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < other.cols; j++) { ComplexNum sum(0, 0); for (int k = 0; k < cols; k++) { sum = sum + (*this)(i, k) * other(k, j); } result(i, j) = sum; } } return result; } void Matrix::print() const { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { std::cout << (*this)(i, j) << " "; } std::cout << std::endl; } } ``` 最后在test.cpp中调用: ``` #include <iostream> #include "Matrix.h" int main() { // 创建4个复数对象 ComplexNum c1(2, 3), c2(4, 5), c3(6.7), c4(8, 9); // 为简化代码处理,构建复数数组 ComplexNum c[4] = { c1, c2, c3, c4 }; // 构建2*2的复数矩阵 Matrix m(2, 2); for (int i = 0; i < 2 * 2; i++) { m(i / 2, i % 2) = c[i]; } // 控制台打印矩阵 std::cout << "以下为复数矩阵:" << std::endl; m.print(); return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值