Informatika gyűjtemény

Egy szinttel feljebb matrix.h

2004050607080910

NézetNyomtat

matrix.h (Vissza)
Az alábbi letöltési lehetőségek közül választhatsz: (segítség)
Karakterkódolás:
Sortörés:
Típus: text/plain
Tartalmaz szöveget
Karakterkódolás: us-ascii
Méret: 1 KB
#ifndef type
#define type int
#endif

class cMatrix {
private:
int N, M;
type * P;
public:
cMatrix (void);
cMatrix (int, int);
cMatrix (const cMatrix &);
~cMatrix (void);

cMatrix & operator = (const cMatrix &);

void Set (int, int);
int GetN (void) const;
int GetM (void) const;
type & GetItem (int, int) const;
void SetItem (int, int, type);
void SetRow (int, ...);

void Empty (void);
int bSquare (void) const;
};

cMatrix::cMatrix (void) {
N = 1;
M = 1;
P = new type [N * M];
}
cMatrix::cMatrix (int N_, int M_) {
N = N_ < 1 ? 1 : N_;
M = M_ < 1 ? 1 : M_;
P = new type [N * M];
}
cMatrix::cMatrix (const cMatrix & A) {
N = A.N;
M = A.M;
P = new type [N * M];
for (int I = 0; I < N * M; I++)
P[I] = A.P[I];
}
cMatrix::~cMatrix (void) {
delete [] P;
}

cMatrix & cMatrix::operator = (const cMatrix & A) {
if (A.N != N || A.M != M) {
delete [] P;
N = A.N;
M = A.M;
P = new type [N * M];
}
for (int I = 0; I < N * M; I++)
P[I] = A.P[I];
return *this;
}

void cMatrix::Set (int N_, int M_) {
delete [] P;
N = N_ < 1 ? 1 : N_;
M = M_ < 1 ? 1 : M_;
P = new type [N * M];
}
inline int cMatrix::GetN (void) const {
return N;
}
inline int cMatrix::GetM (void) const {
return M;
}
inline type & cMatrix::GetItem (int X, int Y) const {
return X >= 0 && X < M && Y >= 0 && Y < N ? P[Y * M + X] : P[0];
}
inline void cMatrix::SetItem (int X, int Y, type New) {
if (X >= 0 && X < M && Y >= 0 && Y < N)
P[Y * M + X] = New;
}
void cMatrix::SetRow (int Y, ...) {
type * A = (type *)(&Y + 1);
type * B = &P[Y * M];

for (int I = 0; I < N; I++)
*B++ = *A++;
}

void cMatrix::Empty (void) {
for (int I = 0; I < N * M; I++)
P[I] = 0;
}
inline int cMatrix::bSquare (void) const {
return N == M;
}
(Vissza)