用c++的动态数组写一个矩阵计算器,用菜单系统,根据用户输入的行和列进行矩阵的加,减,乘,除,求逆,计算矩阵的秩,行列式,特征值和特征向量,并实现文件的输入输出。
时间: 2024-06-13 14:07:37 浏览: 111
矩阵计算器,C++实现
5星 · 资源好评率100%
以下是一个用C++动态数组实现的矩阵计算器,支持菜单系统,根据用户输入的行和列进行矩阵的加、减、乘、除、求逆、计算矩阵的秩、行列式、特征值和特征向量,并实现文件的输入输出。
```c++
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
class Matrix {
private:
int row, col;
double **data;
public:
Matrix(int r, int c) {
row = r;
col = c;
data = new double *[row];
for (int i = 0; i < row; i++) {
data[i] = new double[col];
for (int j = 0; j < col; j++) {
data[i][j] = 0;
}
}
}
Matrix(const Matrix &m) {
row = m.row;
col = m.col;
data = new double *[row];
for (int i = 0; i < row; i++) {
data[i] = new double[col];
for (int j = 0; j < col; j++) {
data[i][j] = m.data[i][j];
}
}
}
~Matrix() {
for (int i = 0; i < row; i++) {
delete[] data[i];
}
delete[] data;
}
void input() {
cout << "请输入矩阵的元素:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> data[i][j];
}
}
}
void output() {
cout << "矩阵的元素为:" << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << data[i][j] << " ";
}
cout << endl;
}
}
Matrix operator+(const Matrix &m) const {
Matrix result(row, col);
if (row != m.row || col != m.col) {
cout << "矩阵维度不匹配,无法相加!" << endl;
return result;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] + m.data[i][j];
}
}
return result;
}
Matrix operator-(const Matrix &m) const {
Matrix result(row, col);
if (row != m.row || col != m.col) {
cout << "矩阵维度不匹配,无法相减!" << endl;
return result;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] - m.data[i][j];
}
}
return result;
}
Matrix operator*(const Matrix &m) const {
Matrix result(row, m.col);
if (col != m.row) {
cout << "矩阵维度不匹配,无法相乘!" << endl;
return result;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < m.col; j++) {
for (int k = 0; k < col; k++) {
result.data[i][j] += data[i][k] * m.data[k][j];
}
}
}
return result;
}
Matrix operator/(const Matrix &m) const {
Matrix result(row, col);
if (row != m.row || col != m.col) {
cout << "矩阵维度不匹配,无法相除!" << endl;
return result;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[i][j] = data[i][j] / m.data[i][j];
}
}
return result;
}
Matrix transpose() const {
Matrix result(col, row);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result.data[j][i] = data[i][j];
}
}
return result;
}
double determinant() const {
if (row != col) {
cout << "矩阵不是方阵,无法求行列式!" << endl;
return 0;
}
double det = 0;
if (row == 1) {
det = data[0][0];
} else if (row == 2) {
det = data[0][0] * data[1][1] - data[0][1] * data[1][0];
} else {
for (int i = 0; i < row; i++) {
Matrix sub(row - 1, col - 1);
for (int j = 1; j < row; j++) {
for (int k = 0; k < col; k++) {
if (k < i) {
sub.data[j - 1][k] = data[j][k];
} else if (k > i) {
sub.data[j - 1][k - 1] = data[j][k];
}
}
}
det += pow(-1, i) * data[0][i] * sub.determinant();
}
}
return det;
}
Matrix inverse() const {
if (row != col) {
cout << "矩阵不是方阵,无法求逆矩阵!" << endl;
return Matrix(0, 0);
}
double det = determinant();
if (det == 0) {
cout << "矩阵的行列式为0,无法求逆矩阵!" << endl;
return Matrix(0, 0);
}
Matrix result(row, col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
Matrix sub(row - 1, col - 1);
for (int k = 0; k < row; k++) {
if (k == i) {
continue;
}
for (int l = 0; l < col; l++) {
if (l == j) {
continue;
}
if (k < i && l < j) {
sub.data[k][l] = data[k][l];
} else if (k < i && l > j) {
sub.data[k][l - 1] = data[k][l];
} else if (k > i && l < j) {
sub.data[k - 1][l] = data[k][l];
} else {
sub.data[k - 1][l - 1] = data[k][l];
}
}
}
result.data[j][i] = pow(-1, i + j) * sub.determinant() / det;
}
}
return result;
}
int rank() const {
Matrix m(*this);
int r = 0;
for (int i = 0; i < col; i++) {
if (r == row) {
break;
}
int j = r;
while (j < row && m.data[j][i] == 0) {
j++;
}
if (j == row) {
continue;
}
if (j != r) {
swap(m.data[j], m.data[r]);
}
double t = m.data[r][i];
for (int k = i; k < col; k++) {
m.data[r][k] /= t;
}
for (int k = r + 1; k < row; k++) {
double t = m.data[k][i];
for (int l = i; l < col; l++) {
m.data[k][l] -= t * m.data[r][l];
}
}
r++;
}
return r;
}
void eigen() const {
if (row != col) {
cout << "矩阵不是方阵,无法求特征值和特征向量!" << endl;
return;
}
Matrix A(*this);
Matrix Q(row, col);
for (int i = 0; i < row; i++) {
Q.data[i][i] = 1;
}
for (int k = 0; k < 100; k++) {
double s = 0;
for (int i = 0; i < row; i++) {
for (int j = i + 1; j < col; j++) {
s += A.data[i][j] * A.data[i][j];
}
}
if (s == 0) {
break;
}
double t = (A.data[row - 1][row - 1] - A.data[0][0]) / (2 * A.data[0][row - 1]);
double mu = A.data[0][0] - t;
double x = 1 / sqrt(1 + t * t);
double y = x * t;
Matrix G(row, col);
for (int i = 0; i < row; i++) {
G.data[i][i] = 1;
}
G.data[0][0] = x;
G.data[row - 1][row - 1] = x;
G.data[0][row - 1] = y;
G.data[row - 1][0] = -y;
A = G.transpose() * A * G;
Q = Q * G;
}
cout << "特征值为:" << endl;
for (int i = 0; i < row; i++) {
cout << A.data[i][i] << " ";
}
cout << endl;
cout << "特征向量为:" << endl;
for (int i = 0; i < row; i++) {
cout << "第" << i + 1 << "个特征向量为:";
for (int j = 0; j < col; j++) {
cout << Q.data[j][i] << " ";
}
cout << endl;
}
}
void save(const char *filename) const {
ofstream fout(filename);
fout << row << " " << col << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
fout << data[i][j] << " ";
}
fout << endl;
}
fout.close();
}
void load(const char *filename) {
ifstream fin(filename);
fin >> row >> col;
data = new double *[row];
for (int i = 0; i < row; i++) {
data[i] = new double[col];
for (int j = 0; j < col; j++) {
fin >> data[i][j];
}
}
fin.close();
}
};
int main() {
int row, col;
cout << "请输入矩阵的行数和列数:" << endl;
cin >> row >> col;
Matrix m(row, col);
m.input();
while (true) {
cout << "请选择要进行的操作:" << endl;
cout << "1. 矩阵加法" << endl;
cout << "2. 矩阵减法" << endl;
cout << "3. 矩阵乘法" << endl;
cout << "4. 矩阵除法" << endl;
cout << "5. 矩阵转置" << endl;
cout << "6. 求矩阵的行列式" << endl;
cout << "7. 求矩阵的逆矩阵" << endl;
cout << "8. 求矩阵的秩" << endl;
cout << "9. 求矩阵的特征值和特征向量" << endl;
cout << "10. 保存矩阵到文件" << endl;
cout << "11. 从文件中读取矩阵" << endl;
cout << "12. 退出" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1
阅读全文