-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_vector.h
executable file
·186 lines (152 loc) · 4.44 KB
/
C_vector.h
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//good to go!
#ifndef C_VECTOR_H
#define C_VECTOR_H
#include <stdlib.h>
#include <iostream>
//this is a template, meaning that you need to specify the type when you instanciate a object.
template<class dataType> class C_vector
{
public:
C_vector(unsigned short _N); //constructor
virtual ~C_vector(); //destructor
C_vector(C_vector const& X); // constructeur par copie
//overload usefull operators
C_vector& operator= (C_vector const& c);
C_vector& operator= (dataType const& x);
C_vector& operator+ (C_vector const& c);
C_vector& operator+ (dataType const& x);
C_vector& operator- (C_vector const& c);
C_vector& operator- ( dataType const& x);
dataType operator[](unsigned short i)const; //acces array
bool set (unsigned short i, dataType x);
//tool that you could use
unsigned short length() const; // get length
void show();
private:
//size and datas
unsigned short m_N;
dataType *m_V;
};
template<class dataType> C_vector<dataType>::C_vector(unsigned short _N):m_N(_N)
{
/*cout << "create Vector" << endl;
m_V = new dataType[m_N];
for(int i=0; i<m_N; i++)
m_V[i]=0;*/
//std::cout << "create vector" << std::endl;
m_V = NULL;
m_V = new dataType[_N];
if(m_V==NULL)
{
m_N=0;
}
else
{
m_N=_N;
}
}
template<class dataType> C_vector<dataType>::C_vector(C_vector const& X)
{
m_V = NULL;
m_V = new dataType[X.length()];
if(m_V==NULL)
{
m_N=0;
}
else
{
m_N=X.length();
for(unsigned short i=0 ; i<m_N ; i++)
this->m_V[i] = X[i];
}
}
template<class dataType> C_vector<dataType>::~C_vector()
{
//std::cout << "delete vector" << std::endl;
if(m_V!=NULL)
{
delete m_V;
}
}
template<class dataType> void C_vector<dataType>::show()
{
for(unsigned short i=0 ; i<m_N ; i++)
std::cout << "V[" << i << "] = " << (*this)[i] << std::endl;
}
template<class dataType> unsigned short C_vector<dataType>::length() const
{
return m_N;
}
template<class dataType> dataType C_vector<dataType>::operator[] (unsigned short i) const
{
if(i<m_N)
{
return m_V[i];
}
else
{
throw "index must be positive and smaller than max length of vector";
}
}
/*template<class dataType> C_vector<dataType>& C_vector<dataType>::operator= (C_vector const& c)
{
if(this==&c)
return *this;
delete m_V; // nouvelle taille
m_N = c.length();
m_V = new dataType[m_N];
for(int i=0; i<m_N; i++)
m_V[i]=c[i];
return *this;
}*/
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator= (C_vector const& c)
{
if(this==&c) return *this;
if(c.length()!=this->length()) throw "dimension vector must agree";
for(unsigned short i=0 ; i<this->m_N ; i++)
this->m_V[i] = c[i];
return *this;
}
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator= (dataType const& x)
{
for(unsigned short i=0 ; i<this->m_N ; i++)
this->m_V[i] = x;
return *this;
}
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator+ (const C_vector& c)
{
if(c.length()!=this->length()) throw "dimension vector must agree";
C_vector<dataType>* Y = new C_vector<dataType>(this->m_N);
for(unsigned short i=0 ; i<this->m_N ; i++)
Y->set(i,this->m_V[i] + c[i]);
return *Y;
}
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator+ (const dataType& x)
{
C_vector<dataType>* Y = new C_vector<dataType>(this->m_N);
for(unsigned short i=0 ; i<this->m_N ; i++)
Y->set(i,this->m_V[i] + x);
return *Y;
}
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator- (C_vector const& c)
{
if(c.length()!=this->length()) throw "dimension vector must agree";
C_vector<dataType>* Y = new C_vector<dataType>(this->m_N);
for(unsigned short i=0 ; i<this->m_N ; i++)
Y->set(i,this->m_V[i] - c[i]);
return *Y;
}
template<class dataType> C_vector<dataType>& C_vector<dataType>::operator- (dataType const& x)
{
C_vector<dataType>* Y = new C_vector<dataType>(this->m_N);
for(unsigned short i=0 ; i<this->m_N ; i++)
Y->set(i,this->m_V[i] - x);
return *Y;
}
template<class dataType> bool C_vector<dataType>::set (unsigned short i, dataType x)
{
if(i>=m_N) return false;
this->m_V[i] = x;
return true;
}
#endif // C_VECTOR_H