C/C++实现矩阵各种运算

程序很简单,重要的是二维指针的动态分配内存

type **Matrix ;//row为行,col为列
    Matrix=(type **) malloc(row*sizeof(type *)) ;
    for(int i=0; i<row; i++)
        Matrix[i] = (type *)malloc(col * sizeof(type ));

类似数组a[2][3],他一共两行,每一行有3个元素,所以为他先分配两行内存,然后每一行再分配3列内存

Matrix类型是type**,先为它分配row行内存,内存类型与Matrix一样
Matrix=(type**) malloc(rowsizeof(type))
每一行分配col列内存,内存类型与Matix[i]一样,也就是type*
Matrix[i] = (type*)malloc(col * sizeof(type ));

其中一个功能的结果:
在这里插入图片描述

下面是完整代码

#include <iostream>
#include <malloc.h>
#include <stdio.h>
using namespace std;

typedef struct
{
    //结构体
    int row,col;
    //二维指针,目的是动态分配内存
    float **matrix;
} Matrix;

typedef struct
{
    char *name;
    char *number;
} Student;

Matrix CreateMatrix()
{
    Matrix m;
    int row,col;
    cout << "输入行数与列数:" << endl;
    cin >> row >> col;
    float **enterMatrix ;
    enterMatrix=(float**) malloc(row*sizeof(float*)) ;
    for(int i=0; i<row; i++)
        enterMatrix[i] = (float *)malloc(col * sizeof(float));
    cout<<"输入你的矩阵:"<<endl;
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            cin >> enterMatrix[i][j];
        }
    }
    m.col = col;
    m.row = row;
    m.matrix = enterMatrix;
    return m;
}

//初始化一个行为row列为col矩阵
Matrix InitMatrix(int row,int col)
{
    Matrix m;
    float **matrix ;
    matrix=(float**) malloc(row*sizeof(float*)) ;
    for(int i=0; i<row; i++)
        matrix[i] = (float *)malloc(col * sizeof(float));
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        {
            matrix[i][j] = 0;
        }
    }
    m.col = col;
    m.row = row;
    m.matrix = matrix;
    return m;
}

Matrix add(Matrix m1, Matrix m2)
{
    for(int i=0; i<m1.row; i++)
    {
        for(int j=0; j<m1.col; j++)
        {
            m1.matrix[i][j] = m1.matrix[i][j] +m2.matrix[i][j];
        }
    }
    return m1;
}

Matrix sub(Matrix m1, Matrix m2)
{
    for(int i=0; i<m1.row; i++)
    {
        for(int j=0; j<m1.col; j++)
        {
            m1.matrix[i][j] = m1.matrix[i][j] -m2.matrix[i][j];
        }
    }
    return m1;
}

int calRowCol(Matrix M1,Matrix M2,int row,int col)//row为M1的行 col为m2的列
{
    int result = 0;
    int same = M1.col;
    for(int j=0; j<same; j++)
    {
        result+=M1.matrix[row][j]*M2.matrix[j][col];
    }

    return result;
}


Matrix Mul(Matrix m1, Matrix m2)
{
    Matrix result = InitMatrix(m1.row,m2.col);
    for(int i=0; i<m1.row; i++)
    {
        for(int j=0; j<m2.col; j++)
        {
            result.matrix[i][j] = calRowCol(m1,m2,i,j);
        }
    }
    return result;
}

Matrix numMul(Matrix m, int num)
{
    cout<<"数值:"<<num<<endl;
    for(int i=0; i<m.row; i++)
    {
        for(int j=0; j<m.col; j++)
        {
            m.matrix[i][j] = m.matrix[i][j]*num;
        }
    }
    return m;
}

Matrix printMatrix(Matrix m)
{
    for(int i=0; i<m.row; i++)
    {
        for(int j=0; j<m.col; j++)
        {
            cout << m.matrix[i][j] << "  ";
        }
        cout<<endl;
    }
}

