matrix运算c代码.docx

上传人:b****6 文档编号:7495920 上传时间:2023-01-24 格式:DOCX 页数:103 大小:35.38KB
下载 相关 举报
matrix运算c代码.docx_第1页
第1页 / 共103页
matrix运算c代码.docx_第2页
第2页 / 共103页
matrix运算c代码.docx_第3页
第3页 / 共103页
matrix运算c代码.docx_第4页
第4页 / 共103页
matrix运算c代码.docx_第5页
第5页 / 共103页
点击查看更多>>
下载资源
资源描述

matrix运算c代码.docx

《matrix运算c代码.docx》由会员分享,可在线阅读,更多相关《matrix运算c代码.docx(103页珍藏版)》请在冰豆网上搜索。

matrix运算c代码.docx

matrix运算c代码

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

//Matrix.cpp

//

//操作矩阵的类CMatrix的实现文件

//

//周长发编制,2002/8

//////////////////////////////////////////////////////////////////////

#include"stdafx.h"

#include"Matrix.h"

#ifdef_DEBUG

#undefTHIS_FILE

staticcharTHIS_FILE[]=__FILE__;

#definenewDEBUG_NEW

#endif

//////////////////////////////////////////////////////////////////////

//Construction/Destruction

//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////

//基本构造函数

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix()

{

m_nNumColumns=1;

m_nNumRows=1;

m_pData=NULL;

BOOLbSuccess=Init(m_nNumRows,m_nNumColumns);

ASSERT(bSuccess);

}

//////////////////////////////////////////////////////////////////////

//指定行列构造函数

//

//参数:

//1.intnRows-指定的矩阵行数

//2.intnCols-指定的矩阵列数

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix(intnRows,intnCols)

{

m_nNumRows=nRows;

m_nNumColumns=nCols;

m_pData=NULL;

BOOLbSuccess=Init(m_nNumRows,m_nNumColumns);

ASSERT(bSuccess);

}

//////////////////////////////////////////////////////////////////////

//指定值构造函数

//

//参数:

//1.intnRows-指定的矩阵行数

//2.intnCols-指定的矩阵列数

//3.doublevalue[]-一维数组,长度为nRows*nCols,存储矩阵各元素的值

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix(intnRows,intnCols,doublevalue[])

{

m_nNumRows=nRows;

m_nNumColumns=nCols;

m_pData=NULL;

BOOLbSuccess=Init(m_nNumRows,m_nNumColumns);

ASSERT(bSuccess);

SetData(value);

}

//////////////////////////////////////////////////////////////////////

//方阵构造函数

//

//参数:

//1.intnSize-方阵行列数

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix(intnSize)

{

m_nNumRows=nSize;

m_nNumColumns=nSize;

m_pData=NULL;

BOOLbSuccess=Init(nSize,nSize);

ASSERT(bSuccess);

}

//////////////////////////////////////////////////////////////////////

//方阵构造函数

//

//参数:

//1.intnSize-方阵行列数

//2.doublevalue[]-一维数组,长度为nRows*nRows,存储方阵各元素的值

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix(intnSize,doublevalue[])

{

m_nNumRows=nSize;

m_nNumColumns=nSize;

m_pData=NULL;

BOOLbSuccess=Init(nSize,nSize);

ASSERT(bSuccess);

SetData(value);

}

//////////////////////////////////////////////////////////////////////

//拷贝构造函数

//

//参数:

//1.constCMatrix&other-源矩阵

//////////////////////////////////////////////////////////////////////

CMatrix:

:

CMatrix(constCMatrix&other)

{

m_nNumColumns=other.GetNumColumns();

m_nNumRows=other.GetNumRows();

m_pData=NULL;

BOOLbSuccess=Init(m_nNumRows,m_nNumColumns);

ASSERT(bSuccess);

//copythepointer

memcpy(m_pData,other.m_pData,

sizeof(double)*m_nNumColumns*m_nNumRows);

}

//////////////////////////////////////////////////////////////////////

//析构函数

//////////////////////////////////////////////////////////////////////

