-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.cpp
More file actions
65 lines (50 loc) · 1.28 KB
/
table.cpp
File metadata and controls
65 lines (50 loc) · 1.28 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <cassert>
#include <iostream>
#include "table.h"
Table::Table(unsigned int n, unsigned int m){
data = new int[n*m];
for(int i=0;i<n*m;++i) data[i]=0;
N = n; M = m;
}
int & Table::operator()(unsigned int i, unsigned int j){
assert( i<N && j<M );
return data[i*M+j];
}
const int & Table::operator()(unsigned int i, unsigned int j) const{
assert( i<N && j<M );
return data[i*M+j];
}
Table::Table(Table const &rhs){
N = rhs.N; M = rhs.M;
data = new int[N*M];
for(int i=0;i<N*M;++i)
data[i]=rhs.data[i];
}
Table & Table::operator=(Table const & rhs){
int t=1;
if(N == rhs.N && M == rhs.M) {
for(int i=0;i<rhs.N*rhs.M;++i)
if(data[i]!=rhs.data[i]) { t=0; break; }
}
else t=0;
if( t ) return *this;
delete [] data;
N = rhs.N; M = rhs.M;
data = new int[N*M];
for(int i=0;i<N*M;++i)
data[i]=rhs.data[i];
return *this;
}
void Table::resize(unsigned int n, unsigned int m){
delete [] data;
data = new int[n*m];
for(int i=0;i<n*m;++i) data[i]=0;
N = n; M = m;
}
void Table::print(){
for(int i=0;i<M*N;++i){
if( i%M==0 ) { std::cout << std::endl; }
std::cout << data[i] << " ";
}
std::cout << std::endl << std::endl;
}