Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
136 lines (117 loc) · 3.44 KB

File metadata and controls

136 lines (117 loc) · 3.44 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUMBER_H
#include <iostream>
#include <tuple>
class complex {
public:
complex( double r = 0, double i = 0 )
: real{r}, imag{i}
{ }
complex& operator+=( const complex& other ) {
real += other.real;
imag += other.imag;
return *this;
}
complex& operator-=( const complex& other ) {
real -= other.real;
imag -= other.imag;
return *this;
}
complex& operator++() {
++real;
return *this;
}
complex& operator--() {
--real;
return *this;
}
// implement postincrement in terms of preincrement
complex operator++( int ) {
auto temp = *this;
++*this;
return temp;
}
// implement postdecrement in terms of predecrement
complex operator--( int ) {
auto temp = *this;
--*this;
return temp;
}
std::ostream& print(std::ostream& os ) const {
return os << "(" << real << ", " << imag << ")";
}
friend bool operator==(const complex& lhs, const complex& rhs) {
return (!(lhs.real > rhs.real) &&
!(lhs.real < rhs.real) &&
!(lhs.imag > rhs.imag) &&
!(lhs.imag < rhs.imag));
}
friend bool operator< (const complex& lhs, const complex& rhs) {
// Use std::tie to introduce lexicographical comparison of a struct or class
return std::tie(lhs.real, lhs.imag) < std::tie(rhs.real, rhs.imag);
}
private:
double real, imag;
};
// operator+ is written in terms of operator+=
// operator+ should not be a member function.
complex operator+( complex lhs, const complex& rhs ) {
lhs += rhs;
return lhs;
}
// operator- is written in terms of operator-=
// operator- should not be a member function.
complex operator-( complex lhs, const complex& rhs ) {
lhs -= rhs;
return lhs;
}
// operator<< should not be a member function.
std::ostream& operator<<(std::ostream& os, const complex& c ) {
return c.print(os);
}
// operator>> should not be a member function.
std::istream& operator>>(std::istream& is, complex& c) {
double real{};
double imag{};
is >> real;
char op;
is.get(op);
while(op == ' ' || op == '\n' || op == '\t'){
is.get(op);
}
if (op == ',')
{
while(op == ' ' || op == '\n' || op == '\t'){
is.get(op);
}
int mult;
switch(op){
case '+':
mult = 1;
break;
case '-':
mult = -1;
break;
default :
is.setstate(std::ios::failbit);
return is;
};
is >> imag;
imag *= mult;
c = complex{real, imag};
} else {
is.setstate(std::ios::failbit);
}
return is;
}
// implement operator!= in terms of operator== or
// include <utility> which by default implements operator!= in terms of operator==
bool operator!=(const complex& lhs, const complex& rhs) {
return !(lhs == rhs);
}
// implement the other relational operators in terms of operator< or
// include <utility> which by default implements the other relational operators in terms of operator<
bool operator> (const complex& lhs, const complex& rhs){ return rhs < lhs; }
bool operator<=(const complex& lhs, const complex& rhs){ return !(lhs > rhs); }
bool operator>=(const complex& lhs, const complex& rhs){ return !(lhs < rhs); }
#endif
Morty Proxy This is a proxified and sanitized view of the page, visit original site.