CMatrix:

:

~CMatrix()

{

if(m_pData)

{

delete[]m_pData;

m_pData=NULL;

}

}

//////////////////////////////////////////////////////////////////////

//初始化函数

//

//参数:

//1.intnRows-指定的矩阵行数

//2.intnCols-指定的矩阵列数

//

//返回值:

BOOL型,初始化是否成功

//////////////////////////////////////////////////////////////////////

BOOLCMatrix:

:

Init(intnRows,intnCols)

{

if(m_pData)

{

delete[]m_pData;

m_pData=NULL;

}

m_nNumRows=nRows;

m_nNumColumns=nCols;

intnSize=nCols*nRows;

if(nSize<0)

returnFALSE;

//分配内存

m_pData=newdouble[nSize];

if(m_pData==NULL)

returnFALSE;//内存分配失败

if(IsBadReadPtr(m_pData,sizeof(double)*nSize))

returnFALSE;

//将各元素值置0

memset(m_pData,0,sizeof(double)*nSize);

returnTRUE;

}

//////////////////////////////////////////////////////////////////////

//将方阵初始化为单位矩阵

//

//参数:

//1.intnSize-方阵行列数

//

//返回值:

BOOL型,初始化是否成功

//////////////////////////////////////////////////////////////////////

BOOLCMatrix:

:

MakeUnitMatrix(intnSize)

