- 1
- 0
- 约7.85千字
- 约 19页
- 2020-12-30 发布于浙江
- 举报
Matrix矩阵类
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
__________________________________________________
/Matrix.h
//矩阵类定义
#ifndef?MATRIX_H
#define?MATRIX_H
#include?iostream
#include?string
//using?namespace?std;?一般头文件中使用完全限定域名字,而不是包含命名空间,防止重复包含头文件时造成的资源浪费
class?Matrix?
{
????//从流中读入矩阵
????friend?std::istream?operator??(std::istream?is,?Matrix?A);
????//输出矩阵
????friend?std::ostream?operator??(std::ostream?os,?const?Matrix?A);
????//将矩阵输出到名为str的文件中
????friend?void?print_file(const?MatrixA,const?char*?str);
public:
????//定义空矩阵
????Matrix()?{elems?=?NULL;?row?=?0;?col?=?0;};
????//定义m*n零矩阵
????Matrix(int?m,?int?n);
????//定义m*n矩阵,由a初始化
????Matrix(int?m,?int?n,?double?*a,?int?size?=?0);????
????//复制构造函数
????Matrix(const?Matrix?B);
????//从文件str中读取矩阵
????Matrix(const?char*?str);
????~Matrix()?{delete[]elems;?row?=?0;?col?=?0;????};
????
????//重载算数操作符
????Matrix?operator?=?(Matrix?B);
????Matrix?operator?+(const?MatrixB)const;
????Matrix?operator?-(const?MatrixB)const;
????Matrix?operator?*(const?MatrixB)const;
????//返回矩阵第i行第j列元素
????double?operator()(int?i,int?j)const;
????
????double?get_row()const?{return?row;};
????double?get_col()const?{return?col;};
????
????//矩阵转置
????Matrix?trans()const;
????
protected:
private:
????double*?elems;
????int?row,?col;
};
#endif
??1//Matrix.cpp
??2//函数实现
??3
??4#include?Matrix.h
??5#include?iostream
??6#include?fstream
??7#include?sstream
??8#include?string
??9#include?stdexcept
?10
?11using?namespace?std;
?12
?13//重载下标操作符,返回A[i,j]
?14double?Matrix::operator()(int?i,int?j)const
?15{
?16????if(i0?||?i?=?row?||?j??0?||?j?=?col)
?17????????throw?out_of_range(The?suffix?is?out?of?range);
?18????
?19????return?elems[i*col+j];
?20}
?21
?22//从输入流中读入矩阵
?23istream?operator?(istream?is,?MatrixA)
?24{
?25????for(int?i?=?0;?i?!=?A.get_row(
原创力文档

文档评论(0)