- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
/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()
//矩阵转置
Matrix trans()const;
protected: private:
double* elems; int row, col;
};
#endif
//函数实现3
#include Matrix.h
#include iostream
#include fstream
#include sstream
#include string
#include stdexcept 10
11 using namespace std; 12
//重载下标操作符,返回 A[i,j]
double 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
//从输入流中读入矩阵
istream operator (istream is, MatrixA) 24 {
for(int i = 0; i != A.get_row(); ++i)
for(int j = 0; j != A.get_col(); ++j) 27 is A(i,j);
28 return is; 29 }
30
//输出矩阵
ostream operator (ostream os, const Matrix A) 33 {
545556{for(int j = 0; j!= A.col; ++j)outfile A(i,j);5758outfile endl;
54
55
56
{
for(int j = 0; j!= A.col; ++j)
outfile A(i,j);
57
58
outfile endl;
}
3738os A(i,j) ;cout endl;394041}cout ------------------------
37
38
os A(i,j) ;
cout endl;
39
40
41
}
cout ------------------------ endl;
4243return os;
42
43
return os;
45
//将矩阵 A 输出到文件 str 中
48 {495051o
原创力文档


文档评论(0)