矩阵的转置和乘法课程设计程序(20200910104113)_第1页
矩阵的转置和乘法课程设计程序(20200910104113)_第2页
矩阵的转置和乘法课程设计程序(20200910104113)_第3页
矩阵的转置和乘法课程设计程序(20200910104113)_第4页
矩阵的转置和乘法课程设计程序(20200910104113)_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、矩阵的转置和乘法课程设计程序#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;/*组成矩阵的向量指针intrefcnt;/*被引用次intlength;/*矩阵的行数*tmppointer;/*头指针类 *p; public: /Vector * begin() const return p-

2、f;CMatrix();/* 认的构造造函数CMatrix(int xsize,int ysize,T init=0);/*CMatrix(int xlength,const V ector *vec);/*CMatrix(CMatrix &x); /* 拷贝构造函数CMatrix();/*析构函数 CMatrix & operator=(const CMatrix &mat);/* 载赋值运算符introw() const;/*回行数intcol() const;/*回列数Vectoroperator重载 (构造函inti);/*&mat);/*/* 定义构造函数 */阵转置operator

3、 T *();/*重载 *void ReadFromFile();/*文件中读入矩阵friendCMatrixcpy(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);/* 矩阵乘法friendCMatrixoperator

4、*(const CMatrix&v,Tval);/*1数乘;/*类外定义缺省的构造函数 */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

5、-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

6、=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:c

7、ol() 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=matji; mat=tmp;/* 定义重载函数重载赋值操作符号 =*/template CMatrix & CMatrix:operator=(const CMatrix &vec) vec.p-refcnt+;if(-p-refcnt=0)int

8、 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;/* 定义重载函数重载 */ template CMatrix:operator T *() if(p-tmppointer=NULL) int n=row();p

9、-tmppointer=new T *n;for(int i(0);itmppointeri=p-fi-begin();return p-tmppointer;template void CMatrix:ReadFromFile()/*从文件中读入矩阵char filename256;cinfilename;ifstream infile;/ cout*;int row,col;infile.open(filename,ios:in);if(!infile)cout 不能打开输入文件 !rowcol;CMatrix v1(row,col,0);/ infilev100;/ coutv100*e

10、ndl;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();/ coutv1.begin()*&*endl;int len=v1.row();for(int i(0);ilen;i+) os*fin;return os;/* 定义函数重载输入 */template std:istream & operator(std:istream & is

11、,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;/* 定义重载函数重载乘法 */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

12、!=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;im1rows;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)

13、;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);ilength;i+)*(temp.p-fi)=*(v.p-fi)*val;return temp;#endif template/*/定义几个选择函void choiceid();/*void processint();/*void processdouble();/*选择输入矩阵的类型 选择输入矩阵的饿方式 选择输入矩阵的方式void

14、 process(CMatrix &cm,CMatrix &cm1,CMatrix &cm2);void main()cout! 欢迎您进入并使用矩阵转置和乘法程序 !n;coutt (请您注意本程序对您输入的矩阵的项数不等于ntt您事先设定的矩阵项数时无法识别,请您见量 !)nn;choiceid();/* 定 义 选 择 函*/void choiceid()coutchoice;switch(choice)case 1:processint();break;case 2:processdouble();break;default:break;void processint()CMatrix

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

16、e)case 1:couticm1;couticm2;process(icm,icm1,icm2);break;case 2:cout 输入矩阵 1 的路径 :;icm1.ReadFromFile();cout 输入矩阵 2 的路径 :;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=

17、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=1)cout 谢谢您的使用!再见! n; exit(0); if(choice=2)cout请您选择对矩阵的操作类型:n1.两矩阵相乘n2.矩阵数乘n3.矩阵转置 n 其他键退出 n;continue;break;case 2:cm=cm2*val;cout矩阵 2:nn 乘以数val的结果为:ncmendl;coutchoice;if(choice=1)cout 谢谢您的使用!再见! n; exit(0); if(choice=2)cout请您选择对矩阵的操作类型:n1.两矩阵相乘n2.矩阵数乘n3.矩阵转置 n 其他键退出 n;continue; break;case 3:coutchoice)if(choice=1)Inver(cm

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论