티스토리 뷰

#include <stdio.h>

#include <iostream>

#include <crtdbg.h>


using namespace std;



class cMagicSquare

{

public:

cMagicSquare(int _dim);

~cMagicSquare();


private:

int** mP;

int mDim;


public:

void setDim(int _dim);

void setMagic();


int getDim();


void showMagic();

};



cMagicSquare::cMagicSquare(int _dim)

{

mDim = _dim;


mP = new int*[_dim];

for( int i = 0; i < _dim; i++ )

{

mP[i] = new int[_dim];

}

}



void cMagicSquare::setDim(int _dim)

{

mDim = _dim;

}



void cMagicSquare::setMagic()

{

int x,y;


x = getDim() / 2;

y = 0;


for( int i = 0; i < getDim() * getDim(); i++ )

{

mP[x][y] = i + 1;


if( (i + 1) % getDim() == 0 )

y++;

else

x++;


if( x == getDim() )

x = 0;


if( y < 0 )

y = getDim() - 1;

}

}



int cMagicSquare::getDim()

{

return mDim;

}



void cMagicSquare::showMagic()

{

for( int i = 0; i < getDim(); i++ )

{

for( int j = 0; j < getDim(); j++ )

{

cout<<mP[j][i]<<"\t";

}


cout<<endl;

}

}



cMagicSquare::~cMagicSquare()

{

for( int i = 0; i < mDim; i++ )

delete[] mP[i];


delete[] mP;

}






void main()

{

#if defined(DEBUG) | defined(_DEBUG)

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

#endif

char input[20];

scanf("%c", &input);

cMagicSquare mg(atoi(input));

mg.setMagic();

mg.showMagic();

}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함