【C++】两个使用类来实现矩阵运算的例子

本文介绍了如何在C++中创建复数类Complex和矩阵类Matrix,包括它们的加法、减法、乘法、除法以及转置功能,并通过主函数展示了如何实例化和使用这些类进行操作。
摘要由CSDN通过智能技术生成

1.建立一个复数类 complex,使其具有加法、减法、乘法和除法的功能,并编写
主函数,实现复数的加法、减法、乘法和除法,显示输出结果。


#include <iostream>
using namespace std;

class Complex {
private:
    double real;
    double imag;

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

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

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

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

    Complex operator/(const Complex& c) {
        double denom = c.real * c.real + c.imag * c.imag;
        double realPart = (real * c.real + imag * c.imag) / denom;
        double imagPart = (imag * c.real - real * c.imag) / denom;
        return Complex(realPart, imagPart);
    }

    friend ostream& operator<<(ostream& output, const Complex& c) {
        output << c.real << " + " << c.imag << "i";
        return output;
    }
};

int main() {
    int x1, x2, y1, y2;
    cout << "请输入复数实部:";
    cin >> x1 >> x2;
    cout << "请输入复数虚部:";
    cin >> y1 >> y2;
    Complex c1(x1, x2);
    Complex c2(y1, y2);
    cout << "=======================" << endl;
    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    cout << "c1 * c2 = " << c1 * c2 << endl;
    cout << "c1 / c2 = " << c1 / c2 << endl;

    return 0;
}

 

2.写一个矩阵类 Matrix,使其至少具有加法、乘法、转置、显示等功能,编写主
函数,实现矩阵的加法、乘法、转置,并显示结果.



#include <iostream>
using namespace std;
class Matrix {
public:
    int** data;
    int rows;
    int cols;
    Matrix(int m, int n) : rows(m), cols(n) {
        data = new int* [rows];
        for (int i = 0; i < rows; ++i) {
            data[i] = new int[cols];
        }
    }

    Matrix operator+(const Matrix& other) {
        Matrix result(rows, cols);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                result.data[i][j] = data[i][j] + other.data[i][j];
            }
        }
        return result;
    }

    Matrix operator*(const Matrix& other) {
        Matrix result(rows, other.cols);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < other.cols; ++j) {
                int sum = 0;
                for (int k = 0; k < cols; ++k) {
                    sum += data[i][k] * other.data[k][j];
                }
                result.data[i][j] = sum;
            }
        }
        return result;
    }

    Matrix transpose() {
        Matrix result(cols, rows);
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                result.data[j][i] = data[i][j];
            }
        }
        return result;
    }

    void display() {
        for (int i = 0; i < rows; ++i) {
            for (int j = 0; j < cols; ++j) {
                std::cout << data[i][j] << " ";
            }
            std::cout << std::endl;
        }
    }

    ~Matrix() {
        for (int i = 0; i < rows; ++i) {
            delete[] data[i];
        }
        delete[] data;
    }
};

int main() {
    int i1, i2, j1, j2;
    cout << "第一个矩阵大小(行 x 列):";
    cin >> i1 >> j1;
    Matrix m1(i1, j1);
    cout << "该矩阵为:";
    for (int i = 0; i < i1; i++)
    {
        for (int j = 0; j < j1; j++)
        {
            cin>>m1.data[i][j];
        }
    }

    cout << "第二个矩阵大小(行 x 列):";
    cin >> i2 >> j2;
    Matrix m2(i2, j2);
    cout << "该矩阵为:";
    for (int i = 0; i < i2; i++)
    {
        for (int j = 0; j < j2; j++)
        {
            cin >> m2.data[i][j];
        }
    }

    cout << "========================" << endl;
    cout << "第一个矩阵为:" << endl;
    m1.display();
    cout << "========================" << endl;
    cout << "第二个矩阵为:" << endl;
    m2.display();
    cout << "========================" << endl;
    Matrix sum = m1 + m2;
    std::cout << "进行加法操作:" << std::endl;
    sum.display();
    cout << "========================" << endl;
    Matrix product = m1 * m2;
    std::cout << "进行乘法操作:" << std::endl;
    product.display();
    cout << "========================" << endl;
    Matrix transpose = m1.transpose();
    std::cout << "进行转置操作:" << std::endl;
    transpose.display();
    return 0;
}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值