{

if(!

Init(nSize,nSize))

returnFALSE;

for(inti=0;i

for(intj=0;j

if(i==j)

SetElement(i,j,1);

returnTRUE;

}

//////////////////////////////////////////////////////////////////////

//将字符串转化为矩阵的值

//

//参数:

//1.CStrings-数字和分隔符构成的字符串

//2.constCString&sDelim-数字之间的分隔符,默认为空格

//3.BOOLbLineBreak-行与行之间是否有回车换行符,默认为真(有换行符)

//当该参数为FALSE时,所有元素值都在一行中输入,字符串的第一个

//数值应为矩阵的行数,第二个数值应为矩阵的列数

//

//返回值:

BOOL型,转换是否成功

//////////////////////////////////////////////////////////////////////

BOOLCMatrix:

:

FromString(CStrings,constCString&sDelim/*=""*/,BOOL

bLineBreak/*=TRUE*/)

{

if(s.IsEmpty())

returnFALSE;

//分行处理

if(bLineBreak)

{

CTokenizertk(s,"\r\n");

CStringListListRow;

CStringsRow;

while(tk.Next(sRow))

{

sRow.TrimLeft();

sRow.TrimRight();

if(sRow.IsEmpty())

break;

ListRow.AddTail(sRow);

}

//行数

m_nNumRows=ListRow.GetCount();

sRow=ListRow.GetHead();

CTokenizertkRow(sRow,sDelim);

CStringsElement;

//列数

m_nNumColumns=0;

while(tkRow.Next(sElement))

{

m_nNumColumns++;

}

//初始化矩阵

if(!

Init(m_nNumRows,m_nNumColumns))

returnFALSE;

//设置值

POSITIONpos=ListRow.GetHeadPosition();

for(inti=0;i

{

sRow=ListRow.GetNext(pos);

intj=0;

CTokenizertkRow(sRow,sDelim);

while(tkRow.Next(sElement))

{

sElement.TrimLeft();

sElement.TrimRight();

doublev=atof(sElement);

SetElement(i,j++,v);

}

}

returnTRUE;

}

//不分行(单行)处理

CTokenizertk(s,sDelim);

CStringsElement;

//行数

tk.Next(sElement);

sElement.TrimLeft();

sElement.TrimRight();

m_nNumRows=atoi(sElement);

//列数

tk.Next(sElement);

sElement.TrimLeft();

sElement.TrimRight();

m_nNumColumns=atoi(sElement);

//初始化矩阵

if(!

Init(m_nNumRows,m_nNumColumns))

returnFALSE;

//设置值

inti=0,j=0;

while(tk.Next(sElement))

{

sElement.TrimLeft();

sElement.TrimRight();

doublev=atof(sElement);

SetElement(i,j++,v);

if(j==m_nNumColumns)

{

j=0;

i++;

if(i==m_nNumRows)

break;

}

}

returnTRUE;

}

//////////////////////////////////////////////////////////////////////

//将矩阵各元素的值转化为字符串

//

//参数:

//1.constCString&sDelim-数字之间的分隔符,默认为空格

//2BOOLbLineBreak-行与行之间是否有回车换行符,默认为真(有换行符)

//

//返回值:

CString型,转换得到的字符串

//////////////////////////////////////////////////////////////////////

CStringCMatrix:

:

ToString(constCString&sDelim/*=""*/,BOOLbLineBreak

/*=TRUE*/)const

{

CStrings="";

for(inti=0;i

{

for(intj=0;j

{

CStringss;

ss.Format("%f",GetElement(i,j));

s+=ss;

if(bLineBreak)

{

if(j!

=m_nNumColumns-1)

s+=sDelim;

}

else

{

if(i!

=m_nNumRows-1||j!

=m_nNumColumns-1)

s+=sDelim;

}

}

if(bLineBreak)

if(i!

=m_nNumRows-1)

s+="\r\n";

}

returns;

}

//////////////////////////////////////////////////////////////////////

//将矩阵指定行中各元素的值转化为字符串

//

//参数:

//1.intnRow-指定的矩阵行,nRow=0表示第一行

//2.constCString&sDelim-数字之间的分隔符,默认为空格

//

//返回值:

CString型,转换得到的字符串

//////////////////////////////////////////////////////////////////////

CStringCMatrix:

:

RowToString(intnRow,constCString&sDelim/*=""*/)const

{

CStrings="";

if(nRow>=m_nNumRows)

returns;

for(intj=0;j

{

CStringss;

ss.Format("%f",GetElement(nRow,j));

s+=ss;

if(j!

=m_nNumColumns-1)

s+=sDelim;

}

returns;

}

//////////////////////////////////////////////////////////////////////

//将矩阵指定列中各元素的值转化为字符串

//

//参数:

//1.intnCol-指定的矩阵行,nCol=0表示第一列

//2.constCString&sDelim-数字之间的分隔符,默认为空格

//

//返回值:

CString型,转换得到的字符串

//////////////////////////////////////////////////////////////////////

CStringCMatrix:

:

ColToString(intnCol,constCString&sDelim/*=""*/)const

{

CStrings="";

if(nCol>=m_nNumColumns)

returns;

for(inti=0;i

{

CStringss;

ss.Format("%f",GetElement(i,nCol));

s+=ss;

if(i!

=m_nNumRows-1)

s+=sDelim;

}

returns;

}

//////////////////////////////////////////////////////////////////////

//设置矩阵各元素的值

//

//参数:

//1.doublevalue[]-一维数组,长度为m_nNumColumns*m_nNumRows,存储

//矩阵各元素的值

//

//返回值:

//////////////////////////////////////////////////////////////////////

voidCMatrix:

:

SetData(doublevalue[])

{

//emptythememory

memset(m_pData,0,sizeof(double)*m_nNumColumns*m_nNumRows);

//copydata

memcpy(m_pData,value,sizeof(double)*m_nNumColumns*m_nNumRows);

}

//////////////////////////////////////////////////////////////////////

//设置指定元素的值

//

//参数:

//1.intnRows-指定的矩阵行数

//2.intnCols-指定的矩阵列数

//3.doublevalue-指定元素的值

//

//返回值:

BOOL型,说明设置是否成功

//////////////////////////////////////////////////////////////////////

BOOLCMatrix:

:

SetElement(intnRow,intnCol,doublevalue)

{

if(nCol<0||nCol>=m_nNumColumns||nRow<0||nRow>=m_nNumRows)

returnFALSE;//arrayboundserror

if(m_pData==NULL)

returnFALSE;//badpointererror

m_pData[nCol+nRow*m_nNumColumns]=value;

returnTRUE;

}

//////////////////////////////////////////////////////////////////////

//设置指定元素的值

//

//参

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

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

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