资源描述
C++进行矩阵运算类库
头文件:Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include "iostream"
#include "string"
#include <stdlib.h>
#include <vector>
#include <fstream>
#include<sstream> //istringstream 必须包含这个头文件
using namespace std;
class Matrix
{
public:
void readMatrix(string fileName);
void showMatrix();
void writeMatrix(string str);
void inversion();//求矩阵的逆矩阵
void change();//求矩阵的转置
void operator +(Matrix &a);//声明重载运算符的“+”函数
void operator -(Matrix &a);//声明重载运算符的“-”函数
void operator *(Matrix &a);//声明重载运算符的“*”函数
void operator =(Matrix &a);//声明重载运算符的“=”赋值函数
vector<vector<double>> _mat;
int row;
int col;
};
#endif
源文件:main.cpp
#include "iostream"
#include "string"
#include <stdlib.h>
#include <vector>
#include <fstream>
#include<sstream> //istringstream 必须包含这个头文件
#include "Matrix.h"
#include <time.h> //使用随机数
#include <cmath> //数学计算
using namespace std;
//将运算得到的数据存储到box中,形成一个完整的Matrix量
Matrix box_mul;//矩阵相乘的结果
Matrix box_inver;//矩阵的逆的结果
Matrix box_change;//矩阵转置的结果
Matrix box_plus;//矩阵加的结果
Matrix box_cut;//矩阵减的结果
void Matrix::readMatrix(string fileName)
{
ifstream ifile(fileName.c_str(),ios::in);/*声明一个输入句柄,打开fileName所指文件。
接下来就可以通过ifile>>***来读取1.txt中的内容了*/
double tmp;
string line;
vector<double> row;//vector容器,相当于一个数组
while(getline(ifile,line))
{
istringstream istr(line);
while(istr>>tmp)
{
row.push_back(tmp);//在row尾部插入读入的数据
}
this->col = row.size();
_mat.push_back(row);
row.clear();
istr.clear();
line.clear();
}
this->row = _mat.size();//这里的row不是vector变量,是matrix中的row
ifile.close();
}
void Matrix::showMatrix()
{
cout<<"矩阵:"<<endl;
//cout<<"row="<<row<<endl;
//cout<<"col="<<col<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
cout<<_mat[i][j]<<" ";
cout<<endl;
}
}
void Matrix::writeMatrix(string str)
{
ofstream os(str,ios::trunc);
//ofstream os("C:\\Users\\Administrator\\Desktop\\数据矩阵W.txt",ios::trunc);
if (os)
{
for (int i=0;i<row;++i)
{
for (int j=0;j<col;++j)
os<< _mat[i][j]<<" ";
os<<endl;
}
}
else
cerr<<"无法打开文件!"<<endl;
}
void Matrix::operator+(Matrix &a)
{
//cout<<"矩阵的和:"<<endl;
double b[200][200];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
b[i][j]=_mat[i][j]+a._mat[i][j];
}
box_plus.col=col;box_plus.row=row;//行列数设置好
box_plus._mat.clear();
vector <double> t;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
t.push_back(b[i][j]);
}
box_plus._mat.push_back(t);//数据输入_mat量中
t.clear();
}
}
void Matrix::operator-(Matrix &a)
{
//cout<<"矩阵的差:"<<endl;
double b[200][200];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
b[i][j]=_mat[i][j]-a._mat[i][j];
}
box_cut.col=col;box_cut.row=row;//行列数设置好
box_cut._mat.clear();
vector <double> t;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
t.push_back(b[i][j]);
}
box_cut._mat.push_back(t);//数据输入_mat量中
t.clear();
}
}
void Matrix::operator*(Matrix &a) //可连续使用
{
//cout<<"矩阵相乘:"<<endl;
double b[200][200];
for(int i=0;i<row;i++)
{
for(int j=0;j<a.col;j++)
{
b[i][j]=0;
for(int k=0;k<col;k++)
{
b[i][j]+=_mat[i][k]*a._mat[k][j];
}
}
}
box_mul.col=a.col;
box_mul.row=row;//行列数设置好
box_mul._mat.clear();
vector <double> t;
for(int i=0;i<row;i++)
{
for(int j=0;j<a.col;j++)
{
t.push_back( b[i][j]);
}
box_mul._mat.push_back(t);//数据输入_mat量中
t.clear();
}
}
void Matrix::operator=(Matrix &a)
{
row=a.row;col=a.col;
_mat.clear();
vector <double> t;
for (int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
t.push_back(a._mat[i][j]);
}
_mat.push_back(t);
t.clear();
}
}
void Matrix::change() //可连续使用
{
//cout<<"矩阵的转置:"<<endl;
double b[200][200];
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
b[j][i]=_mat[i][j];
}
box_change.col=row;box_change.row=col;//行列数设置好
box_change._mat.clear();
vector <double> t;
for(int i=0;i<col;i++)
{
for(int j=0;j<row;j++)
{
t.push_back( b[i][j]);
}
box_change._mat.push_back(t);//数据输入_mat量中
t.clear();
}
}
void Matrix::inversion() //可连续使用
{
//cout<<"矩阵的逆:"<<endl;
int i,j,k,M=this->col,N=2*this->col;
double b[200][200];
for(i=0;i<M;i++) //赋值
{
for(j=0;j<M;j++)
b[i][j]=_mat[i][j]; }
for(i=0;i<M;i++) //扩展
for(j=M;j<N;j++)
{
if(i==(j-M))
b[i][j]=1;
else
b[i][j]=0;
}
for(i=0;i<M;i++)
{
if(b[i][i]==0)
{
for(k=i;k<M;k++)
{
if(b[k][i]!=0)
{
for(int j=0;j<N;j++)
{
double temp;
temp=b[i][j];
b[i][j]=b[k][j];
b[k][j]=temp;
}
break;
}
}
if(k==M)
{
cout<<"该矩阵不可逆!\n";
exit(0);
}
}
for(j=N-1;j>=i;j--)
b[i][j]/=b[i][i];
for(k=0;k<M;k++)
{
if(k!=i)
{
double temp=b[k][i];
for(j=0;j<N;j++)
b[k][j]-=temp*b[i][j];
}
}
}
box_inver.col=col;box_inver.row=row;//行列数设置好
box_inver._mat.clear();
vector <double> t;
for(int i=0;i<M;i++)
{
for(int j=M;j<N;j++)
{
//cout<<b[i][j]<<endl;
t.push_back( b[i][j]);
}
box_inver._mat.push_back(t);//数据输入_mat量中
t.clear();
}
}
int main()
{
///////////////////////////////
system("pause");
return 0;
}
展开阅读全文