ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:17.66KB ,
资源ID:7859381      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/7859381.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(矩阵的转置和乘法课程设计程序.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

矩阵的转置和乘法课程设计程序.docx

1、矩阵的转置和乘法课程设计程序矩阵的转置和乘法课程设计程序#ifndef CMatrix_H_/*条件编译#define CMatrix_H_#include#include#includevec.h/using namespace std;#define MIN(a,b) (a)(b)?(a):(b);/*-定义类模板-*/template class CMatrix struct node Vector *f;/*组成矩阵的向量指针 int refcnt;/*被引用次数 int length;/*矩阵的行数 T *tmppointer;/*头指针类型 *p; public: / Vector

2、 * 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

3、( 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 ope

4、rator*(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

5、=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:CMatr

6、ix(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-tmppoint

7、er; delete p-f; /*-定义函数返回行数-*/template int CMatrix:row() const return p-length;/*-定义函数返回列数-*/template int CMatrix:col() const return 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=matj

8、i; 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) i

9、f(i=0)&(ilength) return *p-fi; else couterrorf0; /*-定义重载函数重载*-*/templateCMatrix:operator T *() if(p-tmppointer=NULL) int n=row(); p-tmppointer=new T *n; for(int i(0);itmppointeri=p-fi-begin(); return p-tmppointer;template void CMatrix:ReadFromFile()/*从文件中读入矩阵 char filename256; cinfilename; ifstream

10、infile;/ cout*; int row,col; infile.open(filename,ios:in); if(!infile) cout不能打开输入文件!rowcol; CMatrix v1(row,col,0);/ infilev100;/ coutv100*endl; for(int i(0);irow;i+) for(int j(0);jv1ij; *this=v1;/*-定义函数重载输出-*/template std:ostream & operator(std:ostream & os,CMatrix &v1)/ osendl; Vector *f=v1.begin()

11、;/ coutv1.begin()*&*endl; int len=v1.row(); for(int i(0);ilen;i+) os*fin; return os;/*-定义函数重载输入-*/template std:istream & operator(std:istream & is,CMatrix &v1) int row,col; coutrowcol; CMatrix x(row,col,0); cout请输入row*col矩阵n; for(int i(0);irow;i+) for(int j(0);jxij; v1=x; return is;/*-定义重载函数重载乘法*-*/

12、template CMatrix operator*(CMatrix &m1,CMatrix &m2) int i,j; int m1rows=m1.row(); int m1cols=m1.col(); int m2rows=m2.row(); int m2cols=m2.col(); if(m1cols!=m2rows) couterror!endl; CMatrix v(m1rows,m2cols); CMatrix flip(m2cols,m2rows); for(i=0;im2rows;i+) for(j=0;jm2cols;j+) flipji=m2ij; for(i=0;im1r

13、ows;i+) for(j=0;jm2cols;j+) vij=dot_prod(m1i,flipj); return v;/*-定义函数重载数乘(整型,双精度型)-*/CMatrix operator*(const CMatrix &v,int val) CMatrix temp; temp=v; for(int i(0);ilength;i+) *(temp.p-fi)=*(v.p-fi)*val; return temp;CMatrix operator*(const CMatrix &v,double val) CMatrix temp; temp=v; for(int i(0);il

14、ength;i+) *(temp.p-fi)=*(v.p-fi)*val; return temp;#endif/*-定义几个选择函数-*/void choiceid();/*选择输入矩阵的类型void processint();/*选择输入矩阵的饿方式void processdouble();/*选择输入矩阵的方式templatevoid process(CMatrix &cm,CMatrix &cm1,CMatrix &cm2);void main() cout!欢迎您进入并使用矩阵转置和乘法程序!n; coutt(请您注意本程序对您输入的矩阵的项数不等于ntt您事先设定的矩阵项数时无法识

15、别,请您见量!)nn;choiceid(); /*-定义选择函数-*/void choiceid() coutchoice; switch(choice) case 1: processint(); break; case 2: processdouble(); break; default: break; void processint() CMatrix icm(2,2,0),icm1,icm2; coutchoice) switch(choice) case 1: couticm1; couticm2; process(icm,icm1,icm2); break; case 2: cou

16、t输入矩阵1的路径:; icm1.ReadFromFile(); cout输入矩阵2的路径:; icm2.ReadFromFile(); process(icm,icm1,icm2); break; default: break; void processdouble() CMatrix icm,icm1,icm2; coutchoice) switch(choice) case 1: couticm1; couticm2; process(icm,icm1,icm2); break; case 2: cout输入矩阵1的路径:; icm1.ReadFromFile(); cout输入矩阵2的

17、路径:; icm2.ReadFromFile(); process(icm,icm1,icm2); break; default: break; templatevoid process(CMatrix &cm,CMatrix &cm1,CMatrix &cm2) int choice; double val; coutchoice) switch(choice) case 1: cm=cm1*cm2; cout两矩阵相乘的结果为:ncmendl; coutchoice; if(choice=1)cout谢谢您的使用!再见!n;exit(0); if(choice=2) cout请您选择对矩阵的操作类型:n1.两矩阵相乘n2.矩阵数乘n3.矩阵转置n其他键退出n; continue; ;break; case 2: coutval; coutchoice; switch(choice) case 1: cm=cm1*val; cout矩阵1:nn乘以数val的结果为:ncmendl; coutchoice; if(choice=

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1