-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex
More file actions
70 lines (63 loc) · 1.68 KB
/
complex
File metadata and controls
70 lines (63 loc) · 1.68 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
66
67
68
69
70
#include <iostream>
#include <sstream>
using namespace std;
struct complex_t {
public:
static unsigned int count;
complex_t() {
real = 0.0f;
imag = 0.0f;
++count;
}
complex_t add(complex_t other) const { // complex_t * const константный указатель, complex_t const * const константный указатель на константный тип
complex_t complex;
complex.real = this->real + other.real;
complex.imag = this->imag + other.imag;
return complex;
}
ostream & output(ostream &stream) const { // complex_t * const
return stream << '(' << this->real << ',' << this->imag << ')' << endl;
}
~complex_t() {
}
void set_real(float value) {
if( value > 0.0f) {
real = value;
}
}
void set_imag(float value) {
if( value > 0.0f) {
imag = value;
}
}
private:
float real;
float imag;
};
bool read(istream & stream, complex_t & complex) {
char ch;
bool res = true;
float real, imag;
if (stream >> ch && ch == '(' &&
stream >> real &&
stream >> ch && ch == ',' &&
stream >> imag &&
stream>> ch && ch == ')') {
complex.set_real(real);
complex.set_imag(imag);
}
else {res = false; cout << "An error has occured while reading input data";}
return res;
}
unsigned int complex_t::count = 0;
int main() {
complex_t cm1, cm2, result;
string string;
getline(cin, string);
istringstream stream(string);
if(read(stream, cm1) && read(stream, cm2)) {
result = cm1.add(cm2);
result.output(cout);
}
cout << complex_t::count;
}