#include "stdafx.h"
#include "ForArray.h"
///////////Array类,数组的赋值、调用、求和///////////////////////////////////////
int i,j;
Array::Array(int sizerows,int sizecols)
{
sizerow=sizerows;
sizecol=sizecols;
Ay = new double*[sizerows]; //设置行,一个指针指向一个指针数组。
for(j=0;j<sizerows;j++)
{
Ay[j] = new double[sizecols]; //这个指针数组的每个指针元素又指向一个数组。
}
for (i=0;i<sizerows;i++)
{
for (j=0;j<sizecols;j++)
{Ay[i][j]=0;}
}
}
Array::~Array(){} //定义析构函数
bool Array::cpy(int row,int col,double zhi)
{
if(row<=sizerow&&row>0&&col<=sizecol&&col>0)
{
Ay[row-1][col-1]=zhi;
return TRUE;
}
else
return FALSE;
}
double Array::a(int row,int col)
{
if (row<=sizerow&&row>0&&col<=sizecol&&col>0)
{
return Ay[row-1][col-1];
}
else { return 0; }
}
double Array::getsum()
{ double summ=0,total=0;
for (i=0;i<sizerow;i++)
{
for (j=0;j<sizecol;j++)
{
total=Ay[i][j]+total;
}
}
return total;
}
double Array::sum(int minrow,int maxrow,int mincol,int maxcol) //get sum for one row
{ double summ=0,total=0;
if (minrow>0&&mincol>0&&maxrow<=sizerow&&maxcol<=sizecol&&minrow<=maxrow&&mincol<=maxcol)
{
for (i=minrow-1;i<maxrow;i++)
{
for (j=mincol-1;j<maxcol;j++)
{
summ=Ay[i][j]+summ;
}
}
return summ;
}
else {return getsum();}
}
double Array::amax(int minrow,int maxrow,int mincol,int maxcol) //get sum for one row
{
if (!(minrow>0&&mincol>0&&maxrow<=sizerow&&maxcol<=sizecol&&minrow<=maxrow&&mincol<=maxcol))
{
minrow=1;mincol=1;maxrow=sizerow;maxcol=sizecol;
}
double maxm=Ay[minrow-1][mincol-1];
dexi=1;dexj=1;
for (i=minrow-1;i<maxrow;i++)
{
for (j=mincol-1;j<maxcol;j++)
{
if ( Ay[i][j]>maxm )
{maxm=Ay[i][j];
dexi=i+1;dexj=j+1;}
}
}
return maxm;
}
double Array::amin(int minrow,int maxrow,int mincol,int maxcol) //get sum for one row
{
if (!(minrow>0&&mincol>0&&maxrow<=sizerow&&maxcol<=sizecol&&minrow<=maxrow&&mincol<=maxcol))
{
minrow=1;mincol=1;maxrow=sizerow;maxcol=sizecol;
}
double minm=Ay[minrow-1][mincol-1];
dexi=1;dexj=1;
for (i=minrow-1;i<maxrow;i++)
{
for (j=mincol-1;j<maxcol;j++)
{
if ( Ay[i][j]<minm )
{ minm=Ay[i][j];
dexi=i+1;dexj=j+1;}
}
}
return minm;
}
////////////////////Array3D class//////////////////////////
Array3D::Array3D(int sizerows,int sizecols,int sizezols)
{ sizerow=sizerows;
sizecol=sizecols;
sizezol=sizezols;
int r,c,z;
Ay = new double**[sizerow];
for(r=0;r<sizerow;r++)
{
Ay[r] = new double*[sizecol];
for(c=0;c<10;c++)
{
Ay[r][c] = new double[sizezol];
}
}
for (r=0;r<sizerow;r++)
{
for (c=0;c<sizecol;c++)
{
for (z=0;z<sizezol;z++)
{Ay[r][c][z]=0;}
}
}
}
Array3D::~Array3D(){} //定义析构函数
double Array3D::cpy(int row,int col,int zol,double zhi)
{
if ( row>sizerow||col>sizecol||zol>sizezol)
{
AfxMessageBox(_T("Array3D::cpy rows or cols exceed"));
return false;
}
else
{
Ay[row-1][col-1][zol-1]=zhi;
return Ay[row-1][col-1][zol-1];
}
}
double Array3D::a(int row,int col,int zol)
{
if (row<=sizerow&&row>0&&col<=sizecol&&col>0&&zol<=sizezol&&zol>0)
{
return Ay[row-1][col-1][zol-1];
}
else { return 0;}
}
double Array3D::getsum()
{
double total=0;
int r,c,z;
for (r=0;r<sizerow;r++)
{
for (c=0;c<sizecol;c++)
{
for (z=0;z<sizezol;z++)
{total=Ay[r][c][z]+total;}
}
}
return total;
}
double Array3D::sum(int minrow,int maxrow,int mincol,int maxcol,int minzol,int maxzol)
{
double total=0;
int r,c,z;
for (r=minrow-1;r<maxrow;r++)
{
for (c=mincol-1;c<maxcol;c++)
{
for (z=minzol-1;z<maxzol;z++)
{total=Ay[r][c][z]+total;}
}
}
return total;
}
double Array3D::amax(int minrow,int maxrow,int mincol,int maxcol,int minzol,int maxzol)
{
double maxx=Ay[minrow-1][mincol-1][minzol-1];
int r,c,z;
for (r=minrow-1;r<maxrow;r++)
{
for (c=mincol-1;c<maxcol;c++)
{
for (z=minzol-1;z<maxzol;z++)
{
if (Ay[r][c][z]>maxx)
{maxx=Ay[r][c][z];}
}
}
}
return maxx;
}
bool CpyArray(Array a,int fromai,int toai,int fromaj,int toaj,Array b,int frombi,int tobi,int frombj,int tobj)
{
if( fromai>0&&toai<=a.sizerow&&fromai<=toai&&fromaj>0&&toaj<=a.sizecol&&fromaj<=toaj&&
frombi>0&&tobi<=b.sizerow&&frombi<=tobi&&frombj>0&&tobj<=b.sizecol&&frombj<=tobj )
{
for (i=0;i<toai-fromai+1;i++)
{
for (j=0;j<toaj-fromaj+1;j++)
{
a.cpy(i+fromai,j+fromaj,b.a(i+frombi,j+frombj));
}
}
return TRUE;
}
else
{
// AfxMessageBox("CpyArray:array's rows or cols are not same or exceed!");
return FALSE;
}
}//////end bool CpyArray
//////////////////////////////////////
bool CpyaddArray(Array c,int fromci,int toci,int fromcj,int tocj,Array a,int fromai,int toai,int fromaj,int toaj,Array b,int frombi,int tobi,int frombj,int tobj)
{
if( fromai>0&&toai<=a.sizerow&&fromai<=toai&&fromaj>0&&toaj<=a.sizecol&&fromaj<=toaj&&
frombi>0&&tobi<=b.sizerow&&frombi<=tobi&&frombj>0&&tobj<=b.sizecol&&frombj<=tobj&&
fromci>0&&toci<=c.sizerow&&fromci<=toci&&fromcj>0&&tocj<=c.sizecol&&fromcj<=tocj )
{
for (i=0;i<toai-fromai+1;i++)
{
for (j=0;j<toaj-fromaj+1;j++)
{
c.cpy(i+fromci,j+fromcj,a.a(i+fromai,j+fromaj)+b.a(i+frombi,j+frombj));
}
}
return TRUE;
}
else
{
//AfxMessageBox("CpyaddArray: array's rows or cols are not same or exceed!");
return FALSE;
}
}/////end bool CpyaddArray
bool CpytimesArray(Array c,int fromci,int toci,int fromcj,int tocj,Array a,int fromai,int toai,int fromaj,int toaj,Array b,int frombi,int tobi,int frombj,int tobj)
{
if( fromai>0&&toai<=a.sizerow&&fromai<=toai&&fromaj>0&&toaj<=a.sizecol&&fromaj<=toaj&&
frombi>0&&tobi<=b.sizerow&&frombi<=tobi&&frombj>0&&tobj<=b.sizecol&&frombj<=tobj&&
fromci>0&&toci<=c.sizerow&&fromci<=toci&&fromcj>0&&tocj<=c.sizecol&&fromcj<=tocj )
{
for (i=0;i<toai-fromai+1;i++)
{
for (j=0;j<toaj-fromaj+1;j++)
{
c.cpy(i+fromci,j+fromcj,a.a(i+fromai,j+fromaj)*b.a(i+frombi,j+frombj));
}
}
return TRUE;
}
else
{
//AfxMessageBox("CpytimesArray: array's rows or cols are not same or exceed!");
return FALSE;
}
}/////end bool CpytimesArray
bool CpyminusArray(Array c,int fromci,int toci,int fromcj,int tocj,Array a,int fromai,int toai,int fromaj,int toaj,Array b,int frombi,int tobi,int frombj,int tobj)
{
if( fromai>0&&toai<=a.sizerow&&fromai<=toai&&fromaj>0&&toaj<=a.sizecol&&fromaj<=toaj&&
frombi>0&&tobi<=b.sizerow&&frombi<=tobi&&frombj>0&&tobj<=b.sizecol&&frombj<=tobj&&
fromci>0&&toci<=c.sizerow&&fromci<=toci&&fromcj>0&&tocj<=c.sizecol&&fromcj<=tocj )
{
for (i=0;i<toai-fromai+1;i++)
{
for (j=0;j<toaj-fromaj+1;j++)
{
c.cpy(i+fromci,j+fromcj,a.a(i+fromai,j+fromaj)-b.a(i+frombi,j+frombj));
}
}
return TRUE;
}
else
{
//AfxMessageBox("CpyminusArray: array's rows or cols are not same or exceed!");
return FALSE;
}
}/////end bool CpyminusArray
///点除
bool CpydivideArray(Array c,int fromci,int toci,int fromcj,int tocj,Array a,int fromai,int toai,int fromaj,int toaj,Array b,int frombi,int tobi,int frombj,int tobj)
{
if( fromai>0&&toai<=a.sizerow&&fromai<=toai&&fromaj>0&&toaj<=a.sizecol&&fromaj<=toaj&&
frombi>0&&tobi<=b.sizerow&&frombi<=tobi&&frombj>0&&tobj<=b.sizecol&&frombj<=tobj&&
评论0