2次元データ

  char 型の2次元配列を扱う



  
#include <windows.h>
#include <stdio.h>
#include "Matrix.h"


typedef char DIM[16];
FILE *fp;

int main(int argc, char* argv[])
{
	char *data = new char[16*10];
	FillMemory( data, 16*10, 0x00 );

	DIM *dim;
	dim = (DIM *)data;

	dim[0][0] = 1;
	dim[1][1] = 1;
	dim[3][7] = 1;
	dim[4][9] = 1;

	fp = fopen( "C:\\TEMP\\dim.dat", "wb" );
	fwrite( data, 16, 10, fp );
	fclose( fp );

	delete [] data;

	Matrix matrix(16,10);

	matrix.put(0,0,1);
	matrix.put(1,1,1);
	matrix.put(3,7,5);
	matrix.put(4,9,1);

	printf( "%d\n", matrix.get( 3, 7 ) );

	fp = fopen( "C:\\TEMP\\matrix.dat", "wb" );
	fwrite( matrix.ptr, 16, 10, fp );
	fclose( fp );

	return 0;
}
  

クラスのデータ
  
00000000 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000010 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000030 00 00 00 00 00 00 00 05 00 00 00 00 00 00 00 00  ................
00000040 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00  ................
00000050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
00000090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  



  Matrix.h



  
class Matrix  
{
public:
	void put();
	Matrix();
	Matrix( int x, int y);
	void put(int x, int y, int data);
	int get(int x, int y);
	virtual ~Matrix();

	int x;
	int y;
	int size;
	char *ptr;
};
  



  Matrix.cpp

  
#include "stdafx.h"
#include "Matrix.h"
#include "windows.h"

//////////////////////////////////////////////////////////////////////
// 構築/消滅
//////////////////////////////////////////////////////////////////////

Matrix::Matrix()
{
	x = 16;
	y = 16;
	size = x * y;
	ptr = (char *)GlobalAlloc(
			GMEM_FIXED,
			size+128
	);
	if ( ptr != NULL ) {
		FillMemory( ptr, size, 0x00 );
	}

}
Matrix::Matrix( int x, int y)
{
	Matrix::x = x;
	Matrix::y = y;
	size = Matrix::x * Matrix::y;
	ptr = (char *)GlobalAlloc(
			GMEM_FIXED,
			size+128
	);
	if ( ptr != NULL ) {
		FillMemory( ptr, size, 0x00 );
	}
}

Matrix::~Matrix()
{
	if ( ptr == NULL ) {
		GlobalFree( (HGLOBAL)(ptr) );
	}
}

void Matrix::put(int x, int y, int data)
{
	*(ptr + (Matrix::x * x + y)) = (unsigned char)data;
}

int Matrix::get(int x, int y)
{
	return (int)(*(ptr + (Matrix::x * x + y)));
}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