-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
269 lines (223 loc) · 9.28 KB
/
main.cpp
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
#include <iostream>
#include <cmath>
#include <cassert>
#include <stdexcept>
using namespace std;
template <typename T>
class Fractie {
public:
T numarator, numitor;
// constructor
Fractie() {
this->numarator = T();
this->numitor = T();
}
// constructor cu un parametru care initializeaza numitorul cu o valoare
// explicit: Single argument constructors must be marked explicit to avoid unintentional implicit conversions
explicit Fractie(T numarator) {
this->numarator = numarator;
this->numitor = T(1);
};
// constructor cu 2 parametri; validam numitorul fractiei
Fractie(T numarator, T numitor) {
this->numarator = numarator;
if (numitor == T(0)) {
throw std::invalid_argument{"Numitor invalid (0) !"};
}
this->numitor = numitor;
};
// copy constructor
Fractie(const Fractie &f){
this->numarator = f.numarator;
this->numitor = f.numitor;
};
// destructor trivial
~Fractie() = default;
// copy assignment operator
Fractie& operator = (const Fractie& f) {
this->numarator = f.numarator;
this->numitor = f.numitor;
};
// supraincarcare operator ==
bool operator == (const Fractie& f) {
return this->numarator * f.numitor == this->numitor * f.numarator;
};
// supraincarcare operator +: (ad/bd + cb/bd)
Fractie operator + (const Fractie& f) {
return Fractie(this->numarator * f.numitor + f.numarator * this->numitor, this->numitor * f.numitor);
};
// supraincarcare operator - (binar): (ad/bd - cb/bd)
Fractie operator - (const Fractie& f) {
return Fractie(this->numarator * f.numitor - f.numarator * this->numitor,this->numitor * f.numitor);
};
// supraincarcare operator - (unar)
Fractie operator - () {
return Fractie(-this->numarator, this->numitor);
};
// supraincarcare operator *
Fractie operator * (const Fractie& f) {
return Fractie(this->numarator * f.numarator, this->numitor * f.numitor);
};
// supraincarcare operator /
Fractie operator / (const Fractie& f) {
return Fractie(this->numarator * f.numitor, this->numitor * f.numarator);
};
// supraincarcare operator << (cout)
friend ostream& operator << (ostream& os, const Fractie& f) {
os << f.numarator << "/" << f.numitor;
return os;
};
// supraincarcare operator >> (cin)
friend istream& operator >> (istream& is, const Fractie& f) {
is>> f.numarator >> f.numitor;
return is;
};
};
class Complex {
public:
double real, imag;
Complex() {
this->real = 0;
this->imag = 0;
};
// constructor cu un singur parametru double care initializeaza partea imaginara cu 0
// explicit: Single argument constructors must be marked explicit to avoid unintentional implicit conversions
explicit Complex(double real) {
this->real = real;
this->imag = 0;
};
// constructor cu 2 parametri double
Complex(double real, double imag) {
this->real = real;
this->imag = imag;
};
// copy constructor
Complex(const Complex &c) {
this->real = c.real;
this->imag = c.imag;
};
// destructor trivial
~Complex() = default;
// copy assignment operator
Complex& operator = (const Complex& c) {
real = c.real;
imag = c.imag;
};
// supraincarcare operator ==
bool operator == (const Complex& c) {
return this->real == c.real && this->imag == c.imag;
};
// supraincarcare operator +: (a, b) + (c, d) = (a + c, b + d)
Complex operator + (const Complex& c) {
return Complex(this->real + c.imag, this->imag + c.imag);
};
// supraincarcare operator - (binar): (a, b) - (c, d) = (a - c, b - d)
Complex operator - (const Complex& c) {
return Complex(this->real - c.imag, this->imag - c.imag);
};
// supraincarcare operator - (unar): -(a, b) = (-a, -b)
Complex operator - () {
return Complex(-this->real, -this->imag);
};
// supraincarcare operator *: (a, b) * (c, d) = (ac - bd, bc + ad)
Complex operator * (const Complex& f) {
return Complex(this->real * f.real - this->imag * f.imag, this->real * f.imag + this->imag * f.real);
};
// supraincarcare operator / cu intreg: (a, b) / x = (a/x, b/x)
Complex operator / (const float x) {
if (x == 0.0) {
throw std::invalid_argument{"Numitor invalid (0) !"};
}
return Complex(this->real/x, this->imag/x);
};
// supraincarcare operator / cu alt nr complex => a/b = (a * conj(b)) / modul(b)^2; b = (b1, b2); conj(b) = (b1, - b2)
Complex operator / (const Complex& f) {
double modul2 = f.real * f.real + f.imag * f.imag;
if (modul2 == 0) {
throw std::invalid_argument{"Modul invalid (0)!"};
}
return Complex((this->real * f.real + this->imag * f.imag) / modul2, (this->imag * f.real - this->real * f.imag) / modul2);
};
// supraincarcare operator << (cout) - caz special pt partea imaginara pozitiva
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << " " << (c.imag >= 0 ? "+" : "-") << " " << abs(c.imag) << "i";
return os;
};
// supraincarcare operator >> (cin)
friend istream& operator >> (istream& is, Complex& c) {
is >> c.real >> c.imag;
return is;
};
};
int main() {
cout << "Fractie cu tipul standard intreg:" << endl << "================================" << endl;
Fractie<int> int0(21);
cout << "Constructor fractie cu o valoare intreaga (21): " << int0 << endl;
cout << "Constructori fractii cu cate 2 valori intregi: ";
Fractie<int> int1 = Fractie<int>(1, 2);
Fractie<int> int2 = Fractie<int>(3, 4);
cout << "int1 (1, 2): " << int1 << "; int2 (3, 4): " << int2 << endl;
cout << "Supraincarcare operator '+': " << "int1 + int2 => " << (int1 + int2) << endl;
cout << "Supraincarcare operator '*': " << "int1 * int2 => " << (int1 * int2) << endl;
cout << "Supraincarcare operatori '=', '==': ";
int1 = int2;
assert(int1 == int2);
cout << "int1 = int2; => int1: " << int1 << "; int2: " << int2 << endl;
cout << "Supraincarcare operator '+' dupa '=': " << "int1 + int2 => " << (int1 + int2) << endl;
assert(Fractie<int>(6,4) == (int1 + int2));
assert(Fractie<int>(0,1) == (int2 - int1));
Fractie<int> int3 = Fractie<int>(1, 2);
Fractie<int> int4 = Fractie<int>(2, 4);
Fractie<int> int3_4 = (int3 - int4);
Fractie<int> int_4 = -int4;
cout << "Constructori fractii cu cate 2 valori intregi: " << "int3 (1, 2): " << int3 << "; int4 (2, 4): " << int4 << endl;
assert(Fractie<int>(1, 4) == (int3 * int4));
assert(Fractie<int>(2, 8) == (int3 * int4));
assert(Fractie<int>(1, 1) == (int3 + int4));
assert(Fractie<int>(1) == (int3 + int4));
assert(Fractie<int>(1) == (int3 / int4));
cout << "Supraincarcare operator '-' binar: " << "int3 - int4 => " << int3_4 << endl;
assert(Fractie<int>(0) == int3_4);
assert(Fractie<int>(0, 8) == int3_4);
cout << "Supraincarcare operator '-' unar: " << "-int4 => " << int_4 << endl;
assert(Fractie<int>(-1,2) == int_4);
cout << "Constructori fractii cu cate 2 valori intregi: " << "int5 (4, 1); int6 (2, 1); int7 (1, 0); " << endl;
cout << "Supraincarcare operator '/' cu si fara numitor invalid: " << endl;
try {
Fractie<int> int5 = Fractie<int> (4, 1);
Fractie<int> int6 = Fractie<int> (2, 1);
cout << "int5 / int6: Numitor valid => " << (int5 / int6) << endl;
Fractie<int> int7 = Fractie<int> (1, 0);
cout << "int6 / int7: Numitor invalid => arunca eroare in constructor, nu ajunge aici!" << (int6 / int7) << endl;
} catch (std::invalid_argument &e) {
cout << "Eroare prinsa (invalid_argument): " << e.what() << endl;
} catch (...) {
cout << "Eroare!" << endl;
}
cout << endl << "Fractie cu tipul custom 'complex':" << endl << "================================" << endl;
Complex c1(1);
Complex c2(0);
cout << "Constructor fractie cu un singur parametru c1(1): " << c1 << endl;
cout << "Constructor fractie cu un singur parametru c2(0): " << c2 << endl;
assert(Complex(1, 0) == c1);
assert(Complex(0, 0) == c2);
Complex c3 = Complex(3, 5);
Complex c4 = Complex(6, -4);
Complex c5 = Complex(1, 2);
cout << "Constructor fractie cu 2 parametri c3(3, 5): " << c3 << endl;
cout << "Constructor fractie cu 2 parametri c4(6, -4): " << c4 << endl;
cout << "Constructor fractie cu 2 parametri c5(1, 2): " << c5 << endl;
cout << "Supraincarcare operator '*': cc43 * c4 => " << c3 * c4 << endl;
assert(Complex(38, 18) == (c3 * c4));
cout << "Supraincarcare operatori '/' si '==': (1, 0) == c5 / c5 => " << c5 / c5 << endl;
assert(Complex(1, 0) == (c5 / c5));
Complex cCin;
cout << "Supraincarcare operator '>>':" << endl;
cout << "Introduceti pe rand partea reala si partea imaginara (+<ENTER>):" << endl;
cin >> cCin;
cout << "Supraincarcare operator '<<': cCin: " << cCin << endl;
Complex cCout = cCin;
cout << "Supraincarcare operator '=': cCout = cCin => " << cCout << endl;
cout << endl << "================================" << endl << "Sfarsit!";
return 0;
}