螺旋矩阵的类的详细介绍

螺旋矩阵的类的详细介绍

//////////////////////////////////////////////////////////////////////////
// HelixMatrix.h: interface for the Matrix class.
// Purpose : create & Print HelixMatrix
// Author : HCJ
// Date : 2005/1/5
//////////////////////////////////////////////////////////////////////////

#ifndef _HelixMatrix
#define _HelixMatrix

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class HelixMatrix
{
public:

HelixMatrix();
HelixMatrix(int n);
HelixMatrix(int m, int n);
virtual ~HelixMatrix() { delete m_pMatrix; }

void print();//print the matrix
void clear();//clear the matrix with 0
void FillDeasil();//Deasil fill matrix
void FillWiddershins();//Widdershins fill matrix
private:
int *m_pMatrix;
int m_m;
int m_n;

};
#endif

//////////////////////////////////////////////////////////////////////////
// Matrix.cpp: implementation of the Matrix class.
// Purpose : create & Print HelixMatrix
// Author : HCJ
// Date : 2005/1/5
//////////////////////////////////////////////////////////////////////

#include "Matrix.h"
#include <iostream>
#include <iomanip>
using namespace std;

// Constructor
HelixMatrix::HelixMatrix()
{
m_m=4;
m_n=4;
m_pMatrix=new int[m_m*m_n];
this->clear();
}

HelixMatrix::HelixMatrix(int n)
{
m_m=n;
m_n=n;
m_pMatrix = new int[m_m*m_n];
this->clear();
}

HelixMatrix::HelixMatrix(int m, int n)
{
m_m=m;
m_n=n;
m_pMatrix = new int[m_m*m_n];
this->clear();
}

//Destructor
//inline HelixMatrix::~HelixMatrix()
//{
//delete m_pMatrix;
//}

//clear the element with number 0
void HelixMatrix::clear()
{
for(int i=0; i<m_m; i++)
{
for (int j=0; j<m_n; j++)
{
*(m_pMatrix + i*m_n + j)=0;
}
}
}

//print the HelixMatrix
void HelixMatrix::print()
{
for(int i=0; i<m_m; i++)
{
for (int j=0; j<m_n; j++)
{
cout<<setw(5)<< *(m_pMatrix + i*m_n + j);
}
cout<<endl;
}
}

//Deasil Fill the HelixMatrix
void HelixMatrix::FillDeasil()
{
//clear the array
this->clear();

int count = 0;
int h=m_m;
int l=m_n;
int stepx=l;
int stepy=h-1;
int x=0;
int y=0;
int i = 0;


while (count != h*l )
{
//fill from left to right
if(count != h*l )
{
for(i=0; i<stepx; i++)
{
if( *(m_pMatrix + x + y*m_n) ==0 )
{
*(m_pMatrix + y*m_n + x)=++count;
}
else
{
x++;
*(m_pMatrix + y*m_n + x)=++count;
}

}
stepx--;
}
//fill from up to down
if( count != h*l )
{
for(i=0; i<stepy; i++)
{
y++;
*(m_pMatrix +y*m_n+x)=++count;
}
stepy--;
}

//from right to left
if(count != h*l )
{
for(i=0; i<stepx; i++)
{
x--;
*(m_pMatrix +y*m_n+x)=++count;
}
stepx--;
}
//from down to up

if( count != h*l )
{
for(i=0; i<stepy; i++)
{
y--;
*(m_pMatrix +y*m_n+x)=++count;
}
stepy--;
}

}

}

//widdershins fill helixMatrix with integer

void HelixMatrix::FillWiddershins()
{

//clear the array

this->clear();

int count = 0;
int h=m_m;
int l=m_n;
int stepx=l-1;
int stepy=h;
int x=0;
int y=0;
int i = 0;

while (count != h*l )
{
//fill from up to down
if(count != h*l )
{
for(i=0; i<stepy; i++)
{
if( *(m_pMatrix+x+y*m_n) ==0 )
{
*(m_pMatrix +y*m_n+x)=++count;
}
else
{
y++;
*(m_pMatrix +y*m_n+x)=++count;
}

}
stepy--;
}
//fill from left to right
if( count != h*l )
{
for(i=0; i<stepx; i++)
{
x++;
*(m_pMatrix +y*m_n+x)=++count;
}
stepx--;
}

//from down to up
if(count != h*l )
{
for(i=0; i<stepy; i++)
{
y--;
*(m_pMatrix +y*m_n+x)=++count;
}
stepy--;
}
//from right to left

if( count != h*l )
{
for(i=0; i<stepx; i++)
{
x--;
*(m_pMatrix +y*m_n+x)=++count;
}
stepx--;
}

}
}

//////////////////////////////////////////////////////////////////////////
// main.cpp : testing class HelixMatrix
// Author : HCJ
// Dete : 2005/1/5
//////////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

#include "Matrix.h"

int main()
{
HelixMatrix matrix;
matrix.FillDeasil();
matrix.print();
//matrix.clear();
cout<<"---------------------------------------------"<<endl;
matrix.FillWiddershins();
matrix.print();
cout<<"---------------------------------------------"<<endl;

HelixMatrix matrix1(6);
matrix1.FillDeasil();
matrix1.print();
cout<<"---------------------------------------------"<<endl;
//matrix1.clear();
matrix1.FillWiddershins();
matrix1.print();
cout<<"---------------------------------------------"<<endl;

HelixMatrix matrix2(5,4);
matrix2.FillDeasil();
matrix2.print();
//matrix2.clear();
cout<<"---------------------------------------------"<<endl;
matrix2.FillWiddershins();
matrix2.print();

return 0;
}