资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
矩阵的转置和乘法课程设计程序#ifndef CMatrix_H_/*条件编译#define CMatrix_H_#include#include#includevec.h/using namespace std;#define MIN(a,b) (a)(b)?(a):(b);/*-定义类模板-*/template class CMatrixstruct nodeVector *f;/*组成矩阵的向量指针int refcnt;/*被引用次数int length;/*矩阵的行数T *tmppointer;/*头指针类型 *p;public: / Vector * begin() const return p-f;CMatrix();/*默认的构造CMatrix(int xsize,int ysize,T init=0);/*构造函数CMatrix(int xlength,const Vector *vec);/*构造函数CMatrix(CMatrix &x); /*拷贝构造函数CMatrix(); /*析构函数CMatrix & operator=(const CMatrix &mat);/*重载赋值运算符 int row() const;/*返回行数int col() const;/*返回列数Vector & operator ( int i);/*重载 void Inver(CMatrix &mat);/*矩阵转置operator T *();/*重载*void ReadFromFile();/*从文件中读入矩阵friend CMatrix cpy(CMatrix &v);/*拷贝函数friend std:ostream & operator(std:ostream &s,const CMatrix &mat);/重载输出函数friend std:istream & operator(std:istream &s,const CMatrix &mat);/重载输入函数 friend CMatrix operator*(CMatrix &v1,CMatrix &v2);/*矩阵乘法friend CMatrix operator*(const CMatrix &v,T val); /*数乘;/*-类外定义缺省的构造函数-*/template CMatrix:CMatrix()p=new node;p-length=NULL;p-f=0;p-refcnt=1;p-tmppointer=NULL;/*-定义可扩展构造函数-*/template CMatrix:CMatrix(int xsize,int ysize,T init)if(xsize=0|ysize=0) coutlength=xsize;p-f=new Vector *xsize;for(int i(0);ifi=new Vector(ysize,init);p-refcnt=1;p-tmppointer=NULL;/*-定义构造函数-*/template CMatrix:CMatrix(int xlength,const Vector *vec) if(xlength=0) coutlength=xlength; p-f=new Vector *xlength;for(int i(0);ifi=new Vector(*vec);/*-定义拷贝的构造函数-*/template CMatrix:CMatrix(CMatrix &x)x.p-refcnt+; p=x.p;template CMatrix cpy(CMatrix &v) int mr=v.row();int mc=v.col();CMatrix x(mr,mc);for(int i(0);ifi)=*(v.p-fi);return x;/*-定义析构函数-*/template CMatrix:CMatrix()if(-p-refcnt=0)if(p-f!=NULL)int len=p-length;for(int i(0);ifi;if(p-tmppointer!=NULL)delete p-tmppointer;delete p-f; /*-定义函数返回行数-*/template int CMatrix:row() constreturn p-length;/*-定义函数返回列数-*/template int CMatrix:col() constreturn p-f0-dim();/*-定义转置的函数-*/template void Inver(CMatrix &mat)int m = mat.row();int n = mat.col();CMatrix tmp(n,m);int i, j;for(i=0; in; i+)for(j=0; jm; j+)tmpij=matji;mat=tmp;/*-定义重载函数重载赋值操作符号=-*/template CMatrix & CMatrix:operator=(const CMatrix &vec)vec.p-refcnt+;if(-p-refcnt=0)int len=p-length;for(int i(0);ifi;delete p-f;if(p-tmppointer!=NULL)delete p-tmppointer;delete p;p=vec.p;return *this;/*-定义重载函数重载-*/template Vector &CMatrix:operator(int i)if(i=0)&(ilength)return *p-fi;elsecouterrorf0;/*-定义重载函数重载*-
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号