问题描述
最近需要计算一些旋转矩阵,手算太麻烦,于是写了个小软件
参考
C/C++实现矩阵各种运算
Adjoint and Inverse of a Matrix
实现
Vecace博主的C/C++实现矩阵各种运算已经实现了诸如“加、减、乘、数乘”等大部分功能,在此基础上增加求矩阵的逆
的功能,具体如下:
#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;
}
Matrix Create2DArrayDefault(int row_col)
{
Matrix m;
float** enterMatrix;
enterMatrix = (float**)malloc(row_col * sizeof(float*));
for (int i = 0; i < row_col; i++)
enterMatrix[i] = (float*)malloc(row_col * sizeof(float));
for (int i = 0; i < row_col; i++)
{
for (int j = 0; j < row_col; j++)
{
enterMatrix[i][j] = 0;
}
}
m.col = row_col;
m.row = row_col;
m.matrix = enterMatrix;
return m;
}
// Function to get cofactor of A[p][q] in temp[][]. n is current
// dimension of A[][]
void getCofactor(Matrix A, Matrix temp, int p, int q, int n)
{
int i = 0, j = 0;
// Looping for each element of the matrix
for (int row =