?? coearray.cpp
字號:
#pragma once
#include "coeArray.h"
/*===============================================================================
the constructional function of class "coeArray"
Parameters:
len (unsigned int): indicating the number of sub-arrays in the 2-d array
Action:
allocate the memory for the 1st dimension of the 2-d array if len is not 0,
initiate the sub pointer to NULL. allocate memory for the temperature limit
array and the length of sub array and initiate them to 0.
if len is 0, initiate all the pointers to NULL.
-------------------------------------------------------------------------------*/
coeArray::coeArray(unsigned int len):
_len(len)
{
if(_len!=0)
{
_sublen=new unsigned int[_len];
_Tl=new double[_len+1];
A=new double *[_len];
for(unsigned int i=0; i<_len; ++i)
{
_sublen[i]=0;
A[i]=NULL;
_Tl[i]=0.0;
}
_Tl[_len]=0.0;
}
else
{
A=NULL;
_sublen=NULL;
_Tl=NULL;
}
}
/*===============================================================================
the copy constructional function of class "coeArray"
Parameters:
des (coeArray &): the original object to copy
Action:
if the length of array in "des" is not 0, copy all the arrays in des to this.
if the length of array in "des" is 0, set all the pointers in this to NULL.
-------------------------------------------------------------------------------*/
coeArray::coeArray(const coeArray &des)
{
if(des._len!=0)
{
//copy the new data
CopyArray(des);
}
else
{
_len=0;
A=NULL;
_sublen=NULL;
_Tl=NULL;
}
}
/*===============================================================================
the destructional function of class "coeArray"
Parameters: None
Action:
free all the memory for pointers in coeArray if them have been allocated new
memory.
-------------------------------------------------------------------------------*/
coeArray::~coeArray()
{
if(_len!=0)
{
//free the allocated memory
for(unsigned int i=0; i<_len; ++i)
{
if(_sublen[i]!=0)
delete []A[i];
}
delete []_sublen;
delete []A;
delete []_Tl;
}
}
/*===============================================================================
the overload of operator [] in coeArray
Parameters:
index(unsigned int): the index used in returning
Action:
return the address of sub-arrays in the class, indexed by the parameter "index"
-------------------------------------------------------------------------------*/
inline double *coeArray::operator[](unsigned int index)
{
if(index<_len)
return A[index];
else
return NULL;
}
/*===============================================================================
Name: operator=
the overload of operator = in coeArray
Parameters:
des(const coeArray &): the obj
Return:
coeArray &: the refence to this
Action:
copy the data in "des". Before copying, this function will check the memory and
the already allocated
-------------------------------------------------------------------------------*/
inline coeArray &coeArray::operator =(const coeArray &des)
{
if(this==&des)
return *this;
else
{
//free the allocated memory
if(_len!=0)
{
for(unsigned int i=0; i<_len; ++i)
{
if(_sublen[i]!=0)
delete []A[i];
}
delete []A;
delete []_sublen;
delete []_Tl;
_len=0;
A=NULL;
_sublen=NULL;
}
//copy the new data
if(des._len!=0)
{
CopyArray(des);
}
return *this;
}
}
/*==================================================================================
Name: CopyArray
Paremeter:
des(const coeArray &): the object to copy data from
Return:
void
Action:
allocate new memory for this and copy the data in des
----------------------------------------------------------------------------------*/
inline void coeArray::CopyArray(const coeArray &des)
{
_len=des._len;
A=new double *[_len];
_sublen=new unsigned int [_len];
_Tl=new double[_len+1];
for(unsigned int i=0; i<_len; ++i)
{
_sublen[i]=des._sublen[i];
A[i]=new double[_sublen[i]];
if(_sublen[i]!=0)
{
for(unsigned int j=0; j<_sublen[i]; ++j)
A[i][j]=des.A[i][j];
}
_Tl[i]=des._Tl[i];
}
}
/*=======================================================================================
Name: SubAlloc
allocate memory for sub arrays
Parameters:
index(unsigned int): the index of the sub array
sublen(unsigned int): the length of memory to allocate for the sub array
Return:
bool
Action:
if index is larger than the length of the array, the allocation is failed, and the
function will return false.
if index is smaller than the length of the array, the function will allocate some
new function for the sub array and return true.
---------------------------------------------------------------------------------------*/
inline bool coeArray::SubAlloc(unsigned int index,unsigned int sublen)
{
if(index<_len)
{
if(_sublen[index]!=0)
delete []A[index];
A[index]=new double[sublen];
_sublen[index]=sublen;
return true;
}
else
return false;
}
/*===============================================================================
Name: Len
the member function in coeArray
Parameters:
None
Return:
unsigned int: the length of the array
Action:
return the length of array in the class
-------------------------------------------------------------------------------*/
inline unsigned int coeArray::Len()
{
return _len;
}
/*===============================================================================
Name: Sublen
the member function in coeArray
Parameters:
index(unsigned int): the index used in returning
Return:
unsigned int: the length of the indicated sub-array
Action:
return the length of sub-arrays in the class, indexed by the parameter "index".
If the "index" is larger the the length of the array, the function returns a zero.
-------------------------------------------------------------------------------*/
inline unsigned int coeArray::SubLen(unsigned int index)
{
if(index<_len)
return _sublen[index];
else
return 0;
}
/*===============================================================================
Name: Tl
the member function in coeArray
Parameters:
index(unsigned int): the index used in returning
Return:
double &: the temperature limit for each segment
Action:
return the refernce of temperature limit for each segment precising the Cp,
indexed by the parameter "index". If index is larger than the _len, the function
will return the referance to the redundant element in _Tl.
-------------------------------------------------------------------------------*/
inline double &coeArray::Tl(unsigned int index)
{
if(index<_len)
return _Tl[index];
else
return _Tl[_len];
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -