C++_Templates_No1

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;
class Complex {
double mRe;
double mIm;

public:
Complex(double re, double im) {
mRe = re;
mIm = im;
}

double sqnorm() {
return mRe*mRe + mIm*mIm;
}
Complex& operator ++ ()
{
mRe++;
mIm++;
return *this;
}

Complex operator ++ (int)
{
Complex result(*this);
++(*this);
return result;
}

Complex operator+ (const Complex& c){

return Complex(c.mRe + this->mRe, c.mIm + this->mIm);
}
Complex operator- (const Complex& c){

return Complex(-c.mRe + this->mRe, -c.mIm + this->mIm);
}

bool operator <(Complex a) {
if (sqnorm() < a.sqnorm())
return true;
return false;
}

friend std::ostream& operator << (std::ostream &out, const Complex &c) {

out << c.mRe << " ";

if (c.mIm >= 0)
out << "+ " << c.mIm << "i";
else
out << c.mIm << "i";

return out;
}
};

template <class tip_celula> class VECT {
private:
tip_celula *start;
int numar_celule;
public:

VECT() {
start = 0;
numar_celule = 0;
}

void append(tip_celula valoare) {
int new_numar_celule = numar_celule + 1;
tip_celula *new_start = (tip_celula*) malloc(sizeof (tip_celula) * new_numar_celule);
// if (new_start == NULL)
// throw std::runtime_error("Insufficient RAM for vector !");

for (int i = 0; i < numar_celule; i++)
new_start[i] = start[i];

new_start[numar_celule] = valoare;
numar_celule = new_numar_celule;

if (start != 0) {
free(start);
start = new_start;
} else {
start = new_start;
}
}

tip_celula remove() {
if (numar_celule > 0) {
if (numar_celule > 1) {

tip_celula result = start[numar_celule-1];
numar_celule--;
return result;
}
else
{
tip_celula result = start[0];
free(start);
start = 0;
return result;
}
}
// else
// throw std::runtime_error("No cell in vector !");
}

tip_celula& operator[](int index) {
return start[index];
}

tip_celula at(int index) {
return start[index];
}

//friend std::ostream & operator<<(std::ostream &out, const tip_celula &c) {
// out << c;
// return out;
// }

friend std::ostream & operator<<(std::ostream &out, VECT &c) {
for (int i = 0; i < c.numar_celule; i++)
out << c[i] << " ";

return out;
}
};
void incrementByVal(int a) {
a++;
}

void incrementByRef(int& a) {
a++;
}
template <class T>
T getMint(T a, T b) {
if (a < b)
return a;
else
return b;
}

int minOf(int a, int b) {
if (a < b)
return a;
else
return b;
}
int mainSubiect2() {
int a, b, c, d;
std::cin >> a;
std::cin >> b;
std::cin >> c;
std::cin >> d;

Complex c1(a,b), c2(c,d);

VECT<Complex> vector;
vector.append(c1);
vector.append(c2);

std::cout << c1 << std::endl;
std::cout << c2 << std::endl;

std::cout << vector << std::endl;
std::cout << vector[0] << std::endl;

std::cout << c1 + c2 << std::endl;
std::cout << c1 - c2 << std::endl;

VECT<int> vectorint;
vectorint.append(a);
vectorint.append(b);
vectorint.append(c);
vectorint.append(d);

std::cout << vectorint[3] << std::endl;
return 0;
}

int main(int argc, char** argv) {
mainSubiect2();
}
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>

class Complex {
public:
    Complex (double re = 0.0, double im = 0.0);
    double sqnorm ();
    Complex & operator ++ ();
    Complex operator ++ (int);
    Complex operator + (const Complex & c);
    Complex operator - (const Complex & c);
    bool operator < (Complex a);

private:
    double mRe;
    double mIm;

// friend:
    friend std::ostream & operator << (std::ostream & out, const Complex & c);
};


Complex::Complex(double re, double im)
    : mRe { re },
      mIm { im }
{
}


double Complex::sqnorm()
{
    return mRe*mRe + mIm*mIm;
}


Complex & Complex::operator ++ ()
{
    ++mRe;
    ++mIm;
    return *this;
}


Complex Complex::operator ++ (int)
{
    Complex result(*this);
    ++(*this);
    return result;
}


Complex Complex::operator + (const Complex & c)
{
    return Complex(c.mRe + mRe, c.mIm + mIm);
}


Complex Complex::operator- (const Complex & c)
{
    return Complex(-c.mRe + mRe, -c.mIm + mIm);
}


bool Complex::operator < (Complex a)
{
    if (sqnorm() < a.sqnorm()) {
        return true;
    }
    return false;
}


std::ostream & operator << (std::ostream & out, const Complex & c)
{
    out << c.mRe << ' ';

    if (c.mIm >= 0) {
        out << "+ " << c.mIm << "i";
    } else {
        out << c.mIm << "i";
    }

    return out;
}


template <typename tip_celula> class VECT;

template <typename tip_celula>
std::ostream & operator<<(std::ostream & out, VECT<tip_celula> & c);


template <typename tip_celula>
class VECT {
public:
    VECT();
    void append(tip_celula valoare);
    tip_celula remove();
    tip_celula & operator[] (int index);
    tip_celula at(int index); // should check boundaries!!
    ~VECT();

private:
    tip_celula * start;
    int numar_celule;

// friend:
    friend std::ostream & operator<< <>(std::ostream & out, VECT<tip_celula> & c);
};


template <typename tip_celula>
VECT<tip_celula>::VECT()
    : start { nullptr },
      numar_celule {}
{
}


template <typename tip_celula>
void VECT<tip_celula>::append(tip_celula valoare)
{
    int new_numar_celule = numar_celule + 1;
    tip_celula * new_start = new tip_celula[new_numar_celule];

    for (int i = 0; i < numar_celule; ++i) {
        new_start[i] = start[i];
    }

    new_start[numar_celule] = valoare;
    numar_celule = new_numar_celule;

    if (start != nullptr) {
        delete [] start;
        start = new_start;
    } else {
        start = new_start;
    }
}


template <typename tip_celula>
tip_celula VECT<tip_celula>::remove()
{
    if (numar_celule < 1) {
        // throw?
        // return? What?
        // anywhay, exit - so, whe can assume numar_celule > 0
    }

    if (numar_celule > 1) {

        tip_celula result = start[numar_celule-1];
        --numar_celule;
        return result;
    }

    tip_celula result = start[0];
    delete [] start;
    start = nullptr;
    return result;
}


template <typename tip_celula>
tip_celula & VECT<tip_celula>::operator[] (int index)
{
    return start[index];
}


template <typename tip_celula>
tip_celula VECT<tip_celula>::at(int index) // should check boundaries!!
{
    return start[index];
}


template <typename tip_celula>
VECT<tip_celula>::~VECT()
{
    delete [] start;
}


template <typename tip_celula>
std::ostream & operator<< (std::ostream & out, VECT<tip_celula> & c)
{
    for (int i = 0; i < c.numar_celule; ++i) {
        out << c[i] << ' ';
    }
    return out;
}


void incrementByVal(int a);
void incrementByRef(int & a);
template <typename T> T getMint(T a, T b);
int minOf(int a, int b);
int mainSubiect2();


int main()
{
    mainSubiect2();
}


void incrementByVal(int a)  // This function is totally pointless
{                           // Maybe you want to return the incremented value?
    ++a;
}


void incrementByRef(int & a)
{
    ++a;
}


template <typename T>
T getMint(T a, T b)
{
    if (a < b) {
        return a;
    } else {
        return b;
    }
}


int minOf(int a, int b)
{
    if (a < b) {
        return a;
    } else {
        return b;
    }
}


int mainSubiect2()
{
    std::cout << "Please provide 4 integers separated by spaces: ";
    int a, b, c, d;
    std::cin >> a;
    std::cin >> b;
    std::cin >> c;
    std::cin >> d;

    Complex c1(a,b),
            c2(c,d);

    VECT<Complex> myvec;
    myvec.append(c1);
    myvec.append(c2);

    std::cout << c1 << '\n';
    std::cout << c2 << '\n';

    std::cout << myvec << '\n';
    std::cout << myvec[0] << '\n';

    std::cout << c1 + c2 << '\n';
    std::cout << c1 - c2 << '\n';

    VECT<int> vectorint;
    vectorint.append(a);
    vectorint.append(b);
    vectorint.append(c);
    vectorint.append(d);

    std::cout << vectorint[3] << '\n';
    return 0;
}

Topic archived. No new replies allowed.