int main()
{
    int num = 0;
    do
    {
        cout<<"*************************************\n";
        cout<<"*              菜单                 *\n";
        cout<<"*          1.矩阵相加               *\n";
        cout<<"*          2.矩阵相减               *\n";
        cout<<"*          3.矩阵相乘               *\n";
        cout<<"*          4.矩阵数乘               *\n";
        cout<<"*          5.退出                   *\n";
        cout<<"*************************************\n";
        cin>>num;
        if(1 == num|| 2 == num || 3 == num)
        {
            cout<<"请输入矩阵1"<<endl;
            Matrix m1 = CreateMatrix();
            cout<<"请输入矩阵2"<<endl;
            Matrix m2 = CreateMatrix();
            cout<<"两矩阵为"<<endl;
            printMatrix(m1);
            cout<<endl;
            printMatrix(m2);
            switch(num)
            {
            case 1:
            {
                if(m1.col!=m2.col || m1.row!=m2.row)
                {
                    cout<<"行列不同"<<endl;
                }
                else{
                    cout<<"结果为:"<<endl;
                    printMatrix(add(m1,m2));
                }
                break;
            }
            case 2:
            {

                if(m1.col!=m2.col || m1.row!=m2.row)
                {
                    cout<<"参数错误"<<endl;
                }
                else{
                    cout<<"结果为:"<<endl;
                    printMatrix(sub(m1,m2));
                }
                break;

            }
            case 3:
            {
                if(m1.col!=m2.row)
                {
                    cout<<"参数错误"<<endl;
                }
                else{
                    cout<<"结果为:"<<endl;
                    printMatrix(Mul(m1,m2));
                }
                break;
            }
            default:
                break;
            }
        }
        else if(4 == num)
        {
            int number = 1;
            cout<<"请输入矩阵"<<endl;
            Matrix m = CreateMatrix();
            cout<<"请输入数值"<<endl;
            cin>>number;
            cout<<"矩阵为:"<<endl;
            printMatrix(m);
            cout<<"数值为:"<<endl;
            cout<<number<<endl;
            printMatrix(numMul(m,number));
        }
        cout<<"按回车继续....";

        getchar();
        getchar();
        system("cls");
    }
    while(1 == num|| 2 == num || 3 == num ||4 == num);
    return 0;
}

  • 58
    点赞
  • 326
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
C++中,可以使用二维数组来表示矩阵,并实现矩阵的基本运算。下面是一个简单的示例代码,演示了如何实现矩阵的加、减、乘、转置和求逆等基本运算: ```c++ #include <iostream> #include <vector> using namespace std; // 矩阵加法 vector<vector<double>> matrix_addition(vector<vector<double>> A, vector<vector<double>> B) { int rows = A.size(); int cols = A[0].size(); vector<vector<double>> C(rows, vector<double>(cols)); for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { C[i][j] = A[i][j] + B[i][j]; } } return C; } // 矩阵减法 vector<vector<double>> matrix_subtraction(vector<vector<double>> A, vector<vector<double>> B) { int rows = A.size(); int cols = A[0].size(); vector<vector<double>> C(rows, vector<double>(cols)); for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { C[i][j] = A[i][j] - B[i][j]; } } return C; } // 矩阵乘法 vector<vector<double>> matrix_multiplication(vector<vector<double>> A, vector<vector<double>> B) { int rows_A = A.size(); int cols_A = A[0].size(); int rows_B = B.size(); int cols_B = B[0].size(); vector<vector<double>> C(rows_A, vector<double>(cols_B)); if(cols_A != rows_B) { cout<<"Error: Invalid matrix dimensions"<<endl; return C; } for(int i=0; i<rows_A; i++) { for(int j=0; j<cols_B; j++) { double sum = 0.0; for(int k=0; k<cols_A; k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } } return C; } // 矩阵转置 vector<vector<double>> matrix_transpose(vector<vector<double>> A) { int rows = A.size(); int cols = A[0].size(); vector<vector<double>> C(cols, vector<double>(rows)); for(int i=0; i<rows; i++) { for(int j=0; j<cols; j++) { C[j][i] = A[i][j]; } } return C; } // 矩阵求逆 vector<vector<double>> matrix_inverse(vector<vector<double>> A) { int n = A.size(); vector<vector<double>> B(n, vector<double>(n)); vector<vector<double>> C(n, vector<double>(n)); // 初始化单位矩阵 for(int i=0; i<n; i++) { B[i][i] = 1.0; } // 高斯-约旦消元法求逆矩阵 for(int i=0; i<n; i++) { double pivot = A[i][i]; if(pivot == 0.0) { cout<<"Error: The matrix is singular"<<endl; return C; } for(int j=0; j<n; j++) { A[i][j] /= pivot; B[i][j] /= pivot; } for(int j=0; j<n; j++) { if(j != i) { double ratio = A[j][i]; for(int k=0; k<n; k++) { A[j][k] -= ratio * A[i][k]; B[j][k] -= ratio * B[i][k]; } } } } return B; } int main() { // 定义两个矩阵 vector<vector<double>> A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; vector<vector<double>> B = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}}; // 矩阵加法 vector<vector<double>> C = matrix_addition(A, B); cout<<"A + B = "<<endl; for(int i=0; i<C.size(); i++) { for(int j=0; j<C[0].size(); j++) { cout<<C[i][j]<<" "; } cout<<endl; } // 矩阵减法 C = matrix_subtraction(A, B); cout<<"A - B = "<<endl; for(int i=0; i<C.size(); i++) { for(int j=0; j<C[0].size(); j++) { cout<<C[i][j]<<" "; } cout<<endl; } // 矩阵乘法 C = matrix_multiplication(A, B); cout<<"A * B = "<<endl; for(int i=0; i<C.size(); i++) { for(int j=0; j<C[0].size(); j++) { cout<<C[i][j]<<" "; } cout<<endl; } // 矩阵转置 C = matrix_transpose(A); cout<<"A' = "<<endl; for(int i=0; i<C.size(); i++) { for(int j=0; j<C[0].size(); j++) { cout<<C[i][j]<<" "; } cout<<endl; } // 矩阵求逆 C = matrix_inverse(A); cout<<"inv(A) = "<<endl; for(int i=0; i<C.size(); i++) { for(int j=0; j<C[0].size(); j++) { cout<<C[i][j]<<" "; } cout<<endl; } return 0; } ``` 在上面的代码中,我们定义了一个名为`matrix_addition`的函数,用于实现矩阵加法;定义了一个名为`matrix_subtraction`的函数,用于实现矩阵减法;定义了一个名为`matrix_multiplication`的函数,用于实现矩阵乘法;定义了一个名为`matrix_transpose`的函数,用于实现矩阵转置;定义了一个名为`matrix_inverse`的函数,用于实现矩阵求逆。在`main`函数中,我们定义了两个矩阵A和B,并演示了如何调用上述函数实现矩阵的基本运算

